Skip to content

Commit afebe88

Browse files
authored
fix: fixes for embedded resources within unions (#2676)
1 parent 68350bd commit afebe88

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/ash/actions/read/read.ex

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1357,9 +1357,23 @@ defmodule Ash.Actions.Read do
13571357
end)
13581358
end)
13591359

1360+
# Ensure loadable attributes are included in load_through for cleanup,
1361+
# even if they weren't explicitly in the query's load_through.
1362+
# This matches what load_through_attributes does dynamically.
1363+
attribute_load_through =
1364+
resource
1365+
|> Ash.Resource.Info.attributes()
1366+
|> Enum.filter(fn %{name: name, type: type, constraints: constraints} ->
1367+
Ash.Type.can_load?(type, constraints) && (is_nil(query.select) || name in query.select)
1368+
end)
1369+
|> Enum.map(& &1.name)
1370+
|> Enum.reduce(query.load_through[:attribute] || %{}, fn name, acc ->
1371+
Map.put_new(acc, name, [])
1372+
end)
1373+
13601374
records =
13611375
Enum.reduce(
1362-
query.load_through[:attribute] || %{},
1376+
attribute_load_through,
13631377
records,
13641378
fn {attr_name, further_load}, records ->
13651379
attribute = Ash.Resource.Info.attribute(resource, attr_name)

lib/ash/type/union.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,10 @@ defmodule Ash.Type.Union do
464464
end
465465
end
466466

467-
@impl true
468-
def load(unions, [], _, _), do: {:ok, unions}
469-
470467
@impl true
471468
def load(unions, load, constraints, context) do
469+
load = load || []
470+
472471
if Enum.any?(constraints[:types], fn {_name, config} ->
473472
Ash.Type.can_load?(config[:type], config[:constraints])
474473
end) do
@@ -1317,6 +1316,8 @@ defmodule Ash.Type.Union do
13171316
{:ok, type_config} <- Keyword.fetch(type_configs, type_name),
13181317
{:ok, type} <- Keyword.fetch(type_config, :type),
13191318
type_constraints <- Keyword.get(type_config, :constraints, []),
1319+
type_constraints <-
1320+
Keyword.put(type_constraints, :__union_tag__, type_config[:tag]),
13201321
type <- Ash.Type.get_type(type),
13211322
{:ok, value} <- Ash.Type.prepare_change(type, old_value, new_value, type_constraints) do
13221323
{:ok, %Ash.Union{type: type_name, value: value}}
@@ -1332,6 +1333,8 @@ defmodule Ash.Type.Union do
13321333
{:ok, type_config} <- Keyword.fetch(type_configs, type_name),
13331334
{:ok, type} <- Keyword.fetch(type_config, :type),
13341335
type_constraints <- Keyword.get(type_config, :constraints, []),
1336+
type_constraints <-
1337+
Keyword.put(type_constraints, :__union_tag__, type_config[:tag]),
13351338
type <- Ash.Type.get_type(type),
13361339
{:ok, value} <- Ash.Type.prepare_change(type, nil, new_value, type_constraints) do
13371340
{:ok, %Ash.Union{type: type_name, value: value}}

0 commit comments

Comments
 (0)