From 6fbdeb80c76b8a698ab366d21ccd786731b17361 Mon Sep 17 00:00:00 2001 From: pgjones Date: Sun, 16 May 2021 19:32:29 +0100 Subject: [PATCH] Fix nested blueprint url_prefix This fixes the case where the blueprint is registered with a url_prefix but any child blueprints have no prefixes. --- src/flask/blueprints.py | 2 ++ tests/test_blueprints.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/flask/blueprints.py b/src/flask/blueprints.py index 7bfef84b..39396ce7 100644 --- a/src/flask/blueprints.py +++ b/src/flask/blueprints.py @@ -354,6 +354,8 @@ class Blueprint(Scaffold): bp_options["url_prefix"] = ( state.url_prefix.rstrip("/") + "/" + bp_url_prefix.lstrip("/") ) + else: + bp_options["url_prefix"] = state.url_prefix bp_options["name_prefix"] = options.get("name_prefix", "") + self.name + "." blueprint.register(app, bp_options) diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index e7724519..0bae5333 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -868,3 +868,17 @@ def test_nested_blueprint_url_prefix(app, client): assert client.get("/parent/child/").data == b"Child" assert client.get("/parent/child/grandchild/").data == b"Grandchild" assert client.get("/parent/child/orange/").data == b"Apple" + + +def test_nested_blueprint_url_prefix_only_parent_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) + app.register_blueprint(parent, url_prefix="/parent") + + assert client.get("/parent/child-endpoint").data == b"Child"