Skip to content

Commit 90d191a

Browse files
committed
fixs: better handling around struct types w/ resource as instance_of
1 parent 804f064 commit 90d191a

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

lib/ash_json_api/json_schema/json_schema.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ defmodule AshJsonApi.JsonSchema do
371371
end
372372

373373
defp resource_write_attribute_type(%{type: type} = attr, action_type) do
374-
if Ash.Type.embedded_type?(type) do
374+
if embedded?(type) do
375375
embedded_type_input(attr, action_type)
376376
else
377377
if :erlang.function_exported(type, :json_write_schema, 1) do
@@ -439,7 +439,7 @@ defmodule AshJsonApi.JsonSchema do
439439
function_exported?(type, :json_schema, 1) ->
440440
type.json_schema(constraints)
441441

442-
Ash.Type.embedded_type?(type) ->
442+
embedded?(type) ->
443443
%{
444444
"type" => "object",
445445
"properties" => resource_attributes(type),

lib/ash_json_api/json_schema/open_api.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ if Code.ensure_loaded?(OpenApiSpex) do
473473
end
474474

475475
defp resource_write_attribute_type(%{type: type} = attr, action_type) do
476-
if Ash.Type.embedded_type?(type) do
476+
if AshJsonApi.JsonSchema.embedded?(type) do
477477
embedded_type_input(attr, action_type)
478478
else
479479
if :erlang.function_exported(type, :json_write_schema, 1) do
@@ -489,7 +489,7 @@ if Code.ensure_loaded?(OpenApiSpex) do
489489
action_type
490490
) do
491491
if type = constraints[:instance_of] do
492-
if Ash.Type.embedded_type?(type) do
492+
if AshJsonApi.JsonSchema.embedded?(type) do
493493
embedded_type_input(attr, action_type)
494494
else
495495
%Schema{}
@@ -598,7 +598,7 @@ if Code.ensure_loaded?(OpenApiSpex) do
598598

599599
defp resource_attribute_type(%{type: Ash.Type.Struct, constraints: constraints}) do
600600
if type = constraints[:instance_of] do
601-
if Ash.Type.embedded_type?(type) do
601+
if AshJsonApi.JsonSchema.embedded?(type) do
602602
%Schema{
603603
type: :object,
604604
properties: resource_attributes(type),
@@ -616,7 +616,7 @@ if Code.ensure_loaded?(OpenApiSpex) do
616616
constraints = attr.constraints
617617

618618
cond do
619-
Ash.Type.embedded_type?(type) ->
619+
AshJsonApi.JsonSchema.embedded?(type) ->
620620
%Schema{
621621
type: :object,
622622
properties: resource_attributes(type),

lib/ash_json_api/serializer.ex

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,13 +737,21 @@ defmodule AshJsonApi.Serializer do
737737
end
738738

739739
def serialize_value(value, type, constraints, domain) do
740-
{type, _constraints} = flatten_new_type(type, constraints || [])
740+
{type, constraints} = flatten_new_type(type, constraints || [])
741741

742-
if Ash.Type.embedded_type?(type) do
742+
with Ash.Type.Struct <- type,
743+
instance_of when not is_nil(instance_of) <- constraints[:instance_of],
744+
true <- Ash.Resource.Info.resource?(instance_of) do
743745
req = %{fields: %{}, route: %{}, domain: domain}
744746
serialize_attributes(req, value)
745747
else
746-
value
748+
_ ->
749+
if Ash.Type.embedded_type?(type) do
750+
req = %{fields: %{}, route: %{}, domain: domain}
751+
serialize_attributes(req, value)
752+
else
753+
value
754+
end
747755
end
748756
end
749757

0 commit comments

Comments
 (0)