From 3f4663b9f8715119130efe1bfe517f70b356939e Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Mon, 31 Oct 2022 12:58:41 +0200 Subject: [PATCH] Resolve edge case in nested BP Groups (#2592) --- sanic/app.py | 21 ++++++++++----------- tests/test_blueprint_group.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index acda1ad9..41f06580 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -480,17 +480,16 @@ class Sanic(BaseSanic, StartupMixin, metaclass=TouchUpMeta): for item in blueprint: params = {**options} if isinstance(blueprint, BlueprintGroup): - if blueprint.url_prefix: - merge_from = [ - options.get("url_prefix", ""), - blueprint.url_prefix, - ] - if not isinstance(item, BlueprintGroup): - merge_from.append(item.url_prefix or "") - merged_prefix = "/".join( - u.strip("/") for u in merge_from - ).rstrip("/") - params["url_prefix"] = f"/{merged_prefix}" + merge_from = [ + options.get("url_prefix", ""), + blueprint.url_prefix or "", + ] + if not isinstance(item, BlueprintGroup): + merge_from.append(item.url_prefix or "") + merged_prefix = "/".join( + u.strip("/") for u in merge_from if u + ).rstrip("/") + params["url_prefix"] = f"/{merged_prefix}" for _attr in ["version", "strict_slashes"]: if getattr(item, _attr) is None: diff --git a/tests/test_blueprint_group.py b/tests/test_blueprint_group.py index 4321848d..5e793cc1 100644 --- a/tests/test_blueprint_group.py +++ b/tests/test_blueprint_group.py @@ -323,3 +323,20 @@ def test_bp_group_properties(): assert "api/v1/grouped/bp2/" in routes assert "api/v1/primary/grouped/bp1" in routes assert "api/v1/primary/grouped/bp2" in routes + + +def test_nested_bp_group_properties(): + one = Blueprint("one", url_prefix="/one") + two = Blueprint.group(one) + three = Blueprint.group(two, url_prefix="/three") + + @one.route("/four") + def handler(request): + return text("pi") + + app = Sanic("PropTest") + app.blueprint(three) + app.router.finalize() + + routes = [route.path for route in app.router.routes] + assert routes == ["three/one/four"]