Skip to main content

A powerful utility for automatically generating OpenAPI schemas from Flask-RESTful resources, Flask.MethodView classes, and Pydantic models.

Project description

Flask-X-OpenAPI-Schema

Python Version from PEP 621 TOML License GitHub uv Code style: ruff CI Codecov

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.

🚀 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

📦 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

# View HTML coverage report
# Open htmlcov/index.html in your browser

📊 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_x_openapi_schema-0.1.1.tar.gz (55.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_x_openapi_schema-0.1.1-py3-none-any.whl (72.2 kB view details)

Uploaded Python 3

File details

Details for the file flask_x_openapi_schema-0.1.1.tar.gz.

File metadata

  • Download URL: flask_x_openapi_schema-0.1.1.tar.gz
  • Upload date:
  • Size: 55.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for flask_x_openapi_schema-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bba430c1d781cc5a82950a35ad7bdfce79b7b604c11bf96909c6924cfabc4d01
MD5 a9e67e3c2a7030ce3be7f9e1b143e6f9
BLAKE2b-256 8a2c3d72e35e65b1ba93aecb81766f3d5f27dc4b274910ce707ecd0d167cce7b

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_x_openapi_schema-0.1.1.tar.gz:

Publisher: cd.yaml on StrayDragon/flask-x-openapi-schema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flask_x_openapi_schema-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_x_openapi_schema-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a4b0a4e6774f45cc35224d22280b55bcdd1ca995677a23a25a64c54676b9f7b7
MD5 b6ab43345b653696c7a2e666f8527d9e
BLAKE2b-256 30cf9c6c489366d9f0e5c44c5b30e88f7903c204b01322e755fd72b3fea6b9c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_x_openapi_schema-0.1.1-py3-none-any.whl:

Publisher: cd.yaml on StrayDragon/flask-x-openapi-schema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page