Skip to main content

Combine multiple APIRoute subclasses for FastAPI with conflict detection

Project description

fastapi-routechain

Combine multiple APIRoute subclasses for FastAPI — with automatic conflict detection.

Installation

pip install fastapi-routechain

The problem

FastAPI's APIRouter accepts only a single route_class. If you want to use multiple third-party route classes together (e.g. DishkaRoute for DI and XmlRoute for XML support), you'd have to manually create a combined class.

Usage

ChainedAPIRouter — drop-in replacement for APIRouter

from fastapi_routechain import ChainedAPIRouter
from dishka.integrations.fastapi import DishkaRoute
from fastapi_xml import XmlRoute

router = ChainedAPIRouter(
    route_classes=[DishkaRoute, XmlRoute],  # priority = list order (MRO)
)

@router.get("/hello")
def hello():
    return {"hello": "world"}

combine_routes — just the factory

from fastapi import APIRouter
from fastapi_routechain import combine_routes

MyRoute = combine_routes(DishkaRoute, XmlRoute)

router = APIRouter(route_class=MyRoute)

Priority and MRO

The order of classes in route_classes is the priority order — it directly maps to Python's MRO (Method Resolution Order). The first class wins on conflicts.

# DishkaRoute.__init__ takes priority over XmlRoute.__init__
combine_routes(DishkaRoute, XmlRoute)

Conflict detection

fastapi-routechain warns you when combining classes that will silently lose functionality — e.g. two classes that both override get_route_handler without calling super().

# emits RouteConflictWarning
combine_routes(XmlRoute, SomeOtherHandlerRoute)

# raises RouteConflictError instead
combine_routes(XmlRoute, SomeOtherHandlerRoute, strict=True)

You can also check conflicts manually:

from fastapi_routechain import check_conflicts

issues = check_conflicts(RouteA, RouteB, RouteC)
for issue in issues:
    print(issue)

API

Symbol Description
ChainedAPIRouter APIRouter subclass accepting route_classes list
combine_routes(*classes, strict=False) Returns a combined APIRoute subclass
check_conflicts(*classes) Returns list of conflict description strings
RouteConflictWarning Warning class emitted on soft conflicts
RouteConflictError Exception raised in strict mode

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

fastapi_routechain-0.1.1.tar.gz (32.8 kB view details)

Uploaded Source

Built Distribution

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

fastapi_routechain-0.1.1-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_routechain-0.1.1.tar.gz
  • Upload date:
  • Size: 32.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastapi_routechain-0.1.1.tar.gz
Algorithm Hash digest
SHA256 edcf6859e5044e695b80264f4a668165f7cd3126ec5f4264c4afcdc561beb67c
MD5 987783c44b4dea084e33d7ef48a1e139
BLAKE2b-256 23b3846c3039445eec23e352dfae0495deaa77476f623e3452d1c7dac12bf45b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_routechain-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eed6c68b66d53b2f4e71f967f5b1c234613e7568ccaafec6f071a2c3bbe89062
MD5 ad04725b5a74347e82b76df735dedb58
BLAKE2b-256 f276e84851438e95c57563da958aec09511198ab757e681844e8a66e0fc5f4b2

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