Skip to main content

Fail tests if APIs do not have input or output models

Project description

Bridgekeeper

Fail tests if API's do not have input or output models

Supports:

  • FastAPI
  • Flask

Usage

You can use Bridgekeeper to validate your API endpoints and ensure they have explicitly typed input and output models.

Basic Example

from fastapi import FastAPI
from bridgekeeper import check_models

app = FastAPI()

@app.get("/items/{item_id}")
def read_item(item_id: int) -> dict:
    return {"item_id": item_id}

# Run the checker against your initialized app
results = check_models(app)

# `results` will contain a list of endpoints missing type models
print(results)

Options

check_models(app, allow_list=None, check_only=None)

  • allow_list (list of strings, optional): A list of API paths to ignore (e.g., ["/health", "/metrics"]). If an endpoint matches a path in the list, it is skipped.
  • check_only (literal string, optional): If you only want to validate inputs or outputs exclusively, pass "request" or "response".
  • allow_any (boolean, optional): Defaults to False. When False, explicitly typing a parameter or return type as typing.Any is flagged as missing a strict model. Set to True if you want to allow Any as a valid type hint.
results = check_models(
    app, 
    allow_list=["/health"], 
    check_only="response",  # Will strictly look for missing return types
    allow_any=False         # typing.Any will be rejected
)

Handling Complex Apps (Databases/Secrets)

If your FastAPI or Flask app connects to a database (like Postgres) or external service (like Redis) during initialization, trying to run bridgekeeper in a GitHub Action might crash when the app imports, before the tests even run.

Bridgekeeper provides generic mocking utilities to safely bypass these side effects. For highly complex apps where you don't want to list out every single dependency, you can use auto_mock_missing=True to seamlessly mock any module that isn't installed!

from bridgekeeper import mock_modules, mock_env

# 1. Mock critical environment variables
mock_env({
    "DATABASE_URL": "sqlite:///:memory:",
    "SECRET_KEY": "dummy_secret_for_ci"
})

# 2. Automatically mock ANY missing dependency!
# This is extremely useful if you don't want to install heavy libraries (like SQLAlchemy, boto3, etc.)
# just to run a quick static analysis check.
mock_modules(auto_mock_missing=True)

# You can also explicitly mock specific modules with custom fakes:
# mock_modules({"app.core.db": PostgresTestDb}, auto_mock_missing=True)

# 3. Now it is safe to import the app
from myapp.main import app
from bridgekeeper import check_models

results = check_models(app)

Why the name bridgekeeper?

He guards the Bridge of Death and requires travelers to answer "questions three" before crossing safely.

Project details


Download files

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

Source Distribution

api_bridgekeeper-0.1.1.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

api_bridgekeeper-0.1.1-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file api_bridgekeeper-0.1.1.tar.gz.

File metadata

  • Download URL: api_bridgekeeper-0.1.1.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for api_bridgekeeper-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6dffec38776ab7c65f1e90f33c7d855c2605bfc7fa630d548b228ee53795b978
MD5 50f77efe5f5719904598882ad0bca375
BLAKE2b-256 7686949c418af8507fae7a115ad53c4bf050adfa9c9324a30b13d00856eeb908

See more details on using hashes here.

File details

Details for the file api_bridgekeeper-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for api_bridgekeeper-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ea05027b620c3022b9028c314c422d2a6abc0cd3b9a07301c52cae702d432df9
MD5 0fa108fda4de4caec2da2b6a9b5940ce
BLAKE2b-256 8bd16fed3640200608cce9c99a4844dda85166bfe59188819dad44f63a733d76

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