pydantic models for OpeanAPI Specification 3.1.0 objects.
Project description
openapi-pydantic-models
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
Built Distribution
File details
Details for the file openapi-pydantic-models-0.1.1.tar.gz
.
File metadata
- Download URL: openapi-pydantic-models-0.1.1.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6b22cb7a8b214af782598a49066ba3265207decd65b66ee2e56f4a5091ffdde |
|
MD5 | 554c562cf9f4d0d57bd6709c24ddbab1 |
|
BLAKE2b-256 | f07e93aef79a9328cd9da9b3964f015f1effe3a2d7c5838e571dc2b8b169eb81 |
File details
Details for the file openapi_pydantic_models-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: openapi_pydantic_models-0.1.1-py3-none-any.whl
- Upload date:
- Size: 23.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5302a828343ea0209ca479df6375f9db5112e32a98a8c89982ecb6d5f531f439 |
|
MD5 | 38a229948ed45b4161d4027f39367ef5 |
|
BLAKE2b-256 | e19aa9c5eb47dfc7efd0ac899290fa36e4a8f53acd643fe3bfe623f797a05bb3 |