Skip to content
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

Fix @crossdomain decorator #3113

Merged
merged 2 commits into from
Jan 9, 2025
Merged

Fix @crossdomain decorator #3113

merged 2 commits into from
Jan 9, 2025

Conversation

amCap1712
Copy link
Member

When handling OPTIONS requests, Flask can either generate an automatic response internally or route the request to the view function for handling. The request is routed to the view function if the view function has a provides_automatic_options attribute set to False or if the methods list passed to route definition includes OPTIONS. OPTIONS requests handled by Flask internally don't have CORS headers. Hence, API endpoints that need to support CORS should ensure that the OPTIONS request is routed to the view function.

The https://github.com/crossdomain decorator sets the provides_automatic_options attribute on the view function which should have ensured this. However, due to the way decorators work the attribute should have instead been set on the decorator function itself for Flask to pick it up. However, we often list OPTIONS in the methods list of the route anyway when applying https://github.com/crossdomain. As a result, Flask would still route the request as expected. The errors would only happen when the OPTIONS method was not mentioned in the route's methods list.

https://github.com/crossdomain decorator has now been fixed to set the attribute correctly and also add OPTIONS to the methods list automatically. Further, a test has been added to ensure that all API endpoints support CORS.

While at it, also refactor the views to use newer .get/.post decorators insteads of methods list.

@pep8speaks
Copy link

pep8speaks commented Jan 5, 2025

Hello @amCap1712! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 24:5: E303 too many blank lines (2)

Line 129:1: E303 too many blank lines (3)

Line 301:1: E303 too many blank lines (3)

Line 337:1: E302 expected 2 blank lines, found 1

Comment last updated at 2025-01-09 18:25:56 UTC

@amCap1712
Copy link
Member Author

As this touches almost all views, deployed on beta for testing for a while.

@amCap1712 amCap1712 requested a review from mayhem January 8, 2025 09:05
Copy link
Member

@mayhem mayhem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

Base automatically changed from catch-all-path-fix to master January 9, 2025 18:24
When handling OPTIONS requests, Flask can either generate an automatic response internally
or route the request to the view function for handling. The request is routed to the view
function if the view function has a provides_automatic_options attribute set to False or
if the methods list passed to route definition includes OPTIONS. OPTIONS requests
handled by Flask internally don't have CORS headers. Hence, API endpoints that need to
support CORS should ensure that the OPTIONS request is routed to the view function.

The @crossdomain decorator sets the provides_automatic_options attribute on the view
function which should have ensured this. However, due to the way decorators work the
attribute should have instead been set on the decorator function itself for Flask to
pick it up. However, we often list OPTIONS in the methods list of the route anyway
when applying @crossdomain. As a result, Flask would still route the request as expected.
The errors would only happen when the OPTIONS method was not mentioned in the route's
methods list.

@crossdomain decorator has now been fixed to set the attribute correctly and also add
OPTIONS to the methods list automatically. Further, a test has been added to ensure that
all API endpoints support CORS.
@amCap1712 amCap1712 merged commit 635b5d1 into master Jan 9, 2025
2 checks passed
@amCap1712 amCap1712 deleted the update-decorator branch January 9, 2025 18:31
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.

3 participants