Resolve edge case in nested BP Groups (#2592)
This commit is contained in:
parent
65d7447cf6
commit
3f4663b9f8
21
sanic/app.py
21
sanic/app.py
@ -480,17 +480,16 @@ class Sanic(BaseSanic, StartupMixin, metaclass=TouchUpMeta):
|
|||||||
for item in blueprint:
|
for item in blueprint:
|
||||||
params = {**options}
|
params = {**options}
|
||||||
if isinstance(blueprint, BlueprintGroup):
|
if isinstance(blueprint, BlueprintGroup):
|
||||||
if blueprint.url_prefix:
|
merge_from = [
|
||||||
merge_from = [
|
options.get("url_prefix", ""),
|
||||||
options.get("url_prefix", ""),
|
blueprint.url_prefix or "",
|
||||||
blueprint.url_prefix,
|
]
|
||||||
]
|
if not isinstance(item, BlueprintGroup):
|
||||||
if not isinstance(item, BlueprintGroup):
|
merge_from.append(item.url_prefix or "")
|
||||||
merge_from.append(item.url_prefix or "")
|
merged_prefix = "/".join(
|
||||||
merged_prefix = "/".join(
|
u.strip("/") for u in merge_from if u
|
||||||
u.strip("/") for u in merge_from
|
).rstrip("/")
|
||||||
).rstrip("/")
|
params["url_prefix"] = f"/{merged_prefix}"
|
||||||
params["url_prefix"] = f"/{merged_prefix}"
|
|
||||||
|
|
||||||
for _attr in ["version", "strict_slashes"]:
|
for _attr in ["version", "strict_slashes"]:
|
||||||
if getattr(item, _attr) is None:
|
if getattr(item, _attr) is None:
|
||||||
|
@ -323,3 +323,20 @@ def test_bp_group_properties():
|
|||||||
assert "api/v1/grouped/bp2/" in routes
|
assert "api/v1/grouped/bp2/" in routes
|
||||||
assert "api/v1/primary/grouped/bp1" in routes
|
assert "api/v1/primary/grouped/bp1" in routes
|
||||||
assert "api/v1/primary/grouped/bp2" 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"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user