Skip to main content

A library of decorators and functions for generating open api specs

Project description

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.


Use PyPI -> 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(
    title='Super API'

@oapi.doc.namespace('Health Check')
def ping():

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": [
    "consumes": [
    "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.response(HTTPStatus.CREATED.value, HTTPStatus.CREATED.description, book_model)
def add_book():

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

Futher Examples

The best place to look is the end_to_end test in tests/ 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

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

oapispec-0.0.4.tar.gz (12.7 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page