| hide |
|
|---|
OpenAPI accepts a Config object that allows users to customize the behavior of validation and unmarshalling processes.
By default, when creating an OpenAPI instance, the provided specification is also validated.
If you know that you have a valid specification already, disabling the validator can improve performance.
from openapi_core import Config
config = Config(
spec_validator_cls=None,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)By default, the request validator is selected based on the detected specification version.
To explicitly validate a:
- OpenAPI 3.0 spec, import
V30RequestValidator - OpenAPI 3.1 spec, import
V31RequestValidatororV31WebhookRequestValidator - OpenAPI 3.2 spec, import
V32RequestValidatororV32WebhookRequestValidator
from openapi_core import V31RequestValidator
config = Config(
request_validator_cls=V31RequestValidator,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)
openapi.validate_request(request)You can also explicitly import V3RequestValidator, which is a shortcut to the latest OpenAPI v3 version.
By default, the response validator is selected based on the detected specification version.
To explicitly validate a:
- OpenAPI 3.0 spec, import
V30ResponseValidator - OpenAPI 3.1 spec, import
V31ResponseValidatororV31WebhookResponseValidator - OpenAPI 3.2 spec, import
V32ResponseValidatororV32WebhookResponseValidator
from openapi_core import V31ResponseValidator
config = Config(
response_validator_cls=V31ResponseValidator,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)
openapi.validate_response(request, response)You can also explicitly import V3ResponseValidator, which is a shortcut to the latest OpenAPI v3 version.
By default, the request unmarshaller is selected based on the detected specification version.
To explicitly validate and unmarshal a request for:
- OpenAPI 3.0 spec, import
V30RequestUnmarshaller - OpenAPI 3.1 spec, import
V31RequestUnmarshallerorV31WebhookRequestUnmarshaller - OpenAPI 3.2 spec, import
V32RequestUnmarshallerorV32WebhookRequestUnmarshaller
from openapi_core import V31RequestUnmarshaller
config = Config(
request_unmarshaller_cls=V31RequestUnmarshaller,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)
result = openapi.unmarshal_request(request)You can also explicitly import V3RequestUnmarshaller, which is a shortcut to the latest OpenAPI v3 version.
To explicitly validate and unmarshal a response:
- For OpenAPI 3.0 spec, import
V30ResponseUnmarshaller - For OpenAPI 3.1 spec, import
V31ResponseUnmarshallerorV31WebhookResponseUnmarshaller - For OpenAPI 3.2 spec, import
V32ResponseUnmarshallerorV32WebhookResponseUnmarshaller
from openapi_core import V31ResponseUnmarshaller
config = Config(
response_unmarshaller_cls=V31ResponseUnmarshaller,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)
result = openapi.unmarshal_response(request, response)You can also explicitly import V3ResponseUnmarshaller, which is a shortcut to the latest OpenAPI v3 version.
The library comes with a set of built-in media type deserializers for formats such as application/json, application/xml, application/x-www-form-urlencoded, and multipart/form-data.
You can also define your own deserializers. To do this, pass a dictionary of custom media type deserializers with the supported MIME types as keys to the unmarshal_response function:
def protobuf_deserializer(message):
feature = route_guide_pb2.Feature()
feature.ParseFromString(message)
return feature
extra_media_type_deserializers = {
'application/protobuf': protobuf_deserializer,
}
config = Config(
extra_media_type_deserializers=extra_media_type_deserializers,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)
result = openapi.unmarshal_response(request, response)By default, OpenAPI follows JSON Schema behavior: when an object schema omits additionalProperties, extra keys are allowed.
If you want stricter behavior, enable strict_additional_properties. In this mode, omitted additionalProperties is treated as false.
from openapi_core import Config
from openapi_core import OpenAPI
config = Config(
strict_additional_properties=True,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)When strict mode is enabled:
- object schema with omitted
additionalPropertiesrejects unknown fields - object schema with
additionalProperties: truestill allows unknown fields
OpenAPI defines a format keyword that hints at how a value should be interpreted. For example, a string with the format date should conform to the RFC 3339 date format.
OpenAPI comes with a set of built-in format validators, but it's also possible to add custom ones.
Here's how you can add support for a usdate format that handles dates in the form MM/DD/YYYY:
import re
def validate_usdate(value):
return bool(re.match(r"^\d{1,2}/\d{1,2}/\d{4}$", value))
extra_format_validators = {
'usdate': validate_usdate,
}
config = Config(
extra_format_validators=extra_format_validators,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)
openapi.validate_response(request, response)Based on the format keyword, openapi-core can also unmarshal values to specific formats.
The library comes with a set of built-in format unmarshallers, but it's also possible to add custom ones.
Here's an example with the usdate format that converts a value to a date object:
from datetime import datetime
def unmarshal_usdate(value):
return datetime.strptime(value, "%m/%d/%Y").date()
extra_format_unmarshallers = {
'usdate': unmarshal_usdate,
}
config = Config(
extra_format_unmarshallers=extra_format_unmarshallers,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)
result = openapi.unmarshal_response(request, response)