werkzeug icon indicating copy to clipboard operation
werkzeug copied to clipboard

Adding the same route twice will silently fail, should raise an exception instead

Open 502E532E opened this issue 8 months ago • 2 comments

Flask does not warn about adding the same route twice. It will continue to use the view function from the first registration and just silently fail on a second registration.

Desired behavior: Similar to adding the same endpoint twice, some exception should be raised.

import flask

app = flask.Flask(__name__)

@app.route("/test")
def myfunc1():
    return "myfunc1"

@app.route("/test") # This should raise an exception
def myfunc2():
    return "myfunc2"

app.add_url_rule("/test", "my test endpoint", lambda: "myfunc3") # This, too

cli = app.test_client()
response = cli.get("/test")
data = response.get_data(True)
print(data) # Prints "myfunc1"

Environment:

  • Python version: 3.13.2
  • Flask version: 3.1.0

502E532E avatar Apr 11 '25 21:04 502E532E

@pgjones Perhaps this can be detected in StateMachineMatcher.add? Before appending rule to state.rules at the end, we can check if the new rule's methods overlap with any existing rules. That might be problematic though, as OPTIONS and HEAD are always added. I'm also not sure if that's a sufficient check because there are other attributes that affects the match that don't factor in when building the state machine, including websocket, strict_slashes, and merge_slashes. Overall, I'm uncertain whether it's possible to know at registration time whether two arbitrary rules will be equivalent at match time.

davidism avatar Apr 12 '25 14:04 davidism

#3038 is a potential fix - feedback welcome

pgjones avatar Apr 18 '25 16:04 pgjones