How python-openapi/openapi-core works
Project type and purpose
• This is a Python library (“openapi-core”) that implements the OpenAPI v3.0 and v3.1 specifications.
• Its primary goal is to load an OpenAPI spec, then validate and unmarshal request/response data, and expose integration points for web frameworks and HTTP clients.
File structure
• Top-level module “openapi_core/” contains the core implementation.
• Subpackages correspond to major functional areas:
• casting/ – value casting logic for schemas
• deserializing/ – media-type parsers for request/response bodies
• schema/ – OpenAPI schema object models (Parameters, Servers, etc.)
• validation/ – request and response validation pipeline
• unmarshalling/ – turning raw HTTP messages into Python objects based on spec
• templating/ – path and response templating (e.g. URL path parameters, content negotiation)
• security/ – security providers and credential extraction
• contrib/ – framework/client adapters (Flask, Django, FastAPI, AioHTTP, Requests, Starlette, Falcon, Werkzeug)
• Configuration lives in “configurations.py” and integration protocols in “protocols.py”.
• Tests (“tests/”) cover core logic, integrations, unit and integration scenarios.
README for additional insights
• Confirms features: validation, unmarshalling, integration with frameworks/clients, custom deserializers/unmarshallers, security data providers.
• Shows basic usage: construct an OpenAPI object, call unmarshal_request or validate_request, then inspect parameters, body, security.
a. Main components
1. Spec Loader (“OpenAPI.from_file_path”) – reads OpenAPI JSON/YAML → produces an OpenAPI spec model.
2. Core Pipeline
• Validation Subsystem
• Unmarshalling Subsystem
• Casting & Deserializing
• Templating
• Security Providers
3. Configuration & Protocols – interfaces to plug in custom behavior.
4. Contrib Adapters – glue layers for each supported framework/client.
5. User Application – developer’s web service or client that invokes openapi_core.
b. Relationships & interactions
• The user application (or adapter middleware) passes an HTTP request to the Contrib Adapter → invokes core pipeline.
• Core pipeline uses the loaded spec to validate the request (parameters, payload) → if valid, it unmarshals (deserializes) parts (path, query, headers, cookies, body) → applies casting rules → yields a high-level RequestData object.
• Security providers extract credentials from headers/cookies/query and produce a security context.
• The application then processes the request and produces an HTTP response object.
• The response goes back through core pipeline (validation, templating, serialization) if the user opts to validate/unmarshal responses.
c. Architectural patterns & design principles
• Modular layering: each concern isolated in its own subpackage (separation of concerns).
• Open/Closed via configuration and protocols (allows custom media-type handlers or security providers).
• Adapter pattern for framework integrations.
d. Key technologies & libraries
• Python typing and protocols (PEP 484 / PEP 544) to define request/response interfaces.
• YAML/JSON parsing for spec loading.
• mkdocs for documentation; pytest for testing.
Detailed capture of essential elements
• Config & Protocols at the top feeding into each core stage to indicate customization hooks.
• Security providers branching out from Security stage to illustrate API Key, Cookie, Basic, Bearer extraction.