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.model_validate(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 object tree can always be exported back to OpenaAPI data (model_dump) via model_dump() method:

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

{
    '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.model_validate(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.model_validate(data)

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

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

And of course, all objects can be edited:

obj.description = "ZOMG!"
obj.extensions["x-baz"] = {1: {2: 3}}
obj.model_dump()
{'description': 'ZOMG!', 'x-foo': 'bar', 'x-bar': [42], 'x-baz': {1: {2: 3}}}

where specification extensions are protected from invalid keys:

obj.extensions["baz"] = 34
# KeyError: 'baz'

obj.extensions["x-baz"] = 34
# OK

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-1.0.1.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

openapi_pydantic_models-1.0.1-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for openapi-pydantic-models-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2a5295de3abe0e862f67f1444e2281bcadf11cd6e3feb6e70515a829fdc0dd8a
MD5 6de3ca562a0cce66eabe3a488daefbf0
BLAKE2b-256 e542888fb76c014a5ee6a846201f13753b5e363b227509efdb946f9850e9bb8e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openapi_pydantic_models-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f0a028f8b0f344bba1790a90ead48f48430951cad870e507f28f68aa32b57ebd
MD5 a76d49d8432e4e9a9576a018487c1365
BLAKE2b-256 bc1222f375d6b1c686087a6a0bd2b27d79344f5a8612e9b750487ee5e19c3c7c

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