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.0.tar.gz
.
File metadata
- Download URL: openapi-pydantic-models-0.1.0.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1df7050aa43d9f61e469304c003ae0212fced8435627c565274d8d38180ddd11 |
|
MD5 | ce51c006a381d1cdccb00271a8fdb4de |
|
BLAKE2b-256 | 1e8ff6cf11d26765a6512dbf4c08669a6af0821cf5e89f43829394512bd5e0d3 |
File details
Details for the file openapi_pydantic_models-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: openapi_pydantic_models-0.1.0-py3-none-any.whl
- Upload date:
- Size: 23.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c233677bc86f6e36e9c5948e1e5b99565e9cd4cdd956d3315cb05aeb277d339 |
|
MD5 | 14c08f3ec737bc2554b9c521e945beb7 |
|
BLAKE2b-256 | bebe28b1f2772a505cdaf0b484dc6d750d81468503d79eaed6aa5985b66bab62 |