🐛 Fix custom APIRoute subclasses breaking with strict_content_type#15504
Open
algojogacor wants to merge 6 commits into
Open
🐛 Fix custom APIRoute subclasses breaking with strict_content_type#15504algojogacor wants to merge 6 commits into
algojogacor wants to merge 6 commits into
Conversation
…eter Custom APIRoute subclasses that define an explicit __init__ matching the pre-strict_content_type signature (added in FastAPI 0.118+) crash with TypeError when APIRouter.add_api_route passes strict_content_type as a keyword argument. Fall back gracefully by catching TypeError and retrying without the strict_content_type parameter. This maintains backward compatibility with existing custom route classes that pinned the previously accepted constructor parameters. Fixes fastapi#15503
Author
|
Fixed ruff C408: replaced |
Author
|
Pushed a fix for the pre-commit E402 error: moved the two stray imports (from collections.abc import Callable, Sequence and from typing import Any) from lines 124-125 to the top of the test file alongside the other imports. Ruff should now pass. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #15503
Custom
APIRoutesubclasses that define an explicit__init__matching the pre-strict_content_typeparameter list crash withTypeErrorwhenAPIRouter.add_api_routepassesstrict_content_typeas a keyword argument. This regression was introduced whenstrict_content_typewas added toAPIRoute.__init__and propagated throughadd_api_route.Root Cause
APIRouter.add_api_route(line 1386 offastapi/routing.py) unconditionally passesstrict_content_type=get_value_or_default(...)toroute_class(...). CustomAPIRoutesubclasses that defined constructors beforestrict_content_typewas added don't accept this keyword, causing aTypeErrorat route registration time.Fix
Wrap the
route_class(...)call in a try-except that catchesTypeErrorand retries without thestrict_content_typekeyword argument. This maintains backward compatibility with existing custom route classes while preserving thestrict_content_typefunctionality for defaultAPIRouteinstances and subclasses that accept it.Changes
fastapi/routing.py: 12 lines modified (+10 -2) — wraproute_class()call with try-except fallbacktests/test_custom_route_class.py: 82 lines added —LegacyAPIRouteclass and regression testTesting
strict_content_typein constructor: router registers routes withoutTypeError✅APIRouteroutes:strict_content_typestill passed and enforced ✅