Skip to main content

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.

Metadata

Release files for openapi-pydantic-models 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openapi-pydantic-models 1.0.1
File Size Uploaded Provenance Actions
openapi-pydantic-models-1.0.1.tar.gz 14.7 kB Without provenance Details for openapi-pydantic-models-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for openapi-pydantic-models 1.0.1
File Interpreter ABI Platform Actions
openapi_pydantic_models-1.0.1-py3-none-any.whl Python 3 none any Details for openapi_pydantic_models-1.0.1-py3-none-any.whl

Total release size: 38.5 kB

Release files / openapi-pydantic-models-1.0.1.tar.gz

Download URL openapi-pydantic-models-1.0.1.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2a5295de3abe0e862f67f1444e2281bcadf11cd6e3feb6e70515a829fdc0dd8a
BLAKE2b-256 checksum
How to use checksums
e542888fb76c014a5ee6a846201f13753b5e363b227509efdb946f9850e9bb8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / openapi_pydantic_models-1.0.1-py3-none-any.whl

Download URL openapi_pydantic_models-1.0.1-py3-none-any.whl
Size 23.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0a028f8b0f344bba1790a90ead48f48430951cad870e507f28f68aa32b57ebd
BLAKE2b-256 checksum
How to use checksums
bc1222f375d6b1c686087a6a0bd2b27d79344f5a8612e9b750487ee5e19c3c7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.1 CPython/3.11.4

Security for openapi-pydantic-models 1.0.1

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.

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