ash-project / ash_json_api

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

Ash json api don't work with aggregates #69

Closed sezaru closed 1 year ago

sezaru commented 1 year ago

Describe the bug A read action will not work with JSON API if that action uses an aggregate.

To Reproduce

I have a resource that has this aggregate:

  aggregates do
    first :last_bid_price, :bids, :price do
      sort inserted_at: :desc
    end
  end

If I add json api for that resource like this:

  json_api do
    type "property"

    routes do
      base "/property"

      index :read
    end
  end

If I try to get that resource with curl 'localhost:4000/api/markets/property' I will get this error:

[error] #PID<0.811.0> running Phoenix.Endpoint.SyncCodeReloadPlug (connection #PID<0.810.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /api/markets/property
** (exit) an exception was raised:
    ** (MatchError) no match of right hand side value: {:error, "Must provide field type for first"}
        (ash_json_api 0.31.1) lib/ash_json_api/json_schema/json_schema.ex:506: anonymous fn/2 in AshJsonApi.JsonSchema.filter_props/1
...

Which happens in the json_schema.ex:505 file.

This is what the agg variable has at the time the error happens:

 %Ash.Resource.Aggregate{
  name: :last_bid_price,
  relationship_path: [:bids],
  filter: [],
  kind: :first,
  implementation: nil,
  constraints: nil,
  type: nil,
  description: nil,
  private?: false,
  field: :price,
  sort: [inserted_at: :desc],
  default: nil,
  filterable?: true
}

This means that the function is called with: Aggregate.kind_to_type(:first, nil)

Expected behavior The JSON API should work fine with aggregates

** Runtime

zachdaniel commented 1 year ago

fixed in main, thanks for the report!