# python-openapi/openapi-core architecture

> Architecture diagram of the GitHub repository python-openapi/openapi-core, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/python-openapi/openapi-core
- Repository: https://github.com/python-openapi/openapi-core

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Library
    subgraph "Core Library" 
        direction TB
        Config["Configurations"]:::core
        Protocols["Protocols"]:::core
        SpecLoader["Spec Loader (OpenAPI.from_file_path)"]:::core
        SchemaModel["Spec Model"]:::core
        Validation["Validation Subsystem"]:::core
        Unmarshalling["Unmarshalling Subsystem"]:::core
        Casting["Casting Subsystem"]:::core
        Deserializing["Deserializing Subsystem"]:::core
        Templating["Templating Subsystem"]:::core
        Security["Security Providers"]:::core
        Exceptions["Exceptions"]:::core
    end

    %% Contrib Adapters
    subgraph "Contrib Adapters"
        direction TB
        AioHTTP["AioHTTP Adapter"]:::contrib
        Django["Django Adapter"]:::contrib
        Falcon["Falcon Adapter"]:::contrib
        FastAPI["FastAPI Adapter"]:::contrib
        Flask["Flask Adapter"]:::contrib
        Requests["Requests Adapter"]:::contrib
        Starlette["Starlette Adapter"]:::contrib
        Werkzeug["Werkzeug Adapter"]:::contrib
    end

    %% User Application
    UserApp["User Application"]:::external

    %% Core Pipeline Flow
    Config --> SpecLoader
    Protocols --> SpecLoader
    SpecLoader --> SchemaModel
    SchemaModel --> Validation
    Validation --> Unmarshalling
    Validation --> Exceptions
    Unmarshalling --> Casting
    Casting --> Deserializing
    Deserializing --> Templating
    Templating --> Security
    Security --> UserApp

    %% Adapters to Core
    AioHTTP --> Validation
    Django --> Validation
    Falcon --> Validation
    FastAPI --> Validation
    Flask --> Validation
    Requests --> Validation
    Starlette --> Validation
    Werkzeug --> Validation

    %% Styles
    classDef core fill:#E8F1F2,stroke:#0B6E99,stroke-width:2px
    classDef contrib fill:#F2E8F1,stroke:#990B6E,stroke-width:2px
    classDef external fill:#F1F2E8,stroke:#999900,stroke-width:2px

    %% Click Events
    click SpecLoader "https://github.com/python-openapi/openapi-core/blob/master/openapi_core/app.py"
    click Config "https://github.com/python-openapi/openapi-core/blob/master/openapi_core/configurations.py"
    click Protocols "https://github.com/python-openapi/openapi-core/blob/master/openapi_core/protocols.py"
    click Casting "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/casting/"
    click Deserializing "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/deserializing/"
    click SchemaModel "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/schema/"
    click Validation "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/validation/"
    click Unmarshalling "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/unmarshalling/"
    click Templating "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/templating/"
    click Security "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/security/"
    click AioHTTP "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/contrib/aiohttp/"
    click Django "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/contrib/django/"
    click Falcon "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/contrib/falcon/"
    click FastAPI "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/contrib/fastapi/"
    click Flask "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/contrib/flask/"
    click Requests "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/contrib/requests/"
    click Starlette "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/contrib/starlette/"
    click Werkzeug "https://github.com/python-openapi/openapi-core/tree/master/openapi_core/contrib/werkzeug/"
```
