Framework-agnostic OpenAPI 3.0 specification models and render plugins
Project description
OpenAPI Spec Models
[!Note] This code was shamelessly stolen from Litestar, and Claude Code did the work to make it framework-agnostic.
Framework-agnostic OpenAPI 3.0 specification models and render plugins for Python.
Features
- Complete OpenAPI 3.0 Spec Models: Full dataclass-based implementation of the OpenAPI 3.0 specification
- Multiple Documentation UIs: Built-in render plugins for Swagger UI, Redoc, Scalar, RapiDoc, and Stoplight Elements
- Framework-Agnostic: Works with any Python web framework (Django, Flask, FastAPI, Litestar, etc.)
- Type-Safe: Full type annotations with mypy support
- Lightweight: Minimal dependencies (only
typing-extensionsrequired) - Zero Runtime Overhead: Pure Python dataclasses with no heavy dependencies
Installation
pip install openapi-spec-models
For YAML support:
pip install openapi-spec-models[yaml]
Quick Start
Creating an OpenAPI Specification
from openapi_spec_models import OpenAPI, Info, PathItem, Operation, OpenAPIResponse
# Create an OpenAPI spec
spec = OpenAPI(
openapi="3.0.3",
info=Info(
title="My API",
version="1.0.0",
description="A sample API"
),
paths={
"/users": PathItem(
get=Operation(
summary="List users",
responses={
"200": OpenAPIResponse(description="Success")
}
)
)
}
)
# Convert to dictionary
spec_dict = spec.to_schema()
Using Render Plugins
The package includes several render plugins for generating beautiful API documentation:
from openapi_spec_models.plugins import (
SwaggerRenderPlugin,
RedocRenderPlugin,
ScalarRenderPlugin,
)
# Create plugins
swagger = SwaggerRenderPlugin(path="/swagger")
redoc = RedocRenderPlugin(path="/redoc")
scalar = ScalarRenderPlugin(path="/scalar")
# Render documentation
html = swagger.render(spec_dict)
Available Render Plugins
- JsonRenderPlugin: Renders the spec as JSON
- YamlRenderPlugin: Renders the spec as YAML (requires
pyyaml) - SwaggerRenderPlugin: Interactive Swagger UI
- RedocRenderPlugin: Redoc documentation UI
- ScalarRenderPlugin: Modern Scalar API documentation
- RapidocRenderPlugin: RapiDoc UI
- StoplightRenderPlugin: Stoplight Elements UI
OpenAPI Spec Models
The package provides complete dataclass models for the OpenAPI 3.0 specification:
OpenAPI: Root OpenAPI documentInfo,Contact,License: API metadataServer,ServerVariable: Server configurationPathItem,Operation: Endpoint definitionsParameter,RequestBody,OpenAPIResponse: Request/response modelsSchema: JSON Schema for data typesComponents: Reusable componentsSecurityScheme,SecurityRequirement: Security definitions- And many more...
Framework Integration
This package is framework-agnostic. Here's how you might integrate it with different frameworks:
Django
from django.http import HttpResponse
from openapi_spec_models.plugins import SwaggerRenderPlugin
plugin = SwaggerRenderPlugin(path="/swagger")
def swagger_view(request):
# Generate your OpenAPI spec dict
openapi_spec_dict = {...}
html = plugin.render(openapi_spec_dict)
return HttpResponse(html, content_type=plugin.media_type)
Flask
from flask import Response
from openapi_spec_models.plugins import RedocRenderPlugin
plugin = RedocRenderPlugin(path="/redoc")
@app.route("/redoc")
def redoc():
# Generate your OpenAPI spec dict
openapi_spec_dict = {...}
html = plugin.render(openapi_spec_dict)
return Response(html, mimetype=plugin.media_type)
FastAPI
While FastAPI has its own OpenAPI implementation, you can use these models for advanced customization:
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from openapi_spec_models.plugins import ScalarRenderPlugin
plugin = ScalarRenderPlugin(path="/scalar")
@app.get("/scalar", response_class=HTMLResponse)
async def scalar():
spec = app.openapi()
html = plugin.render(spec)
return html
Development
# Clone the repository
git clone https://github.com/yourusername/openapi-spec-models.git
cd openapi-spec-models
# Install with uv
uv sync
# Run tests
just test
# Run tests with coverage
just test-cov
# Format code
just format
# Check code quality
just check
Available Commands (using just)
This project uses just for task automation. Here are the available commands:
Development:
just install- Install package in development modejust install-dev- Install development dependenciesjust test- Run testsjust test-cov- Run tests with coveragejust format- Format code with black and isortjust format-check- Check code formattingjust typecheck- Run mypy type checkingjust check- Run all checks (format, typecheck, tests)
Building:
just build- Build distribution packagesjust clean- Clean build artifacts
Version Management:
just version-get- Display current versionjust version-bump-patch- Bump patch version (0.1.0 → 0.1.1)just version-bump-minor- Bump minor version (0.1.0 → 0.2.0)just version-bump-major- Bump major version (0.1.0 → 1.0.0)
Releasing:
just release-patch- Bump patch version, commit, and create tagjust release-minor- Bump minor version, commit, and create tagjust release-major- Bump major version, commit, and create tag
Release Process
This package uses PyPI Trusted Publishers for automated releases:
-
Create a release using just:
just release-patch # or release-minor, release-major
-
Push the commit and tag:
git push origin main git push origin v0.1.1 # replace with your version
-
GitHub Actions will automatically build and publish to PyPI when the tag is pushed.
First-time setup: Configure the PyPI Trusted Publisher in your PyPI project settings:
- Publisher: GitHub
- Owner:
yourusername - Repository:
openapi-spec-models - Workflow:
publish.yml - Environment:
pypi
Credits
This package is derived from and inspired by Litestar's OpenAPI implementation, adapted to be framework-agnostic.
License
MIT License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openapi_spec_models-0.1.1.tar.gz.
File metadata
- Download URL: openapi_spec_models-0.1.1.tar.gz
- Upload date:
- Size: 30.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c8a98c5deaf8a927d14c72ef4cafbbdad06b859dac1cafeb257369436eb314d
|
|
| MD5 |
af3ea8643c4c7bd7fd0495fc986619d6
|
|
| BLAKE2b-256 |
819a95843cd0e2caf597a451cf6bc3cd2b855690183065cf325f8023ff0e7115
|
Provenance
The following attestation bundles were made for openapi_spec_models-0.1.1.tar.gz:
Publisher:
publish.yml on Tobi-De/openapi-spec-models
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openapi_spec_models-0.1.1.tar.gz -
Subject digest:
5c8a98c5deaf8a927d14c72ef4cafbbdad06b859dac1cafeb257369436eb314d - Sigstore transparency entry: 787920159
- Sigstore integration time:
-
Permalink:
Tobi-De/openapi-spec-models@9929e0c69fb4f97668e1b9e025c6cc5e904a35ce -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/Tobi-De
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9929e0c69fb4f97668e1b9e025c6cc5e904a35ce -
Trigger Event:
push
-
Statement type:
File details
Details for the file openapi_spec_models-0.1.1-py3-none-any.whl.
File metadata
- Download URL: openapi_spec_models-0.1.1-py3-none-any.whl
- Upload date:
- Size: 48.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb59ab02ae6bc29d631df04a1c686f35f44637229026ec263f9a505f6b5a990a
|
|
| MD5 |
2911d4fe240c027dd79bb67594ebe39f
|
|
| BLAKE2b-256 |
89bf8a653c061c970ff7d6adc58df93293f8b2414a47b86ab77818adbd51f8ed
|
Provenance
The following attestation bundles were made for openapi_spec_models-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on Tobi-De/openapi-spec-models
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openapi_spec_models-0.1.1-py3-none-any.whl -
Subject digest:
bb59ab02ae6bc29d631df04a1c686f35f44637229026ec263f9a505f6b5a990a - Sigstore transparency entry: 787920165
- Sigstore integration time:
-
Permalink:
Tobi-De/openapi-spec-models@9929e0c69fb4f97668e1b9e025c6cc5e904a35ce -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/Tobi-De
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9929e0c69fb4f97668e1b9e025c6cc5e904a35ce -
Trigger Event:
push
-
Statement type: