OpenAPI Core
openapi-core provides client-side and server-side support for validating requests and responses against the OpenAPI Specification v3.x and v3.2. It integrates with various web frameworks like Flask, Starlette, Falcon, Django, and aiohttp. The current version is 0.23.1, with a frequent release cadence to support new Python versions and framework updates.
Warnings
- breaking Support for Python 3.9 was dropped in version 0.23.0b1. Support for Python 3.8 was dropped in version 0.20.0.
- breaking With the introduction of OpenAPI 3.2 support, older 'V3 aliases' for specification components have been moved to 'V32' paths. For example, if you were directly importing from `openapi_core.spec.v3_0` it might need adjustment if those aliases were changed or removed.
- deprecated The `spec_base_uri` configuration parameter for `OpenAPI` initialization is deprecated.
- breaking The defaults for `style_deserializers_factory` and `media_tyles_deserialization_factory` in configuration and protocols were changed to `None`.
- gotcha Version 0.23.0 introduced an opt-in strict mode for omitted `additionalProperties`. By default, `additionalProperties` are allowed if not explicitly forbidden in the schema, but this mode can make validation stricter.
Install
-
pip install openapi-core -
pip install openapi-core[flask,starlette,falcon,django,aiohttp]
Imports
- OpenAPI
from openapi_core import OpenAPI
- RequestValidator
from openapi_core.validation.request.validators import RequestValidator
- ResponseValidator
from openapi_core.validation.response.validators import ResponseValidator
- OpenAPIRequest
from openapi_core.wrappers.mock import MockRequest as OpenAPIRequest
- OpenAPIResponse
from openapi_core.wrappers.mock import MockResponse as OpenAPIResponse
Quickstart
import io
from openapi_core import OpenAPI
from openapi_core.validation.request.validators import RequestValidator
from openapi_core.validation.response.validators import ResponseValidator
from openapi_core.wrappers.mock import MockRequest, MockResponse
# Define a simple OpenAPI spec
oas_spec = {
"openapi": "3.0.0",
"info": {"title": "Test API", "version": "1.0.0"},
"paths": {
"/hello": {
"get": {
"parameters": [
{
"name": "name",
"in": "query",
"required": True,
"schema": {"type": "string"}
}
],
"responses": {
"200": {
"description": "A greeting",
"content": {
"application/json": {
"schema": {"type": "object", "properties": {"message": {"type": "string"}}}
}
}
}
}
}
}
}
}
# Load the OpenAPI specification
openapi = OpenAPI.from_dict(oas_spec)
# --- Request Validation ---
# Create a mock request for validation
request = MockRequest(
host_url="http://localhost",
path="/hello",
method="get",
query_string="name=World"
)
validator = RequestValidator(openapi)
result = validator.validate(request)
if result.errors:
print(f"Request validation errors: {result.errors}")
else:
print("Request validated successfully.")
# Access validated parameters
print(f"Validated query parameters: {result.parameters.query}")
# --- Response Validation ---
# Create a mock response for validation
response = MockResponse(
data=io.BytesIO(b'{"message": "Hello, World!"}'),
status_code=200,
mimetype="application/json"
)
validator = ResponseValidator(openapi)
result = validator.validate(request, response)
if result.errors:
print(f"Response validation errors: {result.errors}")
else:
print("Response validated successfully.")