fix: change_attribute failing on a union attribute (#778)

This commit is contained in:
Jeremy Grant 2023-11-21 11:10:35 +11:00 committed by GitHub
parent a3a176287d
commit 1ce05562aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View file

@ -530,7 +530,7 @@ defmodule Ash.Type.Union do
|> Map.new() |> Map.new()
|> case do |> case do
%{tag: field, tag_value: tag} -> %{tag: field, tag_value: tag} ->
if new_value[field] == tag || new_value[to_string(field)] == tag, if get_tag(new_value, field) == tag,
do: do_prepare_change(type_name, old_value, new_value, constraints), do: do_prepare_change(type_name, old_value, new_value, constraints),
else: {:ok, new_value} else: {:ok, new_value}

View file

@ -58,6 +58,22 @@ defmodule Ash.Test.Type.UnionTest do
] ]
] ]
end end
attribute :thing, :union,
constraints: [
types: [
foo: [
type: Foo,
tag: :type,
tag_value: "foo"
],
bar: [
type: Bar,
tag: :type,
tag_value: "bar"
]
]
]
end end
end end
@ -234,6 +250,16 @@ defmodule Ash.Test.Type.UnionTest do
end end
end end
test "it handles changing union attribute on a resource" do
Example
|> Ash.Changeset.for_create(:create, %{thing: %Foo{type: "foo", foo: "foo"}})
|> Ash.Test.AnyApi.create!()
|> Ash.Changeset.new()
|> Ash.Changeset.change_attribute(:thing, %Bar{type: "bar", bar: "bar"})
|> Ash.Changeset.for_update(:update)
|> Ash.Test.AnyApi.update!()
end
test "it handles paths on a resource" do test "it handles paths on a resource" do
Example Example
|> Ash.Changeset.for_create(:create, %{things: [%{type: :foo, foo: "bar"}]}) |> Ash.Changeset.for_create(:create, %{things: [%{type: :foo, foo: "bar"}]})