Skip to content

Commit

Permalink
Merge pull request #103 from open-api-spex/spec-behaviour
Browse files Browse the repository at this point in the history
Add behaviour to OpenApi module documenting the `spec/0` callback.
  • Loading branch information
mbuhot authored Apr 28, 2019
2 parents 60eda20 + 7593733 commit 716f453
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ Start by adding an `ApiSpec` module to your application to populate an `OpenApiS
```elixir
defmodule MyApp.ApiSpec do
alias OpenApiSpex.{OpenApi, Server, Info, Paths}
@behaviour OpenApi

@impl OpenApi
def spec do
%OpenApi{
servers: [
Expand Down
4 changes: 3 additions & 1 deletion examples/phoenix_app/lib/phoenix_app_web/api_spec.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule PhoenixAppWeb.ApiSpec do
alias OpenApiSpex.{Info, OpenApi, Paths}
@behaviour OpenApi

@impl OpenApi
def spec do
%OpenApi{
info: %Info{
Expand All @@ -11,4 +13,4 @@ defmodule PhoenixAppWeb.ApiSpec do
}
|> OpenApiSpex.resolve_schema_modules()
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule PhoenixAppWeb.UserController do
}
end

def create(conn = %{body_params: %Schemas.UserRequest{user: user_params}}, %{group_id: group_id}) do
def create(conn = %{body_params: %Schemas.UserRequest{user: user_params}}, %{group_id: _group_id}) do
with {:ok, %User{} = user} <- Accounts.create_user(user_params) do
conn
|> put_status(:created)
Expand Down
4 changes: 3 additions & 1 deletion examples/plug_app/lib/plug_app/api_spec.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule PlugApp.ApiSpec do
alias OpenApiSpex.{Info, OpenApi, Paths}
alias OpenApiSpex.{Info, OpenApi}
@behaviour OpenApi

@impl OpenApi
def spec do
%OpenApi{
info: %Info{
Expand Down
29 changes: 27 additions & 2 deletions lib/open_api_spex/open_api.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule OpenApiSpex.OpenApi do
defmodule OpenApiSpex.OpenApi do
@moduledoc """
Defines the `OpenApiSpex.OpenApi.t` type.
Defines the `OpenApiSpex.OpenApi.t` type and the behaviour for application modules that
construct an `OpenApiSpex.OpenApi.t` at runtime.
"""
alias OpenApiSpex.{
Info, Server, Paths, Components,
Expand Down Expand Up @@ -35,6 +36,30 @@ defmodule OpenApiSpex.OpenApi do
externalDocs: ExternalDocumentation.t | nil
}

@doc """
A spec/0 callback function is required for use with the `OpenApiSpex.Plug.PutApiSpec` plug.
## Example
@impl OpenApiSpex.OpenApi
def spec do
%OpenApi{
servers: [
# Populate the Server info from a phoenix endpoint
Server.from_endpoint(MyAppWeb.Endpoint, otp_app: :my_app)
],
info: %Info{
title: "My App",
version: "1.0"
},
# populate the paths from a phoenix router
paths: Paths.from_router(MyAppWeb.Router)
}
|> OpenApiSpex.resolve_schema_modules() # discover request/response schemas from path specs
end
"""
@callback spec() :: t

@json_encoder Enum.find([Jason, Poison], &Code.ensure_loaded?/1)

def json_encoder, do: @json_encoder
Expand Down
4 changes: 4 additions & 0 deletions lib/open_api_spex/plug/put_api_spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ defmodule OpenApiSpex.Plug.PutApiSpec do
This allows downstream plugs to use the API spec for casting, validating and rendering.
## Options
- module: A module implementing the `OpenApiSpex.OpenApi` behaviour
## Example
plug OpenApiSpex.Plug.PutApiSpec, module: MyAppWeb.ApiSpec
Expand Down
3 changes: 3 additions & 0 deletions test/operation2_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ defmodule OpenApiSpex.Operation2Test do
end

defmodule SpecModule do
@behaviour OpenApiSpex.OpenApi

@impl OpenApiSpex.OpenApi
def spec do
paths = %{
"/users" => %{
Expand Down
2 changes: 2 additions & 0 deletions test/support/api_spec.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule OpenApiSpexTest.ApiSpec do
alias OpenApiSpex.{OpenApi, Contact, License, Paths, Server, Info, Components}
alias OpenApiSpexTest.{Router, Schemas}
@behaviour OpenApi

@impl OpenApi
def spec() do
%OpenApi{
servers: [
Expand Down

0 comments on commit 716f453

Please sign in to comment.