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

Url conflict assert #1568

Closed
wants to merge 1 commit into from
Closed

Url conflict assert #1568

wants to merge 1 commit into from

Conversation

rawrgulmuffins
Copy link

@rawrgulmuffins rawrgulmuffins commented Sep 14, 2015

This PR is an attempt to reconcile the fact that you can't have two routes with the same function name but you can create two routes with the same URL.

from flask import Flask
app = Flask("the_flask_module")

@app.route("/hello")
def hello_page():
    return "I'm a hello page"

@app.route("/different_route")
def hello_page():
    return "Different route"

will throw an AssertionError

but

@app.route("/hello")
def end_point1():
    return "I'm a hello page"

@app.route("/hello")
def end_point2():
    return "Different route"

wont. In fact the second route is unreachable.

This PR checks to see if three conditions are true before raising an assertion. The new rule has to have a subdomain, url_path, and one method that overlap with a current rule in the self.url_map object.

There's a couple of wonky pieces in this pull request that were required to get all of the unit tests to pass.

if prior_rule.rule == "/static/<path:filename>":
    continue

was required to get blueprints to not throw an assertion since each blueprint registers a "/static/<path:filename>" rule for each blueprint. I tried to find a way to not have to use a static string but alas I failed.

Secondly

 methods_minus_required = ((rule.methods & prior_rule.methods) 
     - required_methods)

is used to check for all explicitly registered methods for a route since there were a bunch of cases where the OPTIONS method was implicitly added to rules that shared a url_path.


This change is Reviewable

@ThiefMaster
Copy link
Member

I think all these commits could be easily squashed into a single one.

@rawrgulmuffins
Copy link
Author

Done, everything was squashed down to that one commit.

@ThiefMaster
Copy link
Member

I think you have some leftovers from failed merges with upstream in there. Check the diff and remove anything that's not actually related to your PR (and maybe try to switch to a <=51-char first line in the commit message when committing it again)

@@ -1029,6 +1029,27 @@ def index():
rule = self.url_rule_class(rule, methods=methods, **options)
rule.provide_automatic_options = provide_automatic_options

for prior_rule in self.url_map.iter_rules():

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@rawrgulmuffins
Copy link
Author

I rebased against master and re-added my changes. Looks like I missed a couple of wonky space changes, in app.py. I also changed to a hashing check rather than a iteration check.

@rawrgulmuffins
Copy link
Author

The last commit was to add some in-line comments that got dropped when I re-added the changes.

prior_rule_methods = self.prior_rules.get(
prior_rule_check,
None)
# Flask adds non-specified methods to routes. Removing the

This comment was marked as off-topic.

This comment was marked as off-topic.

@davidism davidism requested a review from untitaker April 24, 2017 17:25
@untitaker
Copy link
Contributor

untitaker commented Apr 25, 2017 via email

@untitaker
Copy link
Contributor

I will just close this since it's also unclear to me how a better implementation could look like.

@untitaker untitaker closed this Apr 25, 2017
@ThiefMaster
Copy link
Member

ThiefMaster commented Apr 25, 2017

Probably a "better implementation" should be in Werkzeug, not in Flask. i.e. fail when adding a conflicting rule to a url map

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants