ash-project / ash_json_api

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

GET routes of a generic action return no id fields even if they are public #263

Open diogomrts opened 2 days ago

diogomrts commented 2 days ago

Description When setting up a GET route on a generic action, the resource's id is omitted from the response.

To Reproduce In this example Resource you can see the issue:

defmodule TestRepo.Domain.Generic do
  use Ash.Resource,
    domain: TestRepo.Domain,
    extensions: [AshJsonApi.Resource]

  json_api do
    type("generic")

    routes do
      base "/generic"
      route(:get, "", :create)
    end
  end

  actions do
    action :create, {:array, :struct }do
      constraints items: [instance_of: __MODULE__]

      run fn input, context ->
        {:ok, [%__MODULE__{
          id: 1,
          name: "name"
        }]}
      end
    end
  end

  attributes do
    integer_primary_key(:id, writable?: true, public?: true)
    # attribute(:id, :integer, primary_key?: true, allow_nil?: false, public?: true)
    attribute(:name, :string, public?: true)
  end
end

The response JSON is:

[
  {
    "name": "name"
  }
]

Expected behavior The expected response should be:

[
  {
    "id": 1,
    "name": "name"
  }
]