liveBook · Manning

OpenAPI specification

As explained in Build Financial Software with Generative AI (From Scratch)

An OpenAPI definition is a machine-readable description of an HTTP API's available operations, inputs, outputs, and metadata.

An OpenAPI specification is a machine-readable description of an HTTP API: its operations, inputs, outputs, and related metadata. It turns an API from an undocumented backend into something other developers can inspect, understand, and use. Interactive documentation built from the specification can also help keep the published contract aligned with the implementation. section 4.5 section 4.5.3

In an API-driven design, the specification supports more than a reference page. It gives consumers a clear view of available endpoints while supporting client-code generation and the beginnings of server-side request-handling code. This is especially useful when an API must be discoverable by teams that did not build it. section 4.5 section 4.10

A useful OpenAPI-backed API experience can include:

  • Interactive documentation where users can view and try API operations. section 4.5.1
  • Request and response examples that clarify how an endpoint should be called. section 4.5.3
  • Contact and licensing information, plus selectable test, sandbox, or production servers. section 4.5.3
  • Generated client code for consumers and server-side code scaffolding for request handlers. section 4.5 section 4.5.3

FastAPI can generate OpenAPI documentation from decorated functions, reducing the risk that a separate document becomes stale as code changes. Its generated documentation can be visited at a documentation endpoint, while the underlying JSON definition can be opened directly, saved, or edited in Swagger Editor. JSON can be converted to YAML for easier editing, but conversion is optional. section 4.5.1 section 4.5.3

Resolve a version compatibility problem

A generated definition reported OpenAPI 3.1.0, but a WSO2 Docker image and Swagger Editor expected a 3.0.x version and rejected it during validation. Rather than immediately changing every tool, customize FastAPI's OpenAPI generation to report version 3.0.0, rebuild the container, and confirm the JSON before importing it. This illustrates a practical rule: validate a specification against the consumers that must accept it, and do not assume tool support is identical across versions. section 4.6.3 section 4.5.1

Listing 4.12 Adjusting the OpenAPI version
from fastapi.openapi.utils import get_openapi #1
…  #2
def custom_openapi(): #2
    if app.openapi_schema: 
        return app.openapi_schema
    openapi_schema = get_openapi(  #3
        title="File API",  #3
        version="0.0.1",  #3
        routes=app.routes,  #3
        openapi_version="3.0.0" #4
    )
    app.openapi_schema = openapi_schema
    return app.openapi_schema

app.openapi = custom_openapi

The customization replaces FastAPI's default schema generation with a function that explicitly sets the reported OpenAPI version to 3.0.0 before caching the schema. section 4.6.3

An OpenAPI definition can also move an API into an API-management workflow. Import the definition from its URL, validate it, name and version the managed API, configure its backend endpoint, deploy it, generate a key, and try the operations through the manager. The manager then becomes the consumer-facing entry point rather than exposing the backend directly. section 4.6.4 section 4.10

The specification does not replace API design decisions. Clear use cases, versioning plans, server-side validation, scalability, monitoring, and current documentation remain necessary. OpenAPI helps express and publish the API contract; the API still needs stable behavior and careful operational management behind that contract. section 4.1 section 4.10

How Build Financial Software with Generative AI (From Scratch) covers it

How deep it goes
Explains generation, interactive inspection, metadata, code generation, version overrides, and manager import; defers comprehensive API-design treatment.
Examples
Uses FastAPI-generated openapi.json, Swagger Editor, a 3.1.0-to-3.0.0 compatibility fix, and WSO2 API import and deployment.
What it assumes
Introduces OpenAPI through a running FastAPI API and explains documentation tooling without requiring prior OpenAPI experience.
What you can do after
Operate/configure it by inspecting generated definitions, adjusting a reported version, and importing an API into WSO2.
How it connects
Connects OpenAPI to API-driven design, FastAPI, Swagger, Redoc, WSO2, RESTful APIs, versioning, and API management.

Also explained in

Compare how 12 books explain OpenAPI specification →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Financial Software with Generative AI (From Scratch). The section links point to the parts of the book it draws on.