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

Open-API Exception w/ Related #112

Closed ulbrich closed 8 months ago

ulbrich commented 8 months ago

Hi there! πŸ‘‹ I get the following exception when accessing the Open-API specs:

Server: localhost:4000 (http)
Request: GET /api/eventhub/open_api
** (exit) an exception was raised:
    ** (RuntimeError) Haven't figured out more complex route parameters yet.
        (ash_json_api 0.34.2) lib/ash_json_api/json_schema/open_api.ex:521: AshJsonApi.OpenApi.operation/3
        (ash_json_api 0.34.2) lib/ash_json_api/json_schema/open_api.ex:505: AshJsonApi.OpenApi.route_operation/3
        (elixir 1.15.3) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
        (elixir 1.15.3) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
        (elixir 1.15.3) lib/enum.ex:4317: Enum.flat_map_list/2
        (elixir 1.15.3) lib/enum.ex:4318: Enum.flat_map_list/2
        (ash_json_api 0.34.2) lib/ash_json_api/json_schema/open_api.ex:489: AshJsonApi.OpenApi.paths/1
        (elixir 1.15.3) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
        (ash_json_api 0.34.2) lib/ash_json_api/json_schema/open_api.ex:482: AshJsonApi.OpenApi.paths/1
        (ash_json_api 0.34.2) lib/ash_json_api/controllers/open_api.ex:54: AshJsonApi.Controllers.OpenApi.spec/2
        (ash_json_api 0.34.2) lib/ash_json_api/controllers/open_api.ex:14: AshJsonApi.Controllers.OpenApi.call/2

I can get rid of the exception, if I remove the following two relations:

related(:supplies) do
  route("/:extid/supplies")
end

[...]

related(:photos, :read) do
  route("/:id/photos")
end

For the first I experimented with changing unless path_params == [] or path_params == ["id"] do to unless is_list(path_params) and Enum.count(path_params) < 2 do as the name of the parameter should be irrelevant (and that helps), but I still run into the second error.

Versions

This is what I have in ASDF .tool-versions:

erlang 25.3.2.2
elixir 1.15.3-otp-25

Dependencies

{:ash, "~> 2.17.22"},
{:ash_graphql, "~> 0.26.8"},
{:ash_json_api, "~> 0.34.2"},
{:ash_postgres, "~> 1.3.68"},
{:basefiftyeight, "~> 0.1.0"},
zachdaniel commented 8 months ago

Thanks for the report! I've pushed something to main that removes that check. I see no use for it at this point. Please try it out and let me know if it works, will cut a release once I get a chance.

ulbrich commented 8 months ago

Looks very good, but I'm running into a followup-error… πŸ˜‡

[error] #PID<0.6986.0> running TickerWeb.Endpoint (connection #PID<0.6971.0>, stream id 2) terminated
Server: localhost:4000 (http)
Request: GET /api/eventhub/open_api
** (exit) an exception was raised:
    ** (KeyError) key :description not found in: nil

If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
        (ash_json_api 0.34.2) lib/ash_json_api/json_schema/open_api.ex:537: AshJsonApi.OpenApi.action_description/2
        (ash_json_api 0.34.2) lib/ash_json_api/json_schema/open_api.ex:523: AshJsonApi.OpenApi.operation/3
        (ash_json_api 0.34.2) lib/ash_json_api/json_schema/open_api.ex:505: AshJsonApi.OpenApi.route_operation/3
        (elixir 1.15.3) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
        (elixir 1.15.3) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
        (elixir 1.15.3) lib/enum.ex:4317: Enum.flat_map_list/2
        (elixir 1.15.3) lib/enum.ex:4318: Enum.flat_map_list/2
        (ash_json_api 0.34.2) lib/ash_json_api/json_schema/open_api.ex:489: AshJsonApi.OpenApi.paths/1
        (elixir 1.15.3) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
        (ash_json_api 0.34.2) lib/ash_json_api/json_schema/open_api.ex:482: AshJsonApi.OpenApi.paths/1
        (ash_json_api 0.34.2) lib/ash_json_api/controllers/open_api.ex:54: AshJsonApi.Controllers.OpenApi.spec/2
        (ash_json_api 0.34.2) lib/ash_json_api/controllers/open_api.ex:14: AshJsonApi.Controllers.OpenApi.call/2
zachdaniel commented 8 months ago

The implication there is that you have a route set up to use an action that doesn't exist. I've added a better error in main. But something like index :foo, ..., :foo has to be an action.

ulbrich commented 8 months ago

Yes, that sounds more like it, but it's not that the action is missing: It's a manual one:

relationships do
  has_many :photos, Ticker.Eventhub.ArticlePhoto do
    manual Ticker.Eventhub.Article.ResolveAddressPhotos
  end
end

[...]

related(:photos, :read) do
  route("/:id/photos")
end

That read doesn't exist, but how can I fix that?

zachdaniel commented 8 months ago

πŸ€” can you put the :read action on the destination resource even if you don't use it?

ulbrich commented 8 months ago

There already is one. The ArticlePhoto model is a Ash.DataLayer.Simple and that's why I'm using a manual lookup from the Article, but it also has its own route. Maybe that's the problem?

zachdaniel commented 8 months ago

πŸ€” okay, I think I see the issue.

zachdaniel commented 8 months ago

For related routes, we need to look at the related resource and its currently not doing that :)

zachdaniel commented 8 months ago

Just pushed something to main :)

ulbrich commented 8 months ago

Wow, that's it: You're the best! πŸ’Œ

zachdaniel commented 8 months ago

Thank you, and thank you for your support πŸ™‡