-
-
Notifications
You must be signed in to change notification settings - Fork 556
Open
Description
Describe the bug
Authentication runs very late after view decorators. Take this example:
# main router:
api = NinjaAPI(auth=BearerAuth())
api.add_decorator(activate_tenant, mode="view")
# app-specific endpoint in another file:
@router.post("/report")
@decorate_view(save_request)
async def hello(request, report: Report): ...save_requestis a decorator with the characteristics:- must run before validation of the request (to save possibly malformed requests because of client deficiencies)
- relies on
activate_tenantto set some context variable used for the database router.
activate_tenanton the other hand relies onrequest.authbeing set by the auth handler.activate_tenantfails becauserequest.authis not set, as it runs before the auth handler.
I guess I can work around the issue by not using NinjaAPI auth parameter, instead rolling my own authentication as a view decorator.
However, I would have expected that authentication happens as early as possible in the request flow, before any decorators, to keep the code path short for unauthenticated clients (and of course allow my scenario described above).
Versions (please complete the following information):
- Python version: 3.14
- Django version: 5.2
- Django-Ninja version: 1.5.2
- Pydantic version: 2.12.5
Metadata
Metadata
Assignees
Labels
No labels