Flask-X-OpenAPI-Schema
A powerful utility for automatically generating OpenAPI schemas support Flask(MethodView) and Flask-RESTful(Resource) applications and Pydantic models to simplify API documentation with minimal effort.
📚 Documentation
Full documentation is available in the docs directory and online at https://straydragon.github.io/flask-x-openapi-schema/.
📝 Examples
Complete examples are available in the examples/ directory. These examples demonstrate all the features of the library, including:
- Parameter binding (path, query, body)
- File uploads (images, documents, audio, video)
- Internationalization
- Response models
- OpenAPI schema generation
🚀 Quick Start
# Install the package
uv pip install flask-x-openapi-schema
# With Flask-RESTful support
uv pip install flask-x-openapi-schema[flask-restful]
✨ Features
- Framework Support: Works with both Flask and Flask-RESTful applications
- Auto-Generation: Generate OpenAPI schemas from Flask-RESTful resources and Flask.MethodView classes
- Pydantic Integration: Seamlessly convert Pydantic models to OpenAPI schemas
- Smart Parameter Handling: Inject request parameters from Pydantic models with configurable prefixes
- Type Safety: Preserve type annotations for better IDE support and validation
- Multiple Formats: Output schemas in YAML or JSON format
- Internationalization: Built-in i18n support for API documentation with thread-safe language switching
- File Upload Support: Simplified handling of file uploads with validation
- Flexible Architecture: Modular design with framework-specific implementations
- Performance Optimized: Caching of static information for improved performance
🚀 Performance
Flask-X-OpenAPI-Schema is designed with performance in mind. Our benchmarks show minimal overhead when using the library compared to standard Flask and Flask-RESTful applications.
Benchmark Results
| Framework | Endpoint | Requests | Success Rate | Median (ms) | 95%ile (ms) | Avg (ms) | RPS | Overhead |
|---|---|---|---|---|---|---|---|---|
| Flask | Standard | 7930 | 100.00% | 270.00 | 270.00 | 306.01 | 132.18 | baseline |
| Flask | OpenAPI | 7797 | 100.00% | 280.00 | 280.00 | 312.85 | 129.96 | +2.23% |
| Flask-RESTful | Standard | 7654 | 100.00% | 310.00 | 310.00 | 348.35 | 127.30 | baseline |
| Flask-RESTful | OpenAPI | 7603 | 99.68% | 310.00 | 310.00 | 358.92 | 126.45 | +3.03% |
These benchmarks were conducted using Locust with 200 concurrent users and a ramp-up rate of 20 users per second over a 60-second test period. The tests measured the performance of identical endpoints implemented with and without Flask-X-OpenAPI-Schema.
For more detailed benchmarks and to run your own performance tests, see the benchmarks directory.
📦 Installation
Development Setup
This project uses uv for package management and ruff for linting and formatting and need just for project management:
# Install all dependencies including development ones
just sync-all-deps
# Format and lint code
just format-and-lintfix
🛠️ Basic Usage
See the Usage Guide for more detailed examples.
Flask.MethodView Example
(diy) see and run example
Flask-RESTful Example
(diy) see and run example
📋 Key Features
Framework-Specific Implementations
The library provides separate implementations for Flask and Flask-RESTful:
# For Flask.MethodView
from flask_x_openapi_schema.x.flask import openapi_metadata
# For Flask-RESTful
from flask_x_openapi_schema.x.flask_restful import openapi_metadata
Parameter Binding with Special Prefixes
The library binds parameters with special prefixes default, and can custom by yourself:
_x_body: Request body from JSON_x_query: Query parameters_x_path_<param_name>: Path parameters_x_file: File uploads
Custom Parameter Prefixes
from flask_x_openapi_schema import ConventionalPrefixConfig, configure_prefixes
# Create a custom configuration
custom_config = ConventionalPrefixConfig(
request_body_prefix="req_body",
request_query_prefix="req_query",
request_path_prefix="req_path",
request_file_prefix="req_file"
)
# Configure globally
configure_prefixes(custom_config)
# Or per-function
@openapi_metadata(
summary="Test endpoint",
prefix_config=custom_config
)
def my_function(req_body: MyModel, req_query: QueryModel):
# Use custom prefixes
return {"message": "Success"}
I18n Support
from flask_x_openapi_schema import I18nStr, set_current_language
# Set the current language
set_current_language("zh-Hans")
@openapi_metadata(
summary=I18nStr({
"en-US": "Get an item",
"zh-Hans": "获取一个项目",
"ja-JP": "アイテムを取得する"
}),
...
)
def get(self, item_id):
# ...
File Upload Support
from flask_x_openapi_schema import ImageUploadModel
@openapi_metadata(
summary="Upload an image"
)
def post(self, _x_file: ImageUploadModel):
# File is automatically injected and validated
return {"filename": _x_file.file.filename}
Response Models
from flask_x_openapi_schema import BaseRespModel
from pydantic import Field
class ItemResponse(BaseRespModel):
id: str = Field(..., description="Item ID")
name: str = Field(..., description="Item name")
price: float = Field(..., description="Item price")
# Will be automatically converted to a Flask response
# return ItemResponse(id="123", name="Example", price=10.99), 200
🧪 Testing and Coverage
This project uses pytest for testing and pytest-cov for coverage reporting:
# Run tests with coverage report
just test
# Run tests in parallel for faster execution
just test-parallel
# Run tests in parallel with specific number of workers
just test-parallel '' 4
# View HTML coverage report
# Open htmlcov/index.html in your browser
See Testing Guide for more details on testing.
📊 Benchmarking
The project includes benchmarking tools to measure performance:
# Run benchmarks and generate report
just benchmark
📖 More Docs
🧩 Components
- Core: Base functionality shared across all implementations
- Schema Generator: Converts resources to OpenAPI schemas
- Configuration: Configurable parameter prefixes and settings
- Cache: Performance optimization for schema generation
- Framework-Specific:
- Flask: Support for Flask.MethodView classes
- Flask-RESTful: Support for Flask-RESTful resources
- Models:
- Base Models: Type-safe response handling
- File Models: Simplified file upload handling
- Internationalization:
- I18nStr: Multilingual string support
- Language Management: Thread-safe language switching
- Utilities: Helper functions for schema creation and manipulation
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for flask-x-openapi-schema 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flask_x_openapi_schema-0.2.0.tar.gz | 75.0 kB | With provenance | Details for flask_x_openapi_schema-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flask_x_openapi_schema-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for flask_x_openapi_schema-0.2.0-py3-none-any.whl |
Total release size: 169.6 kB
Release files / flask_x_openapi_schema-0.2.0.tar.gz
| Download URL | flask_x_openapi_schema-0.2.0.tar.gz |
|---|---|
| Size | 75.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
84089a73f3c239063ec153e8dc4ab2d8c98795d2dbcf81ad3901b2f3070f5f6b
|
|
BLAKE2b-256 checksum How to use checksums |
b45c3de9e7a57620c641f1401718f07ecd48d633c9c1276442d8cf7e211fa4d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 6, 2025.
Transparency logRelease files / flask_x_openapi_schema-0.2.0-py3-none-any.whl
| Download URL | flask_x_openapi_schema-0.2.0-py3-none-any.whl |
|---|---|
| Size | 94.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ada309f8e8342c6150514246df9354286fa940f10bedecb6a9fe5014282e8f24
|
|
BLAKE2b-256 checksum How to use checksums |
9f643cac0599cb51d71f348d2b55ae9b7d1a65327c4898e230cf4586ec0b4ec3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 6, 2025.
Transparency logSecurity for flask-x-openapi-schema 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for flask_x_openapi_schema-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 6, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.