OpenAPI v3 parser that transforms specification documents into structured Python objects for programmatic access and manipulation
—
The type system provides enumeration classes that define allowed values for various OpenAPI specification fields. These enums ensure type safety and provide validation constraints for data types, formats, HTTP methods, parameter locations, content types, and security configurations.
Defines the fundamental data types available for OpenAPI schemas.
class DataType(Enum):
NULL = "null"
INTEGER = "integer"
NUMBER = "number"
STRING = "string"
BOOLEAN = "boolean"
ARRAY = "array"
OBJECT = "object"
ONE_OF = "oneOf"
ANY_OF = "anyOf"Values:
NULL: Null/none typeINTEGER: Whole numbersNUMBER: Floating-point numbersSTRING: Text valuesBOOLEAN: True/false valuesARRAY: Ordered collectionsOBJECT: Key-value structuresONE_OF: Exactly one schema must matchANY_OF: One or more schemas must matchStandard format specifications for integer values.
class IntegerFormat(Enum):
INT32 = "int32"
INT64 = "int64"Values:
INT32: Signed 32-bit integer (-2³¹ to 2³¹-1)INT64: Signed 64-bit integer (-2⁶³ to 2⁶³-1)Standard format specifications for floating-point numbers.
class NumberFormat(Enum):
FLOAT = "float"
DOUBLE = "double"Values:
FLOAT: Single-precision floating-pointDOUBLE: Double-precision floating-pointFormat specifications for string validation and interpretation.
class StringFormat(Enum):
BYTE = "byte"
BINARY = "binary"
DATE = "date"
DATETIME = "date-time"
PASSWORD = "password"
UUID = "uuid"
UUID4 = "uuid4"
EMAIL = "email"
URI = "uri"
HOSTNAME = "hostname"
IPV4 = "ipv4"
IPV6 = "ipv6"
URL = "url"Values:
BYTE: Base64-encoded binary dataBINARY: Binary dataDATE: Full-date (RFC 3339, e.g., "2023-12-25")DATETIME: Date-time (RFC 3339, e.g., "2023-12-25T10:30:00Z")PASSWORD: Password input (UI hint)UUID: Universally unique identifierUUID4: UUID version 4 specificallyEMAIL: Email addressURI: Universal resource identifierHOSTNAME: Internet hostnameIPV4: IPv4 addressIPV6: IPv6 addressURL: Universal resource locatorDefines the supported HTTP methods for API operations.
class OperationMethod(Enum):
GET = "get"
PUT = "put"
POST = "post"
DELETE = "delete"
OPTIONS = "options"
HEAD = "head"
PATCH = "patch"
TRACE = "trace"Values:
GET: Retrieve resourcePUT: Update/replace resourcePOST: Create resource or submit dataDELETE: Remove resourceOPTIONS: Get allowed methodsHEAD: Get headers onlyPATCH: Partial updateTRACE: Diagnostic traceBase locations for security parameters.
class BaseLocation(Enum):
HEADER = "header"
QUERY = "query"
COOKIE = "cookie"All supported parameter locations including path parameters.
class ParameterLocation(Enum):
HEADER = "header"
QUERY = "query"
COOKIE = "cookie"
PATH = "path"Values:
HEADER: HTTP header parameterQUERY: URL query string parameterCOOKIE: HTTP cookie parameterPATH: URL path parameterSerialization styles for path parameters.
class PathParameterStyle(Enum):
SIMPLE = "simple"
LABEL = "label"
MATRIX = "matrix"Values:
SIMPLE: Simple comma-separated valuesLABEL: Dot-prefixed valuesMATRIX: Semicolon-prefixed key-value pairsSerialization styles for query parameters.
class QueryParameterStyle(Enum):
FORM = "form"
SPACE_DELIMITED = "spaceDelimited"
PIPE_DELIMITED = "pipeDelimited"
DEEP_OBJECT = "deepObject"Values:
FORM: Form-style serializationSPACE_DELIMITED: Space-separated valuesPIPE_DELIMITED: Pipe-separated valuesDEEP_OBJECT: Deep object serializationSerialization styles for header parameters.
class HeaderParameterStyle(Enum):
SIMPLE = "simple"Serialization styles for cookie parameters.
class CookieParameterStyle(Enum):
FORM = "form"Standard MIME content types for request and response bodies.
class ContentType(Enum):
JSON = "application/json"
JSON_TEXT = "text/json"
JSON_ANY = "application/*+json"
JSON_PROBLEM = "application/problem+json"
XML = "application/xml"
FORM = "application/x-www-form-urlencoded"
MULTIPART_FORM = "multipart/form-data"
PLAIN_TEXT = "text/plain"
HTML = "text/html"
PDF = "application/pdf"
PNG = "image/png"
JPEG = "image/jpeg"
GIF = "image/gif"
SVG = "image/svg+xml"
AVIF = "image/avif"
BMP = "image/bmp"
WEBP = "image/webp"
Image = "image/*"
BINARY = "application/octet-stream"Security scheme types for API authentication.
class SecurityType(Enum):
API_KEY = "apiKey"
HTTP = "http"
OAUTH2 = "oauth2"
OPEN_ID_CONNECT = "openIdConnect"Values:
API_KEY: API key authenticationHTTP: HTTP authentication schemesOAUTH2: OAuth 2.0 authenticationOPEN_ID_CONNECT: OpenID Connect authenticationHTTP authentication schemes for security configurations.
class AuthenticationScheme(Enum):
BASIC = "basic"
BEARER = "bearer"
DIGEST = "digest"
HOBA = "hoba"
MUTUAL = "mutual"
NEGOTIATE = "negotiate"
OAUTH = "oauth"
SCRAM_SHA1 = "scram-sha-1"
SCRAM_SHA256 = "scram-sha-256"
VAPID = "vapid"OAuth 2.0 flow types for authentication configuration.
class OAuthFlowType(Enum):
IMPLICIT = "implicit"
PASSWORD = "password"
CLIENT_CREDENTIALS = "clientCredentials"
AUTHORIZATION_CODE = "authorizationCode"Values:
IMPLICIT: Implicit grant flowPASSWORD: Resource owner password credentials flowCLIENT_CREDENTIALS: Client credentials grant flowAUTHORIZATION_CODE: Authorization code grant flowFor compatibility with non-standard specifications, the library provides loose type definitions that allow custom values beyond the standard enums:
@dataclass
class LooseEnum:
"""Container for non-standard enum values"""
value: str
# Type aliases for loose validation (all are aliases to LooseEnum)
LooseContentType = LooseEnum
LooseIntegerFormat = LooseEnum
LooseNumberFormat = LooseEnum
LooseStringFormat = LooseEnumWorking with enums:
from openapi_parser import parse
from openapi_parser.enumeration import DataType, OperationMethod, ContentType
spec = parse('api-spec.yml')
# Check data types in schemas
for name, schema in (spec.schemas or {}).items():
print(f"Schema {name}: {schema.type.value}")
# Type-specific handling
if schema.type == DataType.STRING:
print(f" String schema with format: {getattr(schema, 'format', 'none')}")
elif schema.type == DataType.ARRAY:
print(f" Array with item type: {schema.items.type.value}")
# List HTTP methods used
methods_used = set()
for path in spec.paths:
for operation in path.operations:
methods_used.add(operation.method)
print("HTTP methods used:")
for method in sorted(methods_used, key=lambda m: m.value):
print(f" {method.value.upper()}")
# Find content types
content_types_used = set()
for path in spec.paths:
for operation in path.operations:
if operation.request_body:
for content in operation.request_body.content:
if isinstance(content.type, ContentType):
content_types_used.add(content.type.value)
print("Content types used:")
for ct in sorted(content_types_used):
print(f" {ct}")Enum validation and loose types:
# With strict enum validation (default)
spec_strict = parse('api-spec.yml', strict_enum=True)
# With loose enum validation (allows custom values)
spec_loose = parse('api-spec.yml', strict_enum=False)
# Check for loose types
for path in spec_loose.paths:
for operation in path.operations:
if operation.request_body:
for content in operation.request_body.content:
if isinstance(content.type, str):
print(f"Custom content type: {content.type}")Install with Tessl CLI
npx tessl i tessl/pypi-openapi3-parser