Generate OpenApi with code-first annotations. Use type-hint info and docstrings to document your API. This is an experimental library!
Project description
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))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file openapi-annotations-0.1.2.tar.gz
.
File metadata
- Download URL: openapi-annotations-0.1.2.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.7.4 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 219ae4cca799bc804b9b5b5f8b38b1855a40160dedde2b7a58402469eebda240 |
|
MD5 | c63458ba92ad2ea2dbcc99a83feb724b |
|
BLAKE2b-256 | 5c173910b2b87bab0056b6418c8f827d0902bcc39b4001406640e417b3a37372 |
File details
Details for the file openapi_annotations-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: openapi_annotations-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.7.4 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8390c205933238e34e89d726787dd99dd764c633311d4eda55bb3a09b9416d3 |
|
MD5 | 4c99c5424602655b6da8bc2d609fb8e6 |
|
BLAKE2b-256 | e2235a601bfb2b00294ae9cb71e8544dc70e016a36e89866ffc002ce157d9815 |