From ecd6f7649655d607c4f90d2e4973e1347136c4e7 Mon Sep 17 00:00:00 2001 From: Maico Timmerman Date: Mon, 17 May 2021 21:25:57 +0200 Subject: [PATCH] blueprints: fix nested blueprint url_prefix The url_prefix would only be propagated if the parent blueprint or overwrite had an url_prefix. Fixes #4062 --- src/flask/blueprints.py | 4 ++- tests/test_blueprints.py | 54 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/flask/blueprints.py b/src/flask/blueprints.py index 39396ce7..9f236c5d 100644 --- a/src/flask/blueprints.py +++ b/src/flask/blueprints.py @@ -354,7 +354,9 @@ class Blueprint(Scaffold): bp_options["url_prefix"] = ( state.url_prefix.rstrip("/") + "/" + bp_url_prefix.lstrip("/") ) - else: + elif bp_url_prefix is not None: + bp_options["url_prefix"] = bp_url_prefix + elif state.url_prefix is not None: bp_options["url_prefix"] = state.url_prefix bp_options["name_prefix"] = options.get("name_prefix", "") + self.name + "." diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 0bae5333..c03f32fa 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -870,7 +870,7 @@ def test_nested_blueprint_url_prefix(app, client): assert client.get("/parent/child/orange/").data == b"Apple" -def test_nested_blueprint_url_prefix_only_parent_prefix(app, client): +def test_nested_blueprint_url_prefix_only_parent_overwrite_prefix(app, client): parent = flask.Blueprint("parent", __name__) child = flask.Blueprint("child", __name__) @@ -882,3 +882,55 @@ def test_nested_blueprint_url_prefix_only_parent_prefix(app, client): app.register_blueprint(parent, url_prefix="/parent") assert client.get("/parent/child-endpoint").data == b"Child" + + +def test_nested_blueprint_url_prefix_only_child_overwrite_prefix(app, client): + parent = flask.Blueprint("parent", __name__) + child = flask.Blueprint("child", __name__) + + @child.route("/child-endpoint") + def child_index(): + return "Child" + + parent.register_blueprint(child, url_prefix="/child") + app.register_blueprint(parent) + + assert client.get("/child/child-endpoint").data == b"Child" + + +def test_nested_blueprint_url_prefix_only_child_prefix(app, client): + parent = flask.Blueprint("parent", __name__, url_prefix="/parent") + child = flask.Blueprint("child", __name__) + + @parent.route("/") + def parent_index(): + return "Parent" + + @child.route("/child/") + def child_index(): + return "Child" + + parent.register_blueprint(child) + app.register_blueprint(parent) + + assert client.get("/parent/").data == b"Parent" + assert client.get("/parent/child/").data == b"Child" + + +def test_nested_blueprint_url_prefix_only_parent_prefix(app, client): + parent = flask.Blueprint("parent", __name__) + child = flask.Blueprint("child", __name__, url_prefix="/child") + + @parent.route("/parent/") + def parent_index(): + return "Parent" + + @child.route("/") + def child_index(): + return "Child" + + parent.register_blueprint(child) + app.register_blueprint(parent) + + assert client.get("/parent/").data == b"Parent" + assert client.get("/child/").data == b"Child"