Skip to main content

validation for aiohttp swagger openAPI 3

Project description

aiohttp-swagger3

https://travis-ci.com/hh-h/aiohttp-swagger3.svg?branch=master https://img.shields.io/codecov/c/github/hh-h/aiohttp-swagger3/master.svg?style=flat https://badge.fury.io/py/aiohttp-swagger3.svg https://img.shields.io/badge/python-3.6%2B-brightgreen.svg https://img.shields.io/badge/code%20style-black-black.svg https://img.shields.io/pypi/l/aiohttp-swagger3.svg

About

Package for displaying swagger docs via different UI backends and optionally validating/parsing aiohttp requests using swagger specification 3.0, known as OpenAPI3.

Supported UI backends

Multiple UI backends can be used or UI backend can be disabled at all if only needed validation without being able to view documentation.

Disable validation

Pass validate=False to SwaggerDocs/SwaggerFile class, the default is True
Also, sometimes validation has to be disabled for a route, to do this you have to pass validate=False during the initialization of the route.
ex. web.post("/route", handler, validate=False), the default is True

Requirements

  • python 3.6+

  • aiohttp >= 3.5.4

  • pyyaml == 5.3

  • attrs == 19.3.0

  • python-fastjsonschema == 2.14.3

  • strict_rfc3339 == 0.7

Limitations

  • only application/json and application/x-www-form-urlencoded supported for now, but you can create own handler

  • header/query parameters only supported simple/form array serialization, e.g. 1,2,3,4

  • see TODO below

Installation

pip install aiohttp-swagger3

Example

from aiohttp import web
from aiohttp_swagger3 import SwaggerDocs, SwaggerUiSettings

async def get_one_pet(request: web.Request, pet_id: int) -> web.Response:
    """
    Optional route description
    ---
    summary: Info for a specific pet
    tags:
      - pets
    parameters:
      - name: pet_id
        in: path
        required: true
        description: The id of the pet to retrieve
        schema:
          type: integer
          format: int32
    responses:
      '200':
        description: Expected response to a valid request
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Pet"
    """
    if pet_id not in request.app['storage']:
        raise web.HTTPNotFound()
    return web.json_response(request.app['storage'][pet_id])

def main():
    app = web.Application()
    s = SwaggerDocs(
        app,
        swagger_ui_settings=SwaggerUiSettings(path="/docs/"),
        title="Swagger Petstore",
        version="1.0.0",
        components="components.yaml"
    )
    s.add_routes([
        web.get("/pets/{pet_id}", get_one_pet),
    ])
    app['storage'] = {}
    web.run_app(app)

More examples

How it helps

https://raw.githubusercontent.com/hh-h/aiohttp-swagger3/master/docs/_static/comparison.png

Features

  • application/json

  • application/x-www-form-urlencoded (except array and object)

  • items

  • properties

  • pattern

  • required

  • enum

  • minimum

  • maximum

  • exclusiveMinimum

  • exclusiveMaximum

  • minLength

  • maxLength

  • minItems

  • maxItems

  • uniqueItems

  • minProperties

  • maxProperties

  • default (only primitives)

  • additionalProperties

  • nullable

  • allOf

  • oneOf

  • anyOf

  • string formats: date, date-time, password, byte, binary, email, uuid, hostname, ipv4, ipv6

TODO (raise an issue if needed)

  • multipleOf

  • not

  • allowEmptyValue

  • Common Parameters for All Methods of a Path (spec file only)

  • readOnly, writeOnly

  • more serialization methods, see: https://swagger.io/docs/specification/serialization/

  • encoding

  • form data serialization (array, object)

  • default (array, object)

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

aiohttp-swagger3-0.4.1.tar.gz (1.7 MB view details)

Uploaded Source

Built Distribution

aiohttp_swagger3-0.4.1-py3-none-any.whl (1.7 MB view details)

Uploaded Python 3

File details

Details for the file aiohttp-swagger3-0.4.1.tar.gz.

File metadata

  • Download URL: aiohttp-swagger3-0.4.1.tar.gz
  • Upload date:
  • Size: 1.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for aiohttp-swagger3-0.4.1.tar.gz
Algorithm Hash digest
SHA256 793b806f90b9f707510e8e9a0e91049a144d2f7acda84e2ee2e9a098f48ada71
MD5 0e151fe09941ad26f55b4a6593223739
BLAKE2b-256 70e891705fc3e097f5af28382d422deb5874b712b0eb31566842fff6d0a0674e

See more details on using hashes here.

File details

Details for the file aiohttp_swagger3-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_swagger3-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 1.7 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for aiohttp_swagger3-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8e667726223ca1e58421377b1d2554bf331c7ad94b0117f0b6bc88b0322d367e
MD5 e43ae2d68ee48f5696b7c94c3f49cff6
BLAKE2b-256 758c9399516f504ab7275e1416e23a2feb03b6838fbdd99bd37c9a8c2ab4dafc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page