Skip to main content

Test

openapi-specgen

Openapi-Specgen helps you generate OpenApi specification from your python code.

Includes support for automatically generating OpenApi schemas for Dataclasses and Marshamllow Schemas.

Installation

Install using pip

pip install openapi-specgen

Quick Start

Define your objects using dataclasses or marshmallow schemas. Import the required OpenApi classes and define your Api with it`s paths, params and responses.

from typing import List
from dataclasses import dataclass

@dataclass
class DataclassObject():
    str_field: str
    int_field: int
    float_field: float
    boolean_field: bool
    list_field: List[str]

from openapi_specgen import OpenApi, OpenApiParam, OpenApiPath, OpenApiResponse

sample_response = OpenApiResponse('Response description', data_type=DataclassObject)
sample_param = OpenApiParam('param_name', 'query', data_type=str)
sample_path = OpenApiPath('/api_path', 'get', [sample_response], [sample_param])

sample_api = OpenApi('Sample Api', [sample_path, marshmallow_path])

sample_api.as_dict()

Adding custom type resolvers

The following code snippet expands on the quick start example to show you how to add custom resolvers for any other types.

# Create a func with the following signature:
def custom_resolver(openapi_schema_resolver, data_type):
    # Resolver must return None if it cannot resolve the data_type
    if data_type is not CustomType:
        return

    # For simple types the schema can be returned now, e.g
    # return {"type": "string", "format": "byte"}

    # Optionally for objects we can use a reference using the following syntax

    # Register the actual component
    component_name = "CustomType"
    openapi_schema_resolver.add_component(
        component_name,
        {
            "title": component_name,
            "type": "object",
            "required": ["foo"],
            "properties": {
                # use openapi_schema_resolver.get_schema
                # to recursively resolve other schemas
                "foo": openapi_schema_resolver.get_schema(int)
            }
        }
    )

    # Return the reference
    return {'$ref': openapi_schema_resolver.get_component_ref(component_name)}

# Register your func on your OpenApi instance
test_api.add_resolver(custom_resolver)

License

This project is licensed under the MIT License - see the LICENSE file for details

Metadata

Release files for openapi-specgen 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openapi-specgen 1.0.0
File Size Uploaded
openapi-specgen-1.0.0.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openapi-specgen 1.0.0
File Interpreter ABI Platform
openapi_specgen-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.1 kB

Release files / openapi-specgen-1.0.0.tar.gz

Download URL openapi-specgen-1.0.0.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c5e2ee5ad33b729cc185fae029b7d372d3db24a197ef664f17bea754da02f669
BLAKE2b-256 checksum
How to use checksums
94d8b814e708093602848c0e22193b39a1760dedae47f67624cc8ab78b8e711e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.4

Release files / openapi_specgen-1.0.0-py3-none-any.whl

Download URL openapi_specgen-1.0.0-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
964cc56882fc3dfc0f3c7d417ae3e9e78409506bd0b8656d3e08050f378793c8
BLAKE2b-256 checksum
How to use checksums
2c0af292e27fb8ebc77d264ca17e0d8ec2865973f3d0b4ff6b4bdac83711c37c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.4
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page