fix: union array argument/attribute handling when current attribute is (#1023)

This commit is contained in:
Jeremy Grant 2024-04-30 12:24:44 +10:00 committed by GitHub
parent 417d39e7de
commit 45b76c209d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 1 deletions

View file

@ -700,6 +700,7 @@ defmodule Ash.Type.Union do
end) do end) do
old_values_by_type = old_values_by_type =
old_values old_values
|> List.wrap()
|> Stream.with_index() |> Stream.with_index()
|> Stream.map(fn {item, index} -> |> Stream.map(fn {item, index} ->
Map.put(item, :__index__, index) Map.put(item, :__index__, index)
@ -860,6 +861,7 @@ defmodule Ash.Type.Union do
end) do end) do
old_values_by_type = old_values_by_type =
old_values old_values
|> List.wrap()
|> Stream.with_index() |> Stream.with_index()
|> Stream.map(fn {item, index} -> |> Stream.map(fn {item, index} ->
Map.put(item, :__index__, index) Map.put(item, :__index__, index)

View file

@ -72,7 +72,7 @@ defmodule Ash.Test.Type.UnionTest do
new_thing = new_thing =
Ash.Changeset.get_argument(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( Ash.Changeset.change_attribute(
changeset, changeset,
@ -81,6 +81,11 @@ defmodule Ash.Test.Type.UnionTest do
) )
end end
end end
update :add_things do
require_atomic? false
accept [:things]
end
end end
attributes do attributes do
@ -384,4 +389,57 @@ defmodule Ash.Test.Type.UnionTest do
|> Ash.Changeset.for_update(:add_thing, %{new_thing: %{type: :foo, foo: "foo"}}) |> Ash.Changeset.for_update(:add_thing, %{new_thing: %{type: :foo, foo: "foo"}})
|> Ash.update() |> Ash.update()
end end
test "it should handle union arguments appropriately" do
assert {:ok, _} =
Example
|> Ash.Changeset.for_create(:create, %{things: []})
|> Ash.create!()
|> Ash.Changeset.for_update(:add_thing, %{
new_thing: %Ash.Union{type: :foo, value: %{type: :foo, foo: "foo"}}
})
|> Ash.update()
end
test "it should cast union arguments appropriately when the array is nil" do
assert {:ok, _} =
Example
|> Ash.Changeset.for_create(:create, %{})
|> Ash.create!()
|> Ash.Changeset.for_update(:add_thing, %{new_thing: %{type: :foo, foo: "foo"}})
|> Ash.update()
end
test "it should handle union arguments appropriately when the array is nil" do
assert {:ok, _} =
Example
|> Ash.Changeset.for_create(:create, %{})
|> Ash.create!()
|> Ash.Changeset.for_update(:add_thing, %{
new_thing: %Ash.Union{type: :foo, value: %{type: :foo, foo: "foo"}}
})
|> Ash.update()
end
test "it should handle union attribute" do
assert {:ok, _} =
Example
|> Ash.Changeset.for_create(:create, %{})
|> Ash.create!()
|> Ash.Changeset.for_update(:add_things, %{
things: [%Ash.Union{type: :foo, value: %{type: :foo, foo: "foo"}}]
})
|> Ash.update()
end
test "it should handle union attribute appropriately when the array is nil" do
assert {:ok, _} =
Example
|> Ash.Changeset.for_create(:create, %{})
|> Ash.create!()
|> Ash.Changeset.for_update(:add_things, %{
things: [%Ash.Union{type: :foo, value: %{type: :foo, foo: "foo"}}]
})
|> Ash.update()
end
end end