mbuhot
OpenApiSpex - OpenAPI / Swagger 3.0 for Plug APIs
Leverage Open Api 3.0 (Swagger) to document, test, validate and explore your Plug and Phoenix APIs.
- Generate and serve a JSON Open API (swagger) document from your code
- Use the spec to cast request params to well defined schema structs
- Validate params against schemas, eliminate bad requests before they hit your controllers
- Validate responses against schemas in tests, ensuring your docs are accurate and reliable
- Explore the API interactively with with SwaggerUI
Github: https://github.com/mbuhot/open_api_spex
Docs: https://hexdocs.pm/open_api_spex/
Hex: https://hex.pm/packages/open_api_spex
Integrating with phoenix controllers:
@spec open_api_operation(atom) :: Operation.t
def open_api_operation(action), do: apply(__MODULE__, :"#{action}_operation", [])
@spec show_operation() :: Operation.t
def show_operation() do
%Operation{
tags: ["users"],
summary: "Show user",
description: "Show a user by ID",
operationId: "UserController.show",
parameters: [
Operation.parameter(:id, :path, :integer, "User ID", example: 123)
],
responses: %{
200 => Operation.response("User", "application/json", UserResponse)
}
}
end
def show(conn, %{"id" => id}) do
{:ok, user} = MyApp.Users.find_by_id(id)
json(conn, 200, user)
end
Cast and validate params:
plug OpenApiSpex.Plug.Cast
plug OpenApiSpex.Plug.Validate
def open_api_operation(action) do
apply(__MODULE__, :"#{action}_operation", [])
end
def create_operation do
import Operation
%Operation{
tags: ["users"],
summary: "Create user",
description: "Create a user",
operationId: "UserController.create",
parameters: [],
requestBody: request_body("The user attributes", "application/json", UserRequest),
responses: %{
201 => response("User", "application/json", UserResponse)
}
}
end
def create(conn, request = %UserRequest{user: %User{name: name, email: email, birthday: birthday = %Date{}}}) do
...
end
Most Liked
mbuhot
Version 3.2.0 released.
Lots of internal improvements and some new features thanks to the wonderful contributors, particularly @moxley and the team at Ghost Group / Weedmaps. 
Highlights:
- Jason library support for JSON serialization
- Improved memory usage
- Improved error messages and validations
- Integrate
SwaggerUIwithPlug.CSRFProtection
See the changelog for more details.
moxley
Version 3.5.0 is released!
This version has a lot of changes from the last several months. Highlights:
- Ability to import Open API documents into OpenApiSpex, as an alternative to defining specs using using OpenApiSpex’ Elixir syntax.
- Experimental support for ExDoc-based operation specs. Use doc tags in your controller to define operations instead of defining callback functions.
- Improved Open API compliance and bug fixes for casting and validations
- Deprecate the legacy cast & validate implementation for the newer Cast module.
Thank you to @mbuhot for letting me manage the release.
moxley
Version 3.10.0 is released!
- Feature: Support OAuth2 for swagger-ui (#217)
- Feature: Support
defaultresponse type in responses (#301) - Feature: Allow overriding
x-structinOpenApiSpex.schema/1(#304) - Feature: Ability to specify
deprecatedin ControllerSpec operation (#296) - Feature:
:struct?and:derive?options inOpenApiSpex.schema/1(#312) - Feature:
OpenApiSpex.add_schemas/2(#314) - Enhancement: Remove api_spec data from Conn (#286)
- Enhancement: More informative errors for bad schema (#288, #284, #287)
- Fix: Convert
:formatvalue to atom when decoding schema file (#293) - Fix: Type spec in OpenApiSpex.Info
- Fix: Elixir Formatter rules in published package (#306)
- Docs: Fix spelling error in example code (#295)
- Docs: Fix type in README (#297)
- Docs: Fix links and punctuation in README (#298)
- Docs: Promote ControlerSpecs as the preferred API for controller operations (#311)
As always, a huge thank you to all the contributors and maintainers for keeping the project going 







