phoenix_swagger icon indicating copy to clipboard operation
phoenix_swagger copied to clipboard

PhoenixSwagger.Plug.Validate doesn't work for versioned API

Open spinettaro opened this issue 5 years ago • 7 comments

Hi ,

it seems the PhoenixSwagger.Plug.Validate is not working if you have aliases in the API declaration. Here the example:

pipeline :api do
  plug :accepts, ["json"]
  plug PhoenixSwagger.Plug.Validate
end

scope "/api", MyServer, as: :api do
  pipe_through [:api]

  scope "/v1", Api.V1, as: :v1 do
    get     "/dashboards",      ControllerDashboard, :get_dashboards
  end
end

The error is:

{
    "error": {
        "path": "/api/v1/dashboards",
        "message": "API does not provide resource"
    }
}

Any suggestions?

spinettaro avatar Aug 28 '20 08:08 spinettaro

@spinettaro I don't think nested scopes should be a problem. PhoenixSwagger uses the output of Router.__routes__ to enumerate the paths.

Can you show the output of mix phx.routes ?

mbuhot avatar Aug 28 '20 22:08 mbuhot

yes, no prob. Here the output: api_v1_controller_dashboard_path GET /api/v1/dashboards MyServer.Api.V1.ControllerDashboard :get_dashboards

spinettaro avatar Aug 31 '20 08:08 spinettaro

Looks fine. There should be a corresponding entry in the :validator_table ets table. You can see the contents in an iex shell with :ets.tab2list(:validator_table), eg:

iex -S mix
Interactive Elixir (1.10.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> :ets.tab2list(:validator_table)
[
  {"/get/api/users", nil, %ExJsonSchema.Schema.Root{...}},
  ...
]

mbuhot avatar Aug 31 '20 09:08 mbuhot

weird... it is empty

:ets.tab2list(:validator_table) []

spinettaro avatar Aug 31 '20 09:08 spinettaro

That table is populated by a call to PhoenixSwagger.Validator.parse_swagger_schema/1, eg in your Application start callback: https://github.com/xerions/phoenix_swagger/blob/master/examples/simple/lib/simple/application.ex#L8

mbuhot avatar Aug 31 '20 09:08 mbuhot

I didn't put that line on my Application.ex, because to be honest I didn't find it in the doc guide. After that I have this error:

** (Mix) Could not start application my_server: exited in: MyServer.Application.start(:normal, [])
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function ExJsonSchema.Schema.resolve/1 is undefined (module ExJsonSchema.Schema is not available)
...
...
...
            (phoenix_swagger 0.8.2) lib/phoenix_swagger/validator.ex:137: anonymous fn/4 in PhoenixSwagger.Validator.collect_schema_attrs/1
            (elixir 1.10.0) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
            (elixir 1.10.0) lib/enum.ex:1400: anonymous fn/3 in Enum.map/2
            (stdlib 3.8) maps.erl:257: :maps.fold_1/3
            (elixir 1.10.0) lib/enum.ex:2127: Enum.map/2
            (phoenix_swagger 0.8.2) lib/phoenix_swagger/validator.ex:99: PhoenixSwagger.Validator.collect_schema_attrs/1
            (cw_file_server 0.9.130) lib/my_server/application.ex:14: MyServer.Application.start/2
            (kernel 6.3) application_master.erl:277: :application_master.start_it_old/4

spinettaro avatar Aug 31 '20 09:08 spinettaro

That one comes from the ex_json_schema library being an optional dependency. Add {:ex_json_schema, "~> 0.7"}, to the deps list in mix.exs to make it available.

mbuhot avatar Aug 31 '20 09:08 mbuhot