livery_openapi (livery v0.2.0)

View Source

OpenAPI 3.1 document generation from route metadata.

build/1 turns a list of routes into an OpenAPI 3.1 document (a JSON-encodable map). Each route is {Method, Path, Handler} or {Method, Path, Handler, Meta}; the optional Meta map carries operation-level fields:

#{
    operation_id => binary(),
    summary      => binary(),
    description  => binary(),
    tags         => [binary()],
    parameters   => [map()],   %% extra (non-path) parameters
    request_body => map(),     %% OpenAPI requestBody object
    responses    => #{100..599 | binary() => map()}
}

Livery path templates (:param, *wildcard) are rewritten to OpenAPI's {param} form, and a path parameter is synthesised for each captured segment.

Doc = livery_openapi:build(#{
    info   => #{title => <<"My API">>, version => <<"1.0.0">>},
    routes => [
        {<<"GET">>, <<"/users/:id">>, {users, show},
         #{summary => <<"Fetch a user">>,
           responses => #{200 => #{description => <<"the user">>}}}}
    ]
}),
JsonBytes = livery_openapi:to_json(Doc).

handler/1 returns a Livery handler that serves the document as application/json; mount it at /openapi.json.

Summary

Functions

Build an OpenAPI 3.1 document map from routes + info.

Return a Livery handler that serves the given document as application/json. Mount it at /openapi.json.

Redoc UI handler loading the spec from /openapi.json.

Return a Livery handler serving a Redoc documentation page that loads the OpenAPI spec from SpecUrl. Self-contained HTML (the Redoc bundle is pulled from a CDN); no static files or livery_resp:file support needed.

Swagger UI handler loading the spec from /openapi.json.

Return a Livery handler serving a Swagger UI documentation page that loads the OpenAPI spec from SpecUrl. Self-contained HTML (the Swagger UI bundle is pulled from a CDN); no static files needed.

Encode an OpenAPI document to JSON bytes.

Types

build_opts()

-type build_opts() :: #{info := map(), routes := [route()], servers => [map()]}.

document()

-type document() :: map().

route()

-type route() :: {binary(), binary(), term()} | {binary(), binary(), term(), map()}.

Functions

build(Opts)

-spec build(build_opts()) -> document().

Build an OpenAPI 3.1 document map from routes + info.

handler(Doc)

-spec handler(document()) -> fun((livery_req:req()) -> livery_resp:resp()).

Return a Livery handler that serves the given document as application/json. Mount it at /openapi.json.

redoc_handler()

-spec redoc_handler() -> fun((livery_req:req()) -> livery_resp:resp()).

Redoc UI handler loading the spec from /openapi.json.

redoc_handler(SpecUrl)

-spec redoc_handler(binary()) -> fun((livery_req:req()) -> livery_resp:resp()).

Return a Livery handler serving a Redoc documentation page that loads the OpenAPI spec from SpecUrl. Self-contained HTML (the Redoc bundle is pulled from a CDN); no static files or livery_resp:file support needed.

swagger_ui_handler()

-spec swagger_ui_handler() -> fun((livery_req:req()) -> livery_resp:resp()).

Swagger UI handler loading the spec from /openapi.json.

swagger_ui_handler(SpecUrl)

-spec swagger_ui_handler(binary()) -> fun((livery_req:req()) -> livery_resp:resp()).

Return a Livery handler serving a Swagger UI documentation page that loads the OpenAPI spec from SpecUrl. Self-contained HTML (the Swagger UI bundle is pulled from a CDN); no static files needed.

to_json(Doc)

-spec to_json(document()) -> binary().

Encode an OpenAPI document to JSON bytes.