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

Better error messages when attempting to include private relationships in a request #166

Closed sevenseacat closed 5 months ago

sevenseacat commented 5 months ago

Describe the bug

When attempting to include a relationship that is not marked public? true, a confusing error message is returned. May be tangentially related to #10 .

To Reproduce

The resource:

Tunez.Music.Artist:

  json_api do
    type "artist"
    includes [:albums]

    routes do
      base "/artists"
      get :read
    end
  end

  relationships do
    has_many :albums, Tunez.Music.Album do
      # public? true
    end
  end

The request:

http://localhost:4000/api/artists/4fad50ae-9f07-49c1-a04a-560477a5d47d?include=albums

The error:

[error] ** (ArgumentError) `nil` is not a Spark DSL module.

    nil.fetch_opt([:resource], :base_filter)
    (spark 2.2.1) lib/spark/dsl/extension.ex:262: Spark.Dsl.Extension.do_fetch_opt/3
    (spark 2.2.1) lib/spark/dsl/extension.ex:208: Spark.Dsl.Extension.get_opt/5
    (ash 3.0.12) lib/ash/query/query.ex:422: Ash.Query.new/2
    (ash_json_api 1.2.0) lib/ash_json_api/request.ex:508: anonymous fn/5 in AshJsonApi.Request.set_include_queries/5
    (elixir 1.17.0) lib/enum.ex:1703: Enum."-map/2-lists^map/1-1-"/2
    ...

If uncommenting the public? true line on the relationship, the request works as expected.

Expected behavior

A properly formatted JSON response to the user that they've requested something invalid.

I wonder if we could also make it a compilation error for the developer - you can't include something that isn't public?

Runtime