Skip to content

Commit

Permalink
Resolve edge case in nested BP Groups (#2592)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins authored Oct 31, 2022
1 parent 65d7447 commit 3f4663b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
21 changes: 10 additions & 11 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,17 +480,16 @@ def blueprint(
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:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_blueprint_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,20 @@ def blueprint_2_default_route(request):
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"]

0 comments on commit 3f4663b

Please sign in to comment.