Skip to content

Loader

openapi_first.loader

openapi_first.loaders

OpenAPI specification loading and validation utilities.

This module is responsible for loading an OpenAPI 3.x specification from disk and validating it before it is used by the application.

It enforces the principle that an invalid or malformed OpenAPI document must never reach the routing or runtime layers.

Design principles

  • OpenAPI is treated as an authoritative contract.
  • Invalid specifications fail fast at application startup.
  • Supported formats are JSON and YAML.
  • Validation errors are surfaced clearly and early.

This module intentionally does NOT:

  • Modify the OpenAPI document
  • Infer missing fields
  • Generate models or code
  • Perform request/response validation at runtime

OpenAPISpecLoadError

Bases: OpenAPIFirstError

Raised when an OpenAPI specification cannot be loaded or validated.

This error indicates that the OpenAPI document is unreadable, malformed, or violates the OpenAPI 3.x specification.

load_openapi

load_openapi(path: Union[str, Path]) -> Dict[str, Any]

Load and validate an OpenAPI 3.x specification from disk.

The specification is parsed based on file extension and validated using a strict OpenAPI schema validator. Any error results in an immediate exception, preventing application startup.

Parameters

path : str or pathlib.Path Filesystem path to an OpenAPI specification file. Supported extensions: - .json - .yaml - .yml

Returns

dict Parsed and validated OpenAPI specification.

Raises

OpenAPISpecLoadError If the file does not exist, cannot be parsed, or fails OpenAPI schema validation.