Class OpenApi

java.lang.Object
com.jsgalactic.axiom.openapi.OpenApi

public final class OpenApi extends Object
Generates an OpenAPI 3.1 document, or the equivalent Swagger 2.0 document, from an application's routes and their 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).

  • Method Details

    • builder

      public static OpenApi.Builder builder(String title, String version)
      Starts a configuration.
      Parameters:
      title - API title
      version - API version (the application's, not the OpenAPI version)
      Returns:
      a builder
    • render

      public String render(Application app)
      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

      public String renderSwagger(Application app)
      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

      public Route serve(Application app, String path, Middleware... middleware)
      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 strong ETag (a matching If-None-Match gets 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 configuring
      path - route path, for example /openapi.json
      middleware - route middleware, for example an authentication policy
      Returns:
      the registered route
      Throws:
      IllegalArgumentException - if the document cannot be generated
    • serveSwagger

      public Route serveSwagger(Application app, String path, Middleware... middleware)
      Parameters:
      app - application to document and register on, still configuring
      path - route path, for example /swagger.json
      middleware - route middleware
      Returns:
      the registered route
      Throws:
      IllegalArgumentException - if the document cannot be generated