Essentials OpenAPI

1.4.0 · active · verified Thu Apr 16

Essentials OpenAPI is a Python library (current version 1.4.0) designed for generating OpenAPI Documentation in both v3 and v2 formats, with output capabilities in JSON and YAML. It also provides utilities for generating other types of documents from existing OpenAPI Specification files. The project is actively maintained with a regular release cadence, often addressing bug fixes and adding support for newer OpenAPI specification versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically construct a minimal OpenAPI v3 specification using `essentials-openapi`. It defines API info, a basic path with a GET operation, and a response schema, then outputs the specification as JSON. This approach directly uses the library's Python classes to build the OpenAPI document structure.

from essentials_openapi.v3 import OpenAPI, Info, PathItem, Operation, Response, MediaType, Schema, Contact
from essentials_openapi.v3.models import Reference

# Define contact information
contact_info = Contact(name='API Support', email='support@example.com')

# Define basic API information
info = Info(
    title='My Sample API',
    version='1.0.0',
    description='A simple API to demonstrate essentials-openapi.',
    contact=contact_info
)

# Define a simple response schema
success_schema = Schema(type='object', properties={'message': Schema(type='string')})

# Define a successful response
success_response = Response(
    description='Successful operation',
    content={'application/json': MediaType(schema=Reference(ref='#/components/schemas/SuccessResponse'))}
)

# Define an operation for a GET endpoint
get_operation = Operation(
    summary='Get a greeting',
    responses={'200': success_response}
)

# Define a path item
path_item = PathItem(get=get_operation)

# Create the OpenAPI object
openapi = OpenAPI(
    info=info,
    paths={'/greet': path_item},
    components={'schemas': {'SuccessResponse': success_schema}}
)

# Generate the OpenAPI JSON output
openapi_json = openapi.to_json(indent=2)
print(openapi_json)

# To save to a file (requires PyYAML for YAML, or built-in json for JSON)
# with open('openapi.json', 'w') as f:
#     f.write(openapi_json)

view raw JSON →