Skip to main content

API Contract Guard

api-contract-guard detects breaking changes between an approved JSON API response and a new response.

Installation

pip install api-contract-guard

Python usage

from api_contract_guard import compare, to_text

expected = {
    "id": "123",
    "portNumber": 104,
    "isSelected": True,
}

actual = {
    "uuid": "123",
    "port": "104",
}

report = compare(expected, actual)
print(to_text(report))

if report.has_breaking_changes:
    raise SystemExit(1)

CLI usage

api-contract-guard examples/expected.json examples/actual.json

Fail a CI job when a breaking change is detected:

api-contract-guard \
  examples/expected.json \
  examples/actual.json \
  --fail-on-breaking

JSON output:

api-contract-guard expected.json actual.json --format json

Ignore dynamic fields:

api-contract-guard expected.json actual.json \
  --ignore modified_at \
  --ignore metadata.request_id

integrate with your python code

from api_contract_guard import compare, to_text

expected = {
    "id": "123",
    "portNumber": 104,
    "isSelected": True,
    "server": {
        "name": "Primary PACS",
    },
}

actual = {
    "uuid": "123",
    "port": "104",
    "server": {
        "name": "Primary PACS",
    },
}

report = compare(expected, actual)

print(to_text(report))

if report.has_breaking_changes:
    print("API contains breaking changes")

Current comparison rules

  • Removed fields are breaking.
  • Type changes are breaking.
  • Added fields are non-breaking by default.
  • Nested dictionaries and list items are compared.
  • Dynamic paths can be ignored.
  • Value comparison and list-length comparison are optional.

Development

python -m venv .venv
source .venv/bin/activate
python -m pip install -U pip
pip install -e ".[dev]"
pytest
ruff check .
python -m build
python -m twine check dist/*

Licence

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

api_contract_guard-0.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

api_contract_guard-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file api_contract_guard-0.1.0.tar.gz.

File metadata

  • Download URL: api_contract_guard-0.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for api_contract_guard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 420f8740ef874a7e509e273c562155a45163378c813b1e75d3dedd72e453176c
MD5 8a904c9d27588f6cbc0b10841bcfc165
BLAKE2b-256 8fd76dcd7ce456556a66a4e98e1ce17a160950e2b48423337166c1663f94999b

See more details on using hashes here.

File details

Details for the file api_contract_guard-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for api_contract_guard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f6a031f934073121cfc196c430469394c0c3c572cc12c0b8cafca6adf96b987
MD5 bce9ac2e3de3d52bee73f8c335aedcbd
BLAKE2b-256 dbbe6e10d0094baa4b47adcb2b0717d4a71084a89f0c9d49bcb31fde6d6f6aab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page