diff --git a/lib/ash/type/union.ex b/lib/ash/type/union.ex index 69e6a3d2..93fef360 100644 --- a/lib/ash/type/union.ex +++ b/lib/ash/type/union.ex @@ -700,6 +700,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) @@ -860,6 +861,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) diff --git a/test/type/union_test.exs b/test/type/union_test.exs index 33665723..0656de4f 100644 --- a/test/type/union_test.exs +++ b/test/type/union_test.exs @@ -72,7 +72,7 @@ defmodule Ash.Test.Type.UnionTest do 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, @@ -81,6 +81,11 @@ defmodule Ash.Test.Type.UnionTest do ) end end + + update :add_things do + require_atomic? false + accept [:things] + end end 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.update() 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