Skip to content

🐛 Fix custom APIRoute subclasses breaking with strict_content_type#15504

Open
algojogacor wants to merge 6 commits into
fastapi:masterfrom
algojogacor:fix/route-class-strict-content-type-compat
Open

🐛 Fix custom APIRoute subclasses breaking with strict_content_type#15504
algojogacor wants to merge 6 commits into
fastapi:masterfrom
algojogacor:fix/route-class-strict-content-type-compat

Conversation

@algojogacor
Copy link
Copy Markdown

Summary

Fixes #15503

Custom APIRoute subclasses that define an explicit __init__ matching the pre-strict_content_type parameter list crash with TypeError when APIRouter.add_api_route passes strict_content_type as a keyword argument. This regression was introduced when strict_content_type was added to APIRoute.__init__ and propagated through add_api_route.

Root Cause

APIRouter.add_api_route (line 1386 of fastapi/routing.py) unconditionally passes strict_content_type=get_value_or_default(...) to route_class(...). Custom APIRoute subclasses that defined constructors before strict_content_type was added don't accept this keyword, causing a TypeError at route registration time.

Fix

Wrap the route_class(...) call in a try-except that catches TypeError and retries without the strict_content_type keyword argument. This maintains backward compatibility with existing custom route classes while preserving the strict_content_type functionality for default APIRoute instances and subclasses that accept it.

Changes

  • fastapi/routing.py: 12 lines modified (+10 -2) — wrap route_class() call with try-except fallback
  • tests/test_custom_route_class.py: 82 lines added — LegacyAPIRoute class and regression test

Testing

  • Custom route class without strict_content_type in constructor: router registers routes without TypeError
  • Default APIRoute routes: strict_content_type still passed and enforced ✅
  • Existing custom route class tests: all 5 existing tests pass unchanged ✅

algojogacor and others added 2 commits May 12, 2026 16:14
…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
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 12, 2026

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing algojogacor:fix/route-class-strict-content-type-compat (034ef57) with master (5d5666b)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (e89a37e) during the generation of this report, so 5d5666b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@algojogacor
Copy link
Copy Markdown
Author

Fixed ruff C408: replaced dict() call with dict literal {} throughout the route_kwargs block.

@algojogacor
Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom APIRoute classes with explicit constructors fail after strict_content_type was added

1 participant