Skip to main content

sanic-openapi-ext

sanic-openapi-ext is a Python package that provides a simplified method for generating OpenAPI (Swagger) documentation from Marshmallow schemas in a Sanic web application. It offers an explicit mapping between Marshmallow fields and OpenAPI fields, which aids in the automatic generation of precise API documentation.

Features

  • Automatic generation of OpenAPI (Swagger) documentation from Marshmallow schemas.
  • Handles different types of Marshmallow fields including basic types, complex types, dates, and numbers.
  • Supports conversion of nested fields and lists.

Installation

pip install sanic-openapi-ext

Quick Start

Here's a simple example of how to use sanic-openapi-ext:

from marshmallow import Schema, fields
from sanic_openapi_ext import open_api_schemas

class UserSchema(Schema):
    name = fields.Str()
    birthdate = fields.Date()

# Generate OpenAPI schema from Marshmallow schema
user_openapi_schema = open_api_schemas(UserSchema())

Examples

Input/output simple schema object

Parameter represents query params as ExampleSchema

Response represents json output as ExampleSchema

from marshmallow import Schema, fields
from sanic import Sanic, response
from sanic_openapi_ext import open_api_schemas, open_api_schemas_params
from sanic_ext import openapi
from sanic_ext.extensions.openapi.definitions import Response


class ExampleSchema(Schema):
    name = fields.Str(required=True)
    age = fields.Integer(required=True)


app = Sanic("MyHelloWorldApp")


@app.get("/")
@openapi.definition(
    parameter=open_api_schemas_params(ExampleSchema()),
    response=[
        Response(open_api_schemas(ExampleSchema(), is_json=True), status=200),
    ],
)
async def get_and_return_example_schema(request):
    result = ExampleSchema().dump({"name": "John Doe", "age": 42})
    return response.json({"success": 200, "result": result})

Sanic OpenAPI spec screenshot

Input schema object with enum, output - nested schema with enum

Parameter represents header params as ExampleSchema

Response represents json output as list of ExampleSchema

from enum import Enum
from marshmallow import Schema, fields
from sanic import Sanic, response
from sanic_openapi_ext import open_api_schemas, open_api_schemas_params
from sanic_ext import openapi
from sanic_ext.extensions.openapi.definitions import Response


class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3


class ExampleSchema(Schema):
    text = fields.Str()
    color = fields.Enum(Color)


class ExampleListSchema(Schema):
    items = fields.List(fields.Nested(ExampleSchema))


app = Sanic("MyHelloWorldApp")


@app.get("/")
@openapi.definition(
    parameter=open_api_schemas_params(ExampleSchema(), location="header"),
    response=[
        Response(open_api_schemas(ExampleListSchema(), is_json=True), status=200),
    ],
)
async def get_and_return_example_schema(request):
    result = ExampleListSchema().dump([{"text": "Lorem ipsum", "color": 1}], many=True)
    return response.json({"success": 200, "result": result})

Sanic OpenAPI spec screenshot

Mapping reference

Below is a mapping table of how Marshmallow fields are converted into OpenAPI fields:

Marshmallow Field OpenAPI Field
DateTime doc.DateTime
Date doc.Date
Time doc.Time
Integer,Int doc.Integer
Float doc.Float
Decimal doc.String
String, Str doc.String
Boolean, Bool doc.Boolean
Enum doc.Schema
List doc.Array
Nested doc.Object

Note:

  • If the many attribute of the Marshmallow schema is set to True, or if the input many attribute is True, a doc.List will be returned.
  • If the input is_json attribute is True, a doc.JsonBody will be returned.
  • If the field is of type fields.Enum, a doc.Schema, which accepts the enumeration, is returned.
  • If the field is of type fields.List, the inner_field is accessed. If the inner_field is a fields.Nested, a doc.Object is returned with the properties of the nested schema. If the inner_fields belong to other types, a doc.Array is returned with the properties of the inner type.
  • For fields not listed in the table, doc.String is used as a default.

Please, remember that this mapping may vary according to the use and the specific need of the project.

Contribute

We'd love for you to contribute to our source code and to make sanic-openapi-ext even better than it is today! Here are some ways you can contribute:

  • by reporting bugs
  • by suggesting new features
  • by writing or editing documentation
  • by writing specifications
  • by writing code (no patch is too small, fix typos, add comments, clean up inconsistent whitespace)
  • by refactoring code
  • by closing issues
  • by reviewing patches

License

sanic-openapi-ext is released under the MIT license.

Metadata

Release files for sanic-openapi-ext 0.1.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 sanic-openapi-ext 0.1.0
File Size Uploaded Provenance Actions
sanic_openapi_ext-0.1.0.tar.gz 4.7 kB Without provenance Details for sanic_openapi_ext-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sanic-openapi-ext 0.1.0
File Interpreter ABI Platform Actions
sanic_openapi_ext-0.1.0-py3-none-any.whl Python 3 none any Details for sanic_openapi_ext-0.1.0-py3-none-any.whl

Total release size: 10.3 kB

Release files / sanic_openapi_ext-0.1.0.tar.gz

Download URL sanic_openapi_ext-0.1.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5d177db3d5c4dc5fb45678428620f0fa82f8acc96f095e1c0922b9d854285f88
BLAKE2b-256 checksum
How to use checksums
9060a605f5af40a4dd119f3083d4e0dd567cfc6a29457fac983c80971299d0d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1019-azure

Release files / sanic_openapi_ext-0.1.0-py3-none-any.whl

Download URL sanic_openapi_ext-0.1.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
667447311190db835d92116bc4975862e184a09f5588597dfdbc64c998b9e2c5
BLAKE2b-256 checksum
How to use checksums
cd70e6587b76c90e012173c5be0968a716385afd3b062842d85b484096a50fb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1019-azure

Security for sanic-openapi-ext 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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