Bravado Core
bravado-core is a Python library that implements the Swagger 2.0 (OpenAPI Specification v2.0). It provides core functionalities for both client-side and server-side support, including Swagger Schema ingestion and validation, marshalling and unmarshalling of requests and responses, and modeling Swagger definitions as Python classes or dictionaries. The current version is 6.1.1, and it maintains an active, though not rapid, release cadence with new versions typically addressing bugs or minor features rather than frequent major changes.
Warnings
- breaking In version 5.17.0, the equality (==) feature on `Spec` objects was removed. Direct comparison using `==` will no longer work as expected and might lead to errors or unexpected behavior.
- breaking Version 5.0.0 introduced significant breaking changes, including a refactoring of model discovery. The signature of `bravado_core.spec_flattening.flattened_spec` was updated, and several public methods (e.g., `tag_models`, `bless_models`, `collect_models` from `bravado_core.model`, and `post_process_spec` from `bravado_core.spec`) were removed or changed their interface.
- gotcha Older versions of `bravado-core` (specifically those constrained by `jsonschema<4.0.0`) are not compatible with `jsonschema>=4.0.0`. Using a newer `jsonschema` might cause `bravado-core` to break, especially in offline environments where dependency resolution might be less strict.
- gotcha When loading a spec using `Spec.from_dict()`, the `spec.definitions` dictionary (which contains the Python models) might appear empty if the `origin_url` parameter is not provided or is incorrect, especially when loading local files. `bravado-core` relies on `origin_url` to correctly resolve internal references and build models.
- gotcha If your Swagger/OpenAPI spec defines custom formats (e.g., 'uuid', 'email', 'guid') and these are not registered with `bravado-core` via `SwaggerFormat`, you will see warnings like 'X format is not registered with bravado-core!' This can lead to validation issues or incorrect data handling for these formats.
- deprecated Support for Python 3.5 was dropped in `bravado-core` version 5.17.0. While earlier versions might still work, they are no longer officially supported.
Install
-
pip install bravado-core
Imports
- Spec
from bravado_core.spec import Spec
- SwaggerFormat
from bravado_core.formatter import SwaggerFormat
- SwaggerValidationError
from bravado_core.exception import SwaggerValidationError
Quickstart
import json
from bravado_core.spec import Spec
from bravado_core.validate import validate_object
# Example Swagger 2.0 spec (usually loaded from a file or URL)
swagger_dict = {
"swagger": "2.0",
"info": {"title": "Test API", "version": "1.0.0"},
"paths": {},
"definitions": {
"Pet": {
"type": "object",
"required": ["name"],
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "format": "int32", "minimum": 0}
}
}
}
}
# Load the spec
spec = Spec.from_dict(swagger_dict)
print("Swagger spec loaded successfully.")
# Access a defined model
Pet = spec.definitions['Pet']
my_pet = Pet(name='Rex', age=5)
print(f"Created Pet object: Name={my_pet.name}, Age={my_pet.age}")
# Validate an object against a schema
valid_pet_data = {'name': 'Whiskers', 'age': 2}
try:
validate_object(swagger_spec=spec, json_value=valid_pet_data, swagger_type=spec.definitions['Pet'].swagger_spec)
print(f"Valid pet data: {valid_pet_data} is valid.")
except Exception as e:
print(f"Validation failed for valid data: {e}")
invalid_pet_data = {'name': 'Buddy', 'age': -1}
try:
validate_object(swagger_spec=spec, json_value=invalid_pet_data, swagger_type=spec.definitions['Pet'].swagger_spec)
print("This line should not be reached for invalid data.")
except Exception as e:
print(f"Validation error for invalid pet data: {invalid_pet_data} -> {e}")