ash-project / ash_json_api

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

Failing to generate filter object for nested relationship #224

Closed diogomrts closed 1 month ago

diogomrts commented 1 month ago

The ash_json_api extension is failing to generate the filter object for a two-times nested relationship. For example, given the relationship where Author has many Posts, and Post belongs to Blog, the Authors endpoint generated should include a reference to the blog-filter, but it was never generated.

To Reproduce

  1. Define a resource where Author has many Posts.
  2. Define that Post belongs to Blog.
  3. Generate an API for Authors.
  4. Observe that the blog-filter is not generated in the Authors endpoint.

This are the Resources i tried with:

Author

defmodule TestRepo.Support.Author do
  use Ash.Resource,
    domain: TestRepo.Support,
    extensions: [AshJsonApi.Resource]

  json_api do
    type("author")

    routes do
      base "/authors"
      index :read
    end
  end

  actions do
    default_accept(:*)
    defaults([:create, :read, :update, :destroy])
  end

  attributes do
    uuid_primary_key(:id, writable?: true)
    attribute(:name, :string, public?: true)
  end

  relationships do
    has_many(:posts, TestRepo.Support.Post, public?: true)
  end
end

Post

defmodule TestRepo.Support.Post do
  use Ash.Resource,
    domain: TestRepo.Support,
    extensions: [AshJsonApi.Resource]

  json_api do
    type("post")
  end

  actions do
    default_accept(:*)
    defaults([:create, :read, :update, :destroy])
  end

  attributes do
    uuid_primary_key(:id, writable?: true)
    attribute(:name, :string, public?: true)
  end

  relationships do
    belongs_to :author, TestRepo.Support.Author, public?: true
    belongs_to :blog, TestRepo.Support.Blog, public?: true
  end
end

Blog

defmodule TestRepo.Support.Blog do
  use Ash.Resource,
    domain: TestRepo.Support,
    extensions: [AshJsonApi.Resource]

  json_api do
    type("blog")

    routes do
      base "/blogs"
      index :read
    end
  end

  actions do
    default_accept(:*)
    defaults([:create, :read, :update, :destroy])
  end

  attributes do
    uuid_primary_key(:id, writable?: true)
    attribute(:name, :string, public?: true)
  end

  relationships do
    has_many :posts, TestRepo.Support.Post, public?: true
  end
end

Runtime