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` at Root Level Not Returning Ash Calculations in JSON Response #194

Closed diogo-felix-martins closed 1 month ago

diogo-felix-martins commented 1 month ago

The default_fields defined at the root level in the json_api block of AshJsonApi is not returning Ash calculations in the JSON response. However, defining default_fields at the route level works as expected. Specifically, the calculation :graphs is not included in the response when using root-level default_fields.

To Reproduce

Define a JSON API with default_fields at the root level including an Ash resource attribute :enabled and an Ash calculation :graphs.

Make a call to the API and observe that the :graphs calculation is not included in the JSON response. Uncomment the default_fields at the route level and observe that the :graphs calculation is now included in the response.

Example code:

json_api do
  type "widget"

  default_fields([:enabled, :graphs])

  routes do
    base("/widgets")

    index :read

    get :get_by_id do
      route("/:id")
      # default_fields([:enabled, :graphs])
    end
  end
end

Expected behavior

The default_fields defined at the root level should return both Ash resource attributes and Ash calculations in the JSON response. But it only returns the attributes, in turn the commented default_fields in the route work as expected.

Runtime

Elixir version: 1.16.3 Erlang version: erts-13.2.2.9 OS: MacOS Ash version:

zachdaniel commented 1 month ago

Fixed in main