|> OpenApiSpex.resolve_schema_modules() # discover request/response schemas from path specs
end
end
```
For each plug (controller) that will handle api requests, add an `open_api_operation` callback.
It will be passed the plug opts that were declared in the router, this will be the action for a phoenix controller. The callback populates an `OpenApiSpex.Operation` struct describing the plug/action.
+[example web app](https://github.com/open-api-spex/open_api_spex/blob/master/examples/phoenix_app/lib/phoenix_app_web/controllers/user_controller.ex).
-Declare the JSON schemas for request/response bodies in a `Schemas` module:
-Each module should implement the `OpenApiSpex.Schema` behaviour.
-The only callback is `schema/0`, which should return an `OpenApiSpex.Schema` struct.
-You may optionally declare a struct, linked to the JSON schema through the `x-struct` extension property.
-See `OpenApiSpex.schema/1` macro for a convenient way to reduce some boilerplate.
+Next, declare JSON schema modules for the request and response bodies.
+In each schema module, call `OpenApiSpex.schema/1`, passing the schema definition. The schema must
+have keys described in `OpenApiSpex.Schema.t`. This will define a `%OpenApiSpex.Schema{}` struct.
+This struct is made available from the `schema/0` public function, which is generated by `OpenApiSpex.schema/1`.
+
+You may optionally have the data described by the schema turned into a struct linked to the JSON schema by adding `"x-struct": __MODULE__`
+Optionally, you can create a mix task to write the swagger file to disk:
+
+```elixir
+defmodule Mix.Tasks.MyApp.OpenApiSpec do
+ def run([output_file]) do
+ json =
+ MyAppWeb.ApiSpec.spec()
+ |> Jason.encode!(pretty: true)
+
+ :ok = File.write!(output_file, json)
+ end
+end
+```
+
+Generate the file with: `mix my_app.openapispec spec.json`
+
## Serve Swagger UI
-Once your API spec is available through a route, the `OpenApiSpex.Plug.SwaggerUI` plug can be used to serve a SwaggerUI interface. The `path:` plug option must be supplied to give the path to the API spec.
+Once your API spec is available through a route (see "Serve the Spec"), the `OpenApiSpex.Plug.SwaggerUI` plug can be used to
+serve a SwaggerUI interface. The `path:` plug option must be supplied to give the path to the API spec.
All JavaScript and CSS assets are sourced from cdnjs.cloudflare.com, rather than vendoring into this package.
```elixir
scope "/" do
pipe_through :browser # Use the default browser stack
get "/", MyAppWeb.PageController, :index
get "/swaggerui", OpenApiSpex.Plug.SwaggerUI, path: "/api/openapi"
-Add the `OpenApiSpex.Plug.CastAndValidate` plug to a controller to validate request parameters, and to cast to Elixir types defined by the operation schema.
+OpenApiSpex can automatically validate requests before they reach the controller action function. Or if you prefer,
+you can explicitly call on OpenApiSpex to cast and validate the params within the controller action. This section
+describes the former.
+
+First, the `plug OpenApiSpex.Plug.PutApiSpec` needs to be called in the Router, as described above.
+
+Add the `OpenApiSpex.Plug.CastAndValidate` plug to a controller to validate request parameters and to cast to Elixir types defined by the operation schema.