OpenAPI v3 parser that transforms specification documents into structured Python objects for programmatic access and manipulation
—
The core parsing functionality transforms OpenAPI 3.0 specification documents from various sources into structured Python objects. The parser handles both local files and remote URLs, supports YAML and JSON formats, and provides flexible validation options.
Parses OpenAPI specification documents from files, URLs, or strings into a structured Specification object.
def parse(
uri: Optional[str] = None,
spec_string: Optional[str] = None,
strict_enum: bool = True
) -> SpecificationParameters:
uri (str, optional): Path or URL to OpenAPI specification file. Supports local file paths and HTTP/HTTPS URLsspec_string (str, optional): OpenAPI specification content as a string in YAML or JSON formatstrict_enum (bool): Whether to validate content types and string formats against predefined enums. When False, allows custom values beyond OpenAPI standard enumsReturns:
Specification: Parsed specification object containing the complete API definitionRaises:
ParserError: If the specification is invalid, missing required fields, or cannot be parsedUsage Examples:
Parse from local file:
from openapi_parser import parse
# YAML file
spec = parse('api-spec.yml')
# JSON file
spec = parse('api-spec.json')Parse from URL:
# Remote specification
spec = parse('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.yaml')Parse from string:
yaml_content = '''
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
paths:
/health:
get:
summary: Health check
responses:
'200':
description: OK
'''
spec = parse(spec_string=yaml_content)Parse with loose enum validation:
# Allow custom content types and formats not in OpenAPI standard
spec = parse('spec.yml', strict_enum=False)The parser performs comprehensive validation of OpenAPI specifications:
strict_enum=True, validates enumerated values against OpenAPI standardsCommon Error Scenarios:
from openapi_parser import parse, ParserError
try:
spec = parse('invalid-spec.yml')
except ParserError as e:
print(f"Parsing failed: {e}")
# Handle parsing errors - invalid schema, missing required fields, etc.Parser Error Types:
openapi version fieldThe parser automatically detects and handles multiple input formats:
.yml, .yaml files with YAML content.json files with JSON contentstrict_enum=True adds validation overhead but ensures complianceInstall with Tessl CLI
npx tessl i tessl/pypi-openapi3-parser