-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Add validation for decorator order with @api_view #9821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add validation for decorator order with @api_view #9821
Conversation
Raise TypeError when API policy decorators (@permission_classes, @renderer_classes, etc.) are applied after @api_view instead of before it. Fixes encode#9596
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds validation to ensure API policy decorators are applied in the correct order relative to the @api_view decorator. The feature prevents a common mistake where developers apply policy decorators after @api_view, which silently fails to work as expected.
- Introduced
_check_decorator_order()helper function to detect incorrect decorator ordering - Added order validation to all nine API policy decorators
- Comprehensive test coverage for all affected decorators
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| rest_framework/decorators.py | Added _check_decorator_order() validation function and integrated it into all API policy decorators to raise TypeError when applied after @api_view |
| tests/test_decorators.py | Added nine test cases to verify that each API policy decorator raises TypeError when incorrectly applied after @api_view |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Change 'must be applied before' to 'must come after (below) the' to match DRF docs language - Fix decorator order in example to show @api_view first, then policy decorator below - Remove unnecessary f-string prefixes from non-interpolated lines - Update all test assertions to match new error message wording Addresses feedback from @browniebroke in PR encode#9821
Fixes #9596
This PR adds validation to detect when API policy decorators (
@permission_classes,@renderer_classes, etc.) are applied in the wrong order with@api_view.Problem
When policy decorators are applied after
@api_view(i.e., placed above it in code), the policy settings are silently ignored. This happens because:@api_view, they set attributes on the already-created view function, not the original functionSolution
Added a
_check_decorator_order()helper that:APIView.as_view())TypeErrorwith the correct decorator orderTests
Added 9 comprehensive tests covering all policy decorators to ensure they properly validate ordering.