Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
spinettaro opened this issue Aug 28, 2020 · 7 comments
Open

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

spinettaro opened this issue Aug 28, 2020 · 7 comments

Comments

@spinettaro
Copy link

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?

@mbuhot
Copy link
Contributor

mbuhot commented Aug 28, 2020

@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 ?

@spinettaro
Copy link
Author

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

@mbuhot
Copy link
Contributor

mbuhot commented Aug 31, 2020

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{...}},
  ...
]

@spinettaro
Copy link
Author

weird... it is empty

:ets.tab2list(:validator_table)
[]

@mbuhot
Copy link
Contributor

mbuhot commented Aug 31, 2020

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

@spinettaro
Copy link
Author

spinettaro commented Aug 31, 2020

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

@mbuhot
Copy link
Contributor

mbuhot commented Aug 31, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants