From 1ce05562aa7623932ee1cf501078a472767e5be9 Mon Sep 17 00:00:00 2001 From: Jeremy Grant Date: Tue, 21 Nov 2023 11:10:35 +1100 Subject: [PATCH] fix: change_attribute failing on a union attribute (#778) --- lib/ash/type/union.ex | 2 +- test/type/union_test.exs | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/ash/type/union.ex b/lib/ash/type/union.ex index 1d05736d..4d12651e 100644 --- a/lib/ash/type/union.ex +++ b/lib/ash/type/union.ex @@ -530,7 +530,7 @@ defmodule Ash.Type.Union do |> Map.new() |> case do %{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), else: {:ok, new_value} diff --git a/test/type/union_test.exs b/test/type/union_test.exs index 0e1a7775..04f3bf5a 100644 --- a/test/type/union_test.exs +++ b/test/type/union_test.exs @@ -58,6 +58,22 @@ defmodule Ash.Test.Type.UnionTest do ] ] end + + attribute :thing, :union, + constraints: [ + types: [ + foo: [ + type: Foo, + tag: :type, + tag_value: "foo" + ], + bar: [ + type: Bar, + tag: :type, + tag_value: "bar" + ] + ] + ] end end @@ -234,6 +250,16 @@ defmodule Ash.Test.Type.UnionTest do 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 Example |> Ash.Changeset.for_create(:create, %{things: [%{type: :foo, foo: "bar"}]})