| description | Generate an OpenAPI 3.1 document from your endpoint contracts, serve it, and know what it cannot describe. |
|---|
Azu::OpenAPI builds an OpenAPI 3.1 document from the
Endpoint(Request, Response) contracts of an app, and Azu::OpenAPI::Handler serves it. The request and response
types are the source of truth, so the document cannot drift from the code. It is part of require "azu"; nothing is
built until you ask for the document. It writes JSON, and with docs: the handler also serves a reference page of
it. It is not the azu openapi:export command of the Azu CLI (see Limits).
require "azu"
struct CreateUserRequest
include Azu::Request
getter name : String
getter email : String
getter age : Int32 = 0
validate name, presence: true, size: 2..50
validate email, match: /@/
validate age, gte: 0
def initialize(@name = "", @email = "", @age = 0)
end
end
struct UserResponse
include Azu::Response
include JSON::Serializable
property name : String
def initialize(@name : String = "")
end
def render
to_json
end
end
struct CreateUserEndpoint
include Azu::Endpoint(CreateUserRequest, UserResponse)
def call : UserResponse
UserResponse.new(create_user_request.name)
end
end
app = Azu::App.new
app.routes do
scope "/api" do
post "/users", CreateUserEndpoint
end
end
info = Azu::OpenAPI::Info.new("Shop API", "1.0.0")
document = Azu::OpenAPI.document(app, info)
puts document.to_json(pretty: true)
# Serve it at GET /openapi.json, and a reference page at GET /docs
handler = Azu::OpenAPI::Handler.new(app, info: info, docs: "/docs")
server = app.server([*app.default_handlers, handler] of HTTP::Handler)| Signature | Returns | Notes |
|---|---|---|
Azu::OpenAPI.document(app : App, info : Info, servers : Array(Server) = [] of Server) |
Document |
A snapshot of the routes app has when it is called, mounted apps included |
Document#to_json(pretty : Bool = false) |
String |
Keys sorted, so the same app yields the same bytes; pretty indents by two spaces |
Document#to_h |
OpenAPI::Map |
The document's own map, not a copy: a change to it shows in to_json |
Document.new(root : Map) |
Document |
A document around a map |
Azu::OpenAPI.encode(value : Value, pretty : Bool = false) |
String |
JSON with sorted keys, for any Value |
Azu::OpenAPI::Info.new(title : String, version : String, description : String? = nil) |
Info |
The info object |
Azu::OpenAPI::Server.new(url : String, description : String? = nil) |
Server |
One entry of servers; none are written when the array is empty |
Azu::OpenAPI::Map is Hash(String, Value), and Azu::OpenAPI::Value is
String | Int64 | Float64 | Bool | Nil | Array(Value) | Hash(String, Value).
Azu::OpenAPI::Handler.new(app, *, info, path = "/openapi.json", servers = [] of Server, docs = nil):
| Parameter | Type | Default | Meaning |
|---|---|---|---|
app |
App |
The app the document describes | |
info |
Info |
The info object |
|
path |
String |
"/openapi.json" |
Where the document is served; a trailing slash is dropped, and "/" serves it at the root |
servers |
Array(Server) |
[] of Server |
The servers entries |
docs |
String? |
nil |
Where the HTML reference page is served (off when nil); a trailing slash is dropped. The same path as path raises ArgumentError |
- Paths. One operation per route that leads to an
Azu::Endpoint.:idand*pathbecome{id}and{path}and are declared as required path parameters, typed from the request field of the same name (a string when there is none). Routes insidescopecarry the scope prefix. Apps added withmountare included below their mount path, at any depth. TheHEADroute registered with aGETis not listed again; anoptionsroute you declare is listed as anoptionsoperation. operationIdis the endpoint's type name with::written as.. If one endpoint is routed more than once, later ids get a number suffix.tagshold the namespace the endpoint is declared in:Shop::Users::ShowEndpointis taggedUsers.- Request fields. For
GET,DELETE,HEAD,OPTIONSandTRACEthe fields of the request that are not in the path are query parameters. ForPOST,PUTandPATCHthey are arequestBodyoffered asapplication/jsonand asapplication/x-www-form-urlencoded, because Azu parses both. The two differ where Azu's parsing does. A JSON body is read alone: the path and the query string are not added to it (they only replace a key the body already has, see Params), so a field named like a path parameter is also in the JSON schema (and required there if the request requires it) as well as being declared as a path parameter. A form is read together with the path and the query string, so the form schema leaves path-named fields out. The keys follow the parser:@[JSON::Field(key:)]renames a field in the JSON body only; query parameters and form fields always use the variable name. - Response. If the response type includes
JSON::Serializablethe success response carries its schema asapplication/json;Azu::Response::Json(T)is described byT(whenTisJSON::Serializable), since that is its body. The success response is200unless the endpoint declaressuccess_status 201(or204, which has no content): the endpoint still sets the status itself withstatusincall; the declaration only tells the document. AResponse::Negotiablelists every media type it renders, with the schema underapplication/jsonand{"type": "string"}under the others. - Security and route metadata. Operations carry any
summaryanddescriptiondeclared on the endpoint or route (RouteOptions). Whenconfig.authis enabled or a route has an effective access requirement,components.securitySchemesincludes the HTTP bearer security scheme (config.auth.security_scheme_name,"bearerAuth"by default, with optionalbearerFormatsuch as"JWT"), protected operations emitsecurity: [{"bearerAuth": [...]}], andpublic: trueroutes emitsecurity: []. Whenconfig.auth.document_requirements_in_description?istrue, the operation description also includes a human-readable summary of required roles and scopes. - Errors. An operation with request fields adds
400and422; one with a path parameter adds404; a protected operation adds401and403; a route with a body size limit adds413; a route with content-type restrictions adds415. They reference sharedcomponents:Azu.ValidationProblemis the RFC 7807 problem body (listed asapplication/problem+jsonandapplication/json) thatResponse::ValidationErrorwrites, andAzu.Erroris the problem body (listed asapplication/problem+jsonandapplication/json) the otherResponse::Errorclasses write for a client that accepts JSON (type,title,status,detail,errors,error_id, andinstancewhen the error knows the path). The document follows theerror_formatthe app has when it is built: withEnvelope(config.api_defaults!) the error responses areapplication/jsonreferencingAzu.ErrorEnvelopeandAzu.ValidationErrorEnvelope; withLegacythey referenceAzu.LegacyValidationErrorandAzu.LegacyError(the PascalCase bodies,Statusbeing the underscoredHTTP::Statusmember name). - Components. Every
JSON::Serializabletype becomes a named schema undercomponents.schemas, named after its type (Shop::AddressisShop.Address; a genericPage(Shop::User)isPage.Shop.User, since component names allow only letters, digits,.,-and_) and referenced with$ref. Recursive types work.operationIduses the same naming.
| Crystal | JSON Schema |
|---|---|
String |
string |
Int32, Int64, other integers |
integer with format int32 / int64 / none |
Float32, Float64 |
number with format float / double |
Bool |
boolean |
Time |
string, date-time |
UUID |
string, uuid |
enum |
string with enum of the members as to_json writes them (underscored) |
Array(T), Set(T) |
array with items |
Hash(String, T) |
object with additionalProperties |
T? |
the schema of T, and the field is not required |
A | B |
anyOf |
JSON::Serializable struct or class |
$ref to a component; @[JSON::Field(key:, ignore:)] is honoured |
| anything else | {} (allows anything) |
A field is required unless its type is nilable or it has a default value, or a presence or required
validation says it must be there.
The options of validate are read from the request type:
validate option |
Keyword |
|---|---|
presence: true |
required, and minLength / minItems 1 |
required: true |
required |
size: 3..8, size: 6 |
minLength / maxLength (minItems / maxItems for arrays) |
gt, gte, lt, lte |
exclusiveMinimum, minimum, exclusiveMaximum, maximum |
eq: v |
const |
in: [..], in: 1..5 |
enum, or minimum / maximum |
exclude: [..] |
not: {enum: [..]} |
match: /re/ |
pattern |
Custom validators added with Schema::Validation.use are code and cannot be described, and the
validations of a generic type (Page(T)) are not read.
A match pattern is written only when it means the same in ECMA-262, the dialect JSON Schema uses:
\A becomes ^ and \z and \Z become $. A pattern with options (/i, /m, /x), named groups,
inline flags or other group extensions, POSIX classes ([[:alpha:]]) or Crystal-only escapes (\h,
\R, \p{..} and the like) is left out rather than written wrongly. Crystal's ^ and $ also match at
line breaks, which an ECMA-262 pattern without the m flag does not, so a multi-line value can differ.
Azu::OpenAPI::Handler is an opt-in HTTP::Handler like Handler::HealthCheck: it answers GET and HEAD at its
path with application/json and a Content-Length (no body for HEAD), and passes every other request on. The
document is built by the first request and kept, so routes added after that do not show up: build a new handler to
pick them up. It describes the App it is given, nothing else.
With docs: "/docs" it also answers GET and HEAD at /docs with an HTML page of the same document: every
operation with its parameters, request bodies and responses, then the shared schemas, linked from where they are used
(the shared error responses, BadRequest, NotFound and ValidationFailed, are named, not expanded).
The page is rendered on the server, so it works offline and has no script and nothing loaded from elsewhere; every
value is HTML-escaped. It carries its own Content-Security-Policy (default-src 'none' and the hash of its inline
style), which replaces the app's policy for that path only. It is a reference to read, not a console: to send requests
from the browser, use Swagger UI (see Publish your OpenAPI document).
{% hint style="warning" %}
Pass the handler after the default ones, as above: a server built from [handler] alone has no request id, error
pages or security headers.
{% endhint %}
The document is a map of your application: every route, the names of its parameters, the validation rules and the mounted apps. Treat it like source code you would show to a client.
- Add the handler only where you mean to publish it, for example behind
config.env.development?or a setting, not unconditionally in production. - Put it after your authentication and authorisation handlers when the API is not public.
- A Swagger UI served from another origin needs CORS (
Azu::Handler::CORS) on the document's path.
- Not generated: parameter descriptions, examples, and a success status other than
200(an endpoint's status is set while it runs, so the document cannot know about a201or204). Azu cannot read them from the code; add them by editingto_h(see Publish your OpenAPI document). - JSON only. There is no YAML writer (
JSON.parse(document.to_json).to_yaml, withrequire "yaml", converts it and keeps the key order). Thedocs:page is read-only; for "try it out", point Swagger UI, Redoc or any other tool at the URL. - A response that is not
JSON::Serializablehas no schema, and neither has a request that is the bareAzu::Request: nothing says what they carry. Wrong or missing is better than invented. - A
renderthat does not write the type's ownto_jsonmakes the document wrong; Azu cannot check it. - Hand written
RoutableandHTTP::Handlerroutes have no operation and are left out. So are WebSocket channels. - Query parameters of
POST,PUTandPATCHare not described. Azu reads them only for form bodies (merged into the form), not for JSON ones. @[JSON::Field(ignore:)]and@[URI::Params::Field(ignore:)]leave a field out. A field withconverter:(or JSONroot:) is described as{}, since its type is not what it holds.emit_nullandignore_serializeare not considered.- Nested objects in query parameters and forms use the schema of the JSON object; how a client encodes
them (
a[b]=1) is up to it. - An explicit JSON
nullfor a nilable field is accepted by Azu but not advertised in the schema. - The routes are read when the document is built. Endpoints declared with the verb macros (
get "/path"inside an endpoint) are registered onAzu::App.default, so pass that app.
{% hint style="info" %}
azu openapi:export is a different generator. The command of the Azu CLI
(--output, --format yaml|json, --project, --version) does not compile the app: it reads
src/endpoints/**/*_endpoint.cr with regular expressions. It finds verb macros in structs whose name ends in
Endpoint, so routes declared in routes, scope or mount are missing; it keeps :id in paths instead of
{id}, writes a fixed http://localhost:3000 server and reads no validations. Azu::OpenAPI describes the routes
the app has.
{% endhint %}