This commit is contained in:
Correl Roush 2021-03-22 21:48:55 -04:00
parent 09a737bea3
commit e5a986140a

View file

@ -4,10 +4,11 @@
How to use [[file:20210226114112-openapi_core.org][OpenAPI Core]]
#+begin_src python :results code :exports both
import jsonschema
from openapi_core import create_spec
from openapi_core.unmarshalling.schemas.factories import SchemaUnmarshallersFactory
spec = create_spec({
spec_dict = {
"openapi": "3.0.0",
"info": {
"title": "Test API",
@ -15,10 +16,18 @@ How to use [[file:20210226114112-openapi_core.org][OpenAPI Core]]
},
"components": {
"schemas": {
"resource_id": {
"type": "string",
"format": "uuid",
},
"resource": {
"type": "object",
"properties": {"name": {"type": "string"}},
"required": ["name"],
"properties": {
"id": {"$ref": "#/components/schemas/resource_id"},
"name": {"type": "string"},
},
"required": ["id", "name"],
"additionalProperties": False,
},
},
"securitySchemes": {
@ -53,14 +62,18 @@ How to use [[file:20210226114112-openapi_core.org][OpenAPI Core]]
}
}
},
})
}
spec = create_spec(spec_dict)
data = {
"id": "bad-id",
"badname": "Name",
}
factory = SchemaUnmarshallersFactory()
unmarshaller = factory.create(spec.components.schemas.get("resource"))
factory = SchemaUnmarshallersFactory(
resolver=jsonschema.RefResolver(spec.default_url, spec_dict)
)
unmarshaller = factory.create(spec.get_schema("resource"))
try:
return unmarshaller(data)
@ -70,5 +83,5 @@ How to use [[file:20210226114112-openapi_core.org][OpenAPI Core]]
#+RESULTS:
#+begin_src python
Value {'badname': 'Name'} not valid for schema of type SchemaType.OBJECT: (<ValidationError: "'name' is a required property">,)
Value {'id': 'bad-id', 'badname': 'Name'} not valid for schema of type SchemaType.OBJECT: (<ValidationError: "'name' is a required property">, <ValidationError: "Additional properties are not allowed ('badname' was unexpected)">)
#+end_src