Skip to content

Add route-level middleware decorator with tests and docs example#14782

Draft
VashuTheGreat wants to merge 2 commits intofastapi:masterfrom
VashuTheGreat:feature/route-level-middleware
Draft

Add route-level middleware decorator with tests and docs example#14782
VashuTheGreat wants to merge 2 commits intofastapi:masterfrom
VashuTheGreat:feature/route-level-middleware

Conversation

@VashuTheGreat
Copy link

✨ Add route-level middleware decorator

This PR adds a new route_middleware decorator that allows attaching middleware logic directly to specific routes.

Features

  • Route-specific middleware execution
  • Supports sync & async middleware functions
  • Clean decorator-based API
  • Does not affect global middleware behavior

Added

  • Core implementation in fastapi/route_middleware.py
  • Full test coverage in tests/test_route_middleware.py
  • Documentation example in docs_src/route_middleware/tutorial001.py

Why

FastAPI currently supports only application-level middleware. This feature enables fine-grained, per-route middleware use cases such as:

  • Auth checks
  • Logging
  • Rate limiting
  • Feature flags

All tests pass locally.

@VashuTheGreat
Copy link
Author

✨ Add route-level middleware decorator

This PR adds a new route_middleware decorator that allows attaching middleware logic directly to specific routes.

Features

  • Route-specific middleware execution
  • Supports sync & async middleware functions
  • Clean decorator-based API
  • Does not affect global middleware behavior

Added

  • Core implementation in fastapi/route_middleware.py
  • Full test coverage in tests/test_route_middleware.py
  • Documentation example in docs_src/route_middleware/tutorial001.py

Why

FastAPI currently supports only application-level middleware. This feature enables fine-grained, per-route middleware use cases such as:

  • Auth checks
  • Logging
  • Rate limiting
  • Feature flags

All tests pass locally.

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 29, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing VashuTheGreat:feature/route-level-middleware (7cd6cb6) with master (a3dccae)1

Summary

✅ 20 untouched benchmarks

Footnotes

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

@github-actions
Copy link
Contributor

📝 Docs preview

Last commit 7cd6cb6 at: https://8ed98d8b.fastapitiangolo.pages.dev

@VashuTheGreat
Copy link
Author

✨ Add route-level middleware decorator

This PR adds a new route_middleware decorator that allows attaching middleware logic directly to specific routes.

Features

  • Route-specific middleware execution
  • Supports sync & async middleware functions
  • Clean decorator-based API
  • Does not affect global middleware behavior

Added

  • Core implementation in fastapi/route_middleware.py
  • Full test coverage in tests/test_route_middleware.py
  • Documentation example in docs_src/route_middleware/tutorial001.py

Why

FastAPI currently supports only application-level middleware. This feature enables fine-grained, per-route middleware use cases such as:

  • Auth checks
  • Logging
  • Rate limiting
  • Feature flags

All tests pass locally.

@YuriiMotov YuriiMotov marked this pull request as draft January 29, 2026 14:48
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.

1 participant