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.0.tar.gz (32.7 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.0-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_routechain-0.1.0.tar.gz
  • Upload date:
  • Size: 32.7 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.0.tar.gz
Algorithm Hash digest
SHA256 c0f4793a6048c22e1a407c27296c5a07a59471d298a922626ad69f5df0a3af64
MD5 b42f27aefe0e91a947aba34c71530b6e
BLAKE2b-256 67497fef98a242e0bb0890532b4c295d160a68fe4a2142e53a255072117034ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_routechain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2071b4a629a5d5f82b93dc9d89bc6ce5da2adb1b323c193a75616e781fb91db6
MD5 ece416c962fc964da5325e39b64a5fe8
BLAKE2b-256 00fee0da7abbf6f50e3d6f7cbddd066febd586f275b5b19d05724769832680b5

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