ash-project / ash_json_api

The JSON:API extension for the Ash Framework
https://hexdocs.pm/ash_json_api
MIT License
55 stars 41 forks source link

`default_fields` doesn't seem to do anything? #162

Closed sevenseacat closed 1 month ago

sevenseacat commented 1 month ago

Describe the bug

I've tested it with normal attributes, I've tested it with aggregates, nothing gets changed in the response. Maybe I'm misunderstanding how it's supposed to be used?

To Reproduce

If I define the following JSON API configuration in my resource:

  json_api do
    type "artist"
    default_fields [:biography, :cover_image_url]

    routes do
      base "/artists"

      get :read
    end
  end

In this case, biography is a non-public attribute and cover_image_url is a public aggregate.

Querying for a single record using this route returns a response with neither field in it, only the public attributes (which is what default_fields is supposed to override?)

{
  "data": {
    "attributes": {
      "name": "Crystal Cove",
      "previous_names": [],
      "inserted_at": "2024-05-16T07:18:03.868560Z",
      "updated_at": "2024-06-10T06:27:02.922313Z"
    },
    "id": "0faccf9d-d88a-432d-ac2c-a5f0faa82b5f",
    "links": {},
    "meta": {},
    "type": "artist",
    "relationships": {}
  },
  "links": {
    "self": "http://localhost:4000/api/artists/0faccf9d-d88a-432d-ac2c-a5f0faa82b5f"
  },
  "meta": {},
  "jsonapi": {
    "version": "1.0"
  }
}

Expected behavior

The response should have only the fields I specify in default_fields.

(Ideally the default list would include public calculations/aggregates in the list so I don't need to specify default_fields to include them, but that's probably a separate feature request)

** Runtime

zachdaniel commented 1 month ago

Thanks for the report! Fixed in 7975b970cc6a4c3588d44d3bf77eefd2c54ef03b