You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The situation is that I am including a related resource (in this case "Product") in the GET and relying on the public of default_fields set for that resource.
In the domain module:
json_api do
routes do
base_route "/surveys", DispatchData.Library.Survey do
get :read do
default_fields([:title, :status])
end
end
end
In the main resource module:
json_api do
type "survey"
includes([:client, :visit_type, :product])
default_fields [:title]
end
In the related resource module:
json_api do
type "product"
default_fields([:name])
end
If I set the default_fields for the main "base" resource (in my case "Survey") in the base_route, then the included resource isn't queried for its fields.
However if I set the default_fields for the main "base" resource in the resource module itself (inside the json_api block), then the other related resources is asked for it's fields.
The problem is due to the logic at line 675 of the AshJsonApi.Serializer where fields is determined to be one of three lists.
So it is clear that the priority of the fields value is 1: fields specified in the request, 2: the default_fields on the route, and then 3: the default_attributes on the resource.
The problem happens when this same function is being called for the related resource (i.e. product in my case), where the default_fields set on the route itself ([:title, :status]) have no relevance to the related resource (which only wants [:name])
If I manually swap the second and third parts then the default_fields on the route itself isn't used when there are default fields set for the resource. And of course default_attributes has logic that will look to the explicit default_fields or the public attributes.
So a workaround is to just set the fields to be used as defaults in the resource and not in the route (in this case), but I'd be interested to hear what approach makes most sense.
To Reproduce
In addition to the above, the domain has:
resources do
resource DispatchData.Library.Survey
resource DispatchData.Library.Client
resource DispatchData.Library.Product
resource DispatchData.Library.VisitType
resource DispatchData.Library.Questions
end
The main "survey" resource has:
relationships do
belongs_to :product, DispatchData.Library.Product do
allow_nil? false
public? true
end
And the actions as default with attributes that can be deduced from above.
Expected behavior
I'd expect to receive the following in the "included" part of the response:
Don't use the default_fields in the base_route (in the domain) or in the routes in the main resource and instead rely on the default_fields on the resources themselves.
** Runtime
Elixir version: 1.17.3
Erlang version: Erlang/OTP 27 [erts-15.1.2]
OS: MacOS 15.1.1 (24B91)
Ash version: "3.4.42"
AshJsonAPI version: "1.4.13"
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered:
Describe the bug
The situation is that I am including a related resource (in this case "Product") in the GET and relying on the public of default_fields set for that resource.
In the domain module:
In the main resource module:
In the related resource module:
If I set the default_fields for the main "base" resource (in my case "Survey") in the base_route, then the included resource isn't queried for its fields.
However if I set the default_fields for the main "base" resource in the resource module itself (inside the json_api block), then the other related resources is asked for it's fields.
The problem is due to the logic at line 675 of the AshJsonApi.Serializer where fields is determined to be one of three lists.
So it is clear that the priority of the fields value is 1: fields specified in the request, 2: the default_fields on the route, and then 3: the default_attributes on the resource.
The problem happens when this same function is being called for the related resource (i.e. product in my case), where the default_fields set on the route itself (
[:title, :status]
) have no relevance to the related resource (which only wants[:name]
)If I manually swap the second and third parts then the default_fields on the route itself isn't used when there are default fields set for the resource. And of course default_attributes has logic that will look to the explicit default_fields or the public attributes.
So a workaround is to just set the fields to be used as defaults in the resource and not in the route (in this case), but I'd be interested to hear what approach makes most sense.
To Reproduce
In addition to the above, the domain has:
The main "survey" resource has:
And the actions as default with attributes that can be deduced from above.
Expected behavior
I'd expect to receive the following in the "included" part of the response:
Current behavior
I receive:
** Workaround**
Don't use the default_fields in the base_route (in the domain) or in the routes in the main resource and instead rely on the default_fields on the resources themselves.
** Runtime
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: