OpenAPI Schema Pydantic

1.2.4 · maintenance · verified Sun Apr 12

openapi-schema-pydantic provides Pydantic classes that represent the OpenAPI (v3) specification schema. It allows developers to construct OpenAPI documents using native Python objects with type hints and validation. The library's latest version is 1.2.4. Note: This library is no longer actively maintained, and `openapi-pydantic` is a community-maintained fork.

Warnings

Install

Imports

Quickstart

This example demonstrates how to construct a basic OpenAPI document using the library's Pydantic models. It creates an `OpenAPI` object with an `Info` section and a simple `/ping` endpoint. The output is then serialized to JSON.

from openapi_schema_pydantic import OpenAPI, Info, PathItem, Operation, Response

# Construct OpenAPI by pydantic objects
open_api = OpenAPI(
    info=Info(
        title='My own API',
        version='v0.0.1',
    ),
    paths={
        '/ping': PathItem(
            get=Operation(
                responses={
                    '200': Response(description='pong')
                }
            )
        )
    },
)

# For Pydantic v1.x (which this library primarily supports), use .json()
print(open_api.json(by_alias=True, exclude_none=True, indent=2))

view raw JSON →