Class OpenApi
RouteDoc descriptions.
var openApi = OpenApi.builder("Notes API", "1.0.0")
.securityScheme("bearer", SecurityScheme.bearer("JWT"))
.build();
// after registering every other route:
openApi.serve(app, "/openapi.json");
Every route appears with its method and path; metadata adds to it. Output is deterministic:
paths, methods, response codes and schema names are sorted, record properties keep their
declaration order and JavaBean properties are sorted by name, so equal inputs produce equal
bytes. Nothing is derived from handler code, and no class or package name appears in the output
unless it is the simple name of a described record, bean or enum (rename it with OpenApi.Builder.schemaName(java.lang.Class<?>, java.lang.String)).
Generation fails with IllegalArgumentException, naming the route or property, for a
type it cannot describe, a duplicated operation id, an unknown security scheme, a documented path
parameter the route does not have, or a method OpenAPI cannot express. It happens when the
document is rendered, which serve(com.jsgalactic.axiom.application.Application, java.lang.String, com.jsgalactic.axiom.context.Middleware...) does immediately, so a wrong description stops startup
instead of failing a request.
Instances are immutable and thread-safe. The document describes the application's API surface; whether to expose it publicly is the application's decision (see the guide).
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic OpenApi.BuilderStarts a configuration.render(Application app) Renders the OpenAPI 3.1 document of the application's current routes.renderSwagger(Application app) Renders the Swagger 2.0 document of the application's current routes.serve(Application app, String path, Middleware... middleware) Renders the OpenAPI 3.1 document now and registers a GET route that serves it.serveSwagger(Application app, String path, Middleware... middleware) Likeserve(com.jsgalactic.axiom.application.Application, java.lang.String, com.jsgalactic.axiom.context.Middleware...)for the Swagger 2.0 document.
-
Method Details
-
builder
Starts a configuration.- Parameters:
title- API titleversion- API version (the application's, not the OpenAPI version)- Returns:
- a builder
-
render
Renders the OpenAPI 3.1 document of the application's current routes.- Parameters:
app- application whose routes and descriptions are read; may be configuring or running- Returns:
- the JSON document
- Throws:
IllegalArgumentException- if a route or type cannot be described
-
renderSwagger
Renders the Swagger 2.0 document of the application's current routes. It carries the same information in the older format, with its constraints: a single server (the first one), one body schema per operation, and bearer schemes expressed as an API key header.- Parameters:
app- application whose routes and descriptions are read- Returns:
- the JSON document
- Throws:
IllegalArgumentException- if a route or type cannot be described
-
serve
Renders the OpenAPI 3.1 document now and registers a GET route that serves it. Call it after every other route is registered; the document is a snapshot, cached for the life of the application, with a strongETag(a matchingIf-None-Matchgets 304). The serving route itself is not part of the document. Nothing is served unless this is called.- Parameters:
app- application to document and register on, still configuringpath- route path, for example/openapi.jsonmiddleware- route middleware, for example an authentication policy- Returns:
- the registered route
- Throws:
IllegalArgumentException- if the document cannot be generated
-
serveSwagger
Likeserve(com.jsgalactic.axiom.application.Application, java.lang.String, com.jsgalactic.axiom.context.Middleware...)for the Swagger 2.0 document.- Parameters:
app- application to document and register on, still configuringpath- route path, for example/swagger.jsonmiddleware- route middleware- Returns:
- the registered route
- Throws:
IllegalArgumentException- if the document cannot be generated
-