Skip to main content

pydantic models for OpeanAPI Specification 3.1.0 objects.

Project description

openapi-pydantic-models

PyPI Status license python_versions

OpenAPI Specification v3.1.0 objects implemented as pydantic models.

Install

pip install openapi-pydantic-models

Usage

It can load whole OpenAPI specification:

import requests
import yaml

from openapi_pydantic_models import OpenAPIObject

url = "https://rapidocweb.com/specs/petstore_extended.yaml"
response = requests.get(url)
data = yaml.full_load(response.text)
spec = OpenAPIObject.parse_obj(data)

All parts of OpenAPI object tree are represented by Python classes with static type infos:

repr(spec.paths["/pet/{petId}/uploadImage"].post.responses["200"])

ResponseObject(
    description='successful operation',
    headers=None,
    content={
        'application/json': MediaTypeObject(
            schema_={
                '$ref': '#/components/schemas/ApiResponse'
            },
            example=None,
            examples=None,
            encoding=None
        )
    },
    links=None
)

Any object from can always be exported back to OpenaAPI data (dict) via dict() method:

spec.paths["/pet/{petId}/uploadImage"].post.responses["200"].dict()

{
    'description': 'successful operation',
     'content': {
        'application/json': {
            'schema': {
                '$ref': '#/components/schemas/ApiResponse'
            }
        }
    }
}

Loading specification performs minimal (unavoidable) validation: it rises exception for unknown fields:

from openapi_pydantic_models import ResponseObject

data = {
    "description": 'successful operation',
    "foo": "bar"
}

obj = ResponseObject.parse_obj(data)

# ValidationError: 1 validation error for ResponseObject
# foo
#   extra fields not permitted (type=value_error.extra)

Any other validations defined by OpenAPI Specification are not implemented. openapi-pydantic-models intends to make programmatic editing of OpenAPI specifications easier and developer friendly (compared to working with "raw" dict-s). Complex spec validations are already implemented in other packages.

Even though "extra" fields in input are not allowed, 4.9 Specification Extensions are fully and transparently supported for objects that allow them:

data = {
    "description": 'successful operation',
    "x-foo": "bar",
    "x-bar": [42]
}
obj = ResponseObject.parse_obj(data)

obj.extensions
ExtensionsStorage({'x-foo': 'bar', 'x-bar': [42]})

obj.dict()
{'description': 'successful operation', 'x-foo': 'bar', 'x-bar': [42]}

Where are the docs for this thing?

Makes no sense writing them since OpenAPI Specification is already fully and excellently documented.

In OpenAPI docs, wherever you see "Foo Object" you can find class FooObject in openapi-pydantic-models. Reverse works too: if you want to know what is openapi_pydantic_models.ServerObject check 4.8.5 Server Object

Where are the tests?

There is one!

I know :-( !

This was written during one lazy weekend afternoon. When I get one of those again, I might add more tests.

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

openapi-pydantic-models-0.1.1.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

openapi_pydantic_models-0.1.1-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file openapi-pydantic-models-0.1.1.tar.gz.

File metadata

File hashes

Hashes for openapi-pydantic-models-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f6b22cb7a8b214af782598a49066ba3265207decd65b66ee2e56f4a5091ffdde
MD5 554c562cf9f4d0d57bd6709c24ddbab1
BLAKE2b-256 f07e93aef79a9328cd9da9b3964f015f1effe3a2d7c5838e571dc2b8b169eb81

See more details on using hashes here.

File details

Details for the file openapi_pydantic_models-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for openapi_pydantic_models-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5302a828343ea0209ca479df6375f9db5112e32a98a8c89982ecb6d5f531f439
MD5 38a229948ed45b4161d4027f39367ef5
BLAKE2b-256 e19aa9c5eb47dfc7efd0ac899290fa36e4a8f53acd643fe3bfe623f797a05bb3

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