Skip to main content

OpenAPI Spec Builder

Python library used to generate swagger docs from decorators. Doesn't screw with your requests, doesn't alter your middleware, doesn't put its dirty little hands where they don't belong. You decorate functions, register them on a schema, and generate a swagger doc.

Install

Use PyPI -> oapispec @ https://pypi.org/project/oapispec

Getting Started

In this very simplified example the spec resulted by generating the schema is a valid swagger dict/json spec that can be used in a swagger ui.

from http import HTTPStatus

import oapispec as oapi

schema = oapi.schema(metadata=dict(
    version='4.2.0',
    title='Super API'
))

@oapi.doc.namespace('Health Check')
@oapi.doc.route('/ping')
@oapi.doc.method('GET')
def ping():
    pass

spec = schema.register(ping).generate()

where spec equals vvv below vvv. Using oapispec you can add many more details to your spec.

{
    "swagger": "2.0",
    "basePath": "/",
    "paths": {
        "/ping": {
            "get": {
                "responses": {},
                "operationId": "ping",
                "tags": [
                    "Health Check"
                ]
            }
        }
    },
    "info": {
        "title": "Super API",
        "version": "4.2.0"
    },
    "produces": [
        "application/json"
    ],
    "consumes": [
        "application/json"
    ],
    "tags": [
        {
            "name": "Health Check"
        }
    ]
}

Creating Models

In this example we create a model and use it as an expected parameter to a POST request.

book_model = oapi.model.Model('Book', {
    'title': oapi.fields.string(required=True),
    'author': oapi.fields.string(required=True),
    'genre': oapi.fields.string(),
    'edition': oapi.fields.integer(),
    'isInPrint': oapi.fields.boolean()
})

@oapi.doc.namespace('Book')
@oapi.doc.route('/book')
@oapi.doc.method('POST')
@oapi.doc.response(HTTPStatus.CREATED.value, HTTPStatus.CREATED.description, book_model)
@oapi.doc.expect(book_model)
def add_book():
    pass

spec = schema.register(add_book).generate()

Futher Examples

The best place to look is the end_to_end test in tests/end_to_end_test.py. This is always kept up to date as a strong example and test of what is possible. Note that you can see the expected output of a generated schema in tests/assets/expected_full_schema_result.json. This can give you an idea of how the doc decorators work - both on their own and together - to produce the open api spec.

Contributions & Issues

Both are welcome and encouraged! For any problems your having add an issue in github. If your interested in contributing take a look at the contributing doc. If your interested in contributing you will probably want to know how to run/test/modify the project locally so checkout the developing doc

Release files for oapispec-v2 0.0.5

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

Source distribution (sdist)

Source distribution for oapispec-v2 0.0.5
File Size Uploaded
oapispec-v2-0.0.5.tar.gz 13.6 kB Details

Release files / oapispec-v2-0.0.5.tar.gz

Download URL oapispec-v2-0.0.5.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1487eebbeeb801af0703a9387be2825dccb3efddd330ff26220913df35ac2218
BLAKE2b-256 checksum
How to use checksums
fedc0f6f1aece6da4e16a0343473dbf75604a8102f60c203717b5924f6fd0645
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.7

Release history Release notifications | RSS feed

This release

0.0.5 This release

1 release file

0.0.4

1 release file

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