-
Hi, there. Love this lib, you guys are doing a nice job maintaining all this. I'm migrating from OAS 2.0 to OAS 3.0 on my app and I wonder if there is a way to have multiples swagger (JSON) files, something like the config on Phoenix Swagger (lib that I use to OAS 2.0): https://hexdocs.pm/phoenix_swagger/getting-started.html#configuration I have different contexts with the same routes on my app and need to keep it in separate swaggers (one for private, one for public, one for admin and goes on) Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Yes I think it should be possible. If you define multiple top level API Definition modules https://github.com/open-api-spex/open_api_spex/blob/master/lib/open_api_spex/open_api.ex#L54-L69, then you can serve them at different paths and use them for validation/casting in your different router scopes. pipeline :private_api do
plug OpenApiSpex.Plug.PutApiSpec, module: MyAppWeb.PrivateApiSpec
... other plugs ...
end
pipeline :public_api do
plug OpenApiSpex.Plug.PutApiSpec, module: MyAppWeb.PublicApiSpec
... other plugs ...
end scope "/public/api" do
pipe_through :public_api
resources "/users", MyAppWeb.UserController, only: [:create, :index, :show]
get "/openapi", OpenApiSpex.Plug.RenderSpec, []
end
scope "/private/api" do
pipe_through :private_api
resources "/admins", MyAppWeb.UserController, only: [:create, :index, :show]
get "/openapi", OpenApiSpex.Plug.RenderSpec, []
end |
Beta Was this translation helpful? Give feedback.
-
To separate the operations between the APIs, you can either: a.) create separate routers for each API or b.) filter the output of The filtering approach is probably the simplest: def spec do
%OpenApi{
servers: [
# Populate the Server info from a phoenix endpoint
Server.from_endpoint(Endpoint)
],
info: %Info{
title: "My App",
version: "1.0"
},
# Populate the paths from a phoenix router, filtering to only the `/api/private` routes
paths:
Router
|> Paths.from_router()
|> Enum.filter(fn {path, _item} -> String.starts_with(path, "/api/private/" end)
|> Map.new()
}
|> OpenApiSpex.resolve_schema_modules() # Discover request/response schemas from path specs
end |
Beta Was this translation helpful? Give feedback.
Yes I think it should be possible.
If you define multiple top level API Definition modules https://github.com/open-api-spex/open_api_spex/blob/master/lib/open_api_spex/open_api.ex#L54-L69, then you can serve them at different paths and use them for validation/casting in your different router scopes.