Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding **default_fields** to base_route unexpectedly skips using default_fields in included resources #264

Closed
thevole opened this issue Dec 4, 2024 · 0 comments
Labels
bug Something isn't working needs review

Comments

@thevole
Copy link

thevole commented Dec 4, 2024

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:

  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.

 defp serialize_attributes(request, %resource{} = record) do
    fields =
      Map.get(request.fields, resource) || Map.get(request.route, :default_fields) ||
        default_attributes(resource)

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:

 "included": [
    {
      "attributes": {
        "name": "SD-WAN"
      },
     ...

Current behavior
I receive:

 "included": [
    {
      "attributes": {},
      "id": "346f0262-5f83-433b-b217-baec62f4b317",
   ...

** 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

  • 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.

@thevole thevole added bug Something isn't working needs review labels Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs review
Projects
None yet
Development

No branches or pull requests

1 participant