Skip to content

Commit

Permalink
fix: union array argument/attribute handling when current attribute is (
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremygrant authored Apr 30, 2024
1 parent 9ff8630 commit 66e95c6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/ash/type/union.ex
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ defmodule Ash.Type.Union do
end) do
old_values_by_type =
old_values
|> List.wrap()
|> Stream.with_index()
|> Stream.map(fn {item, index} ->
Map.put(item, :__index__, index)
Expand Down Expand Up @@ -803,6 +804,7 @@ defmodule Ash.Type.Union do
end) do
old_values_by_type =
old_values
|> List.wrap()
|> Stream.with_index()
|> Stream.map(fn {item, index} ->
Map.put(item, :__index__, index)
Expand Down
59 changes: 58 additions & 1 deletion test/type/union_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule Ash.Test.Type.UnionTest do

change fn changeset, _ ->
new_thing = Ash.Changeset.get_argument(changeset, :new_thing)
things = Ash.Changeset.get_attribute(changeset, :things)
things = Ash.Changeset.get_attribute(changeset, :things) || []

Ash.Changeset.change_attribute(
changeset,
Expand All @@ -71,6 +71,10 @@ defmodule Ash.Test.Type.UnionTest do
)
end
end

update :add_things do
accept [:things]
end
end

attributes do
Expand Down Expand Up @@ -361,4 +365,57 @@ defmodule Ash.Test.Type.UnionTest do
|> Ash.Changeset.for_update(:add_thing, %{new_thing: %{type: :foo, foo: "foo"}})
|> Ash.Test.AnyApi.update()
end

test "it should handle union arguments appropriately" do
assert {:ok, _} =
Example
|> Ash.Changeset.for_create(:create, %{things: []})
|> Ash.Test.AnyApi.create!()
|> Ash.Changeset.for_update(:add_thing, %{
new_thing: %Ash.Union{type: :foo, value: %{type: :foo, foo: "foo"}}
})
|> Ash.Test.AnyApi.update()
end

test "it should cast union arguments appropriately when the array is nil" do
assert {:ok, _} =
Example
|> Ash.Changeset.for_create(:create, %{})
|> Ash.Test.AnyApi.create!()
|> Ash.Changeset.for_update(:add_thing, %{new_thing: %{type: :foo, foo: "foo"}})
|> Ash.Test.AnyApi.update()
end

test "it should handle union arguments appropriately when the array is nil" do
assert {:ok, _} =
Example
|> Ash.Changeset.for_create(:create, %{})
|> Ash.Test.AnyApi.create!()
|> Ash.Changeset.for_update(:add_thing, %{
new_thing: %Ash.Union{type: :foo, value: %{type: :foo, foo: "foo"}}
})
|> Ash.Test.AnyApi.update()
end

test "it should handle union attribute" do
assert {:ok, _} =
Example
|> Ash.Changeset.for_create(:create, %{})
|> Ash.Test.AnyApi.create!()
|> Ash.Changeset.for_update(:add_things, %{
things: [%Ash.Union{type: :foo, value: %{type: :foo, foo: "foo"}}]
})
|> Ash.Test.AnyApi.update()
end

test "it should handle union attribute appropriately when the array is nil" do
assert {:ok, _} =
Example
|> Ash.Changeset.for_create(:create, %{})
|> Ash.Test.AnyApi.create!()
|> Ash.Changeset.for_update(:add_things, %{
things: [%Ash.Union{type: :foo, value: %{type: :foo, foo: "foo"}}]
})
|> Ash.Test.AnyApi.update()
end
end

0 comments on commit 66e95c6

Please sign in to comment.