Skip to main content

OpenAPI Python annotations

Generate OpenApi with code-first annotations. Use type-hint info and docstrings to document your API. This is an experimental library!

Getting Started

Install the package

pip install openapi-annotations

Document a model

from openapi_annotations import api_model, api_property, StringFormats


@api_model
class Event:
    """The first model from this API."""

    @property
    def id(self) -> str:
        return self._id

    @id.setter
    @api_property(data_format=StringFormats.Uuid, nullable=True)
    def id(self, value: str):
        """The id of the object."""
        self._id = value

    @property
    def documented_property(self) -> str:
        return self._documented_property

    @documented_property.setter
    @api_property()
    def documented_property(self, value: str):
        """Property documentation."""
        self._documented_property = value

Document a route

from openapi_annotations import api_route, api_response
from http import HTTPStatus
from .models import Event # My custom model


@api_response(HTTPStatus.CREATED, Event)
@api_response(HTTPStatus.BAD_REQUEST)
@api_response(HTTPStatus.INTERNAL_SERVER_ERROR)
@api_route(Methods.Post, f'events/{{eventId}}', Event)
def events_post(body=None):
    """Creates a new event and returns it."""
    result = {}
    return json.dumps(result), 201

View the swagger:

import .controllers # Make sure you import all files containing annotations
from openapi_annotations import get_spec
import json

with open('./openapi.json', 'w') as file:
    file.write(json.dumps(spec, indent=4))

Metadata

Release files for openapi-annotations 0.1.2

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-annotations 0.1.2
File Size Uploaded
openapi-annotations-0.1.2.tar.gz 7.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openapi-annotations 0.1.2
File Interpreter ABI Platform
openapi_annotations-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 16.7 kB

Release files / openapi-annotations-0.1.2.tar.gz

Download URL openapi-annotations-0.1.2.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
219ae4cca799bc804b9b5b5f8b38b1855a40160dedde2b7a58402469eebda240
BLAKE2b-256 checksum
How to use checksums
5c173910b2b87bab0056b6418c8f827d0902bcc39b4001406640e417b3a37372
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.7.4 Windows/10

Release files / openapi_annotations-0.1.2-py3-none-any.whl

Download URL openapi_annotations-0.1.2-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e8390c205933238e34e89d726787dd99dd764c633311d4eda55bb3a09b9416d3
BLAKE2b-256 checksum
How to use checksums
e2235a601bfb2b00294ae9cb71e8544dc70e016a36e89866ffc002ce157d9815
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.7.4 Windows/10

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

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