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

page option in swagger ui leads to error #227

Closed barnabasJ closed 8 hours ago

barnabasJ commented 2 weeks ago

Describe the bug

Adding a page option in the swagger UI leads to a URL like this

image

curl -X 'GET' \
  'http://localhost:4000/api/json/users?page=%7B%22limit%22%3A%20200%7D&fields%5Buser%5D=id%2Cname' \
  -H 'accept: application/vnd.api+json' \
  -H 'x-csrf-token: ...'

Sending a request to this URL leads to this error

[error] #PID<0.939.0> running Phoenix.Endpoint.SyncCodeReloadPlug (connection #PID<0.919.0>, stream id 3) terminated
Server: localhost:4000 (http)
Request: GET /api/json/users?page=%7B%22limit%22%3A%20200%7D&fields%5Buser%5D=id%2Cname
** (exit) an exception was raised:
    ** (BadMapError) expected a map, got: "{\"limit\": 200}"
        (stdlib 5.1.1) :maps.find("limit", "{\"limit\": 200}")
        (ash_json_api 1.4.6) lib/ash_json_api/controllers/helpers.ex:822: AshJsonApi.Controllers.Helpers.add_pagination_parameter/3
        (ash_json_api 1.4.6) lib/ash_json_api/controllers/helpers.ex:810: AshJsonApi.Controllers.Helpers.fetch_pagination_parameters/1
        (ash_json_api 1.4.6) lib/ash_json_api/controllers/index.ex:21: AshJsonApi.Controllers.Index.call/2
        (be_exercise 0.1.0) deps/plug/lib/plug/router.ex:246: anonymous fn/4 in BeExerciseWeb.AshJsonApiRouter.dispatch/2
        (telemetry 1.3.0) /home/joba/sandbox/be-elixir--code-exercise--barnabasJ/deps/telemetry/src/telemetry.erl:324: :telemetry.span/3
        (be_exercise 0.1.0) deps/plug/lib/plug/router.ex:242: BeExerciseWeb.AshJsonApiRouter.dispatch/2
        (be_exercise 0.1.0) lib/be_exercise_web/ash_json_api_router.ex:1: BeExerciseWeb.AshJsonApiRouter.plug_builder_call/2
        (phoenix 1.7.2) lib/phoenix/router/route.ex:42: Phoenix.Router.Route.call/2
        (phoenix 1.7.2) lib/phoenix/router.ex:430: Phoenix.Router.__call__/5
        (be_exercise 0.1.0) lib/be_exercise_web/endpoint.ex:1: BeExerciseWeb.Endpoint.plug_builder_call/2
        (be_exercise 0.1.0) deps/plug/lib/plug/debugger.ex:136: BeExerciseWeb.Endpoint."call (overridable 3)"/2
        (be_exercise 0.1.0) lib/be_exercise_web/endpoint.ex:1: BeExerciseWeb.Endpoint.call/2
        (phoenix 1.7.2) lib/phoenix/endpoint/sync_code_reload_plug.ex:22: Phoenix.Endpoint.SyncCodeReloadPlug.do_call/4
        (plug_cowboy 2.6.1) lib/plug/cowboy/handler.ex:11: Plug.Cowboy.Handler.init/2
        (cowboy 2.10.0) /project/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
        (cowboy 2.10.0) /project/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
        (cowboy 2.10.0) /project/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
        (stdlib 5.1.1) proc_lib.erl:241: :proc_lib.init_p_do_apply/3

In contrast adding a filter

image

leads to URL like this

curl -X 'GET' \
  'http://localhost:4000/api/json/users?filter%5Bname%5D[ilike]=Jun%&fields%5Buser%5D=id%2Cname' \
  -H 'accept: application/vnd.api+json' \
  -H 'x-csrf-token: ...'

which works correctly

** Runtime

Additional context Add any other context about the problem here.

sevenseacat commented 19 hours ago

I'm not seeing this issue in my project - using AshJsonApi 1.4.7, Ash 3.4.8 and OpenApiSpex 3.20.1.

image

Generates a cURL request like:

curl -X 'GET' \
  'http://localhost:4000/api/json/artists?page%5Blimit%5D=3' \
  -H 'accept: application/vnd.api+json' \
  ...
sevenseacat commented 19 hours ago

I can replicate it when keyset pagination is enabled for my resource, so its something to do with that.

briankariuki commented 18 hours ago

I get this same error. Here's my :read action

image