Skip to content

Commit

Permalink
Fix blueprint renaming
Browse files Browse the repository at this point in the history
This ensures that if a blueprint is renamed at the time of
registration that name is used when constructing endpoints, as
expected.
  • Loading branch information
pgjones committed May 21, 2021
1 parent 714b0a4 commit 3257b75
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/flask/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(
#: blueprint.
self.url_prefix = url_prefix

self.name = self.options.get("name", blueprint.name)
self.name_prefix = self.options.get("name_prefix", "")

#: A dictionary with URL defaults that is added to each and every
Expand Down Expand Up @@ -96,9 +97,10 @@ def add_url_rule(
defaults = self.url_defaults
if "defaults" in options:
defaults = dict(defaults, **options.pop("defaults"))

self.app.add_url_rule(
rule,
f"{self.name_prefix}.{self.blueprint.name}.{endpoint}".lstrip("."),
f"{self.name_prefix}.{self.name}.{endpoint}".lstrip("."),
view_func,
defaults=defaults,
**options,
Expand Down
22 changes: 22 additions & 0 deletions tests/test_blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,3 +889,25 @@ def test_self_registration(app, client) -> None:
bp = flask.Blueprint("bp", __name__)
with pytest.raises(ValueError):
bp.register_blueprint(bp)


def test_blueprint_renaming(app, client) -> None:
bp = flask.Blueprint("bp", __name__)
bp2 = flask.Blueprint("bp2", __name__)

@bp.get("/")
def index():
return flask.request.endpoint

@bp2.get("/")
def index2():
return flask.request.endpoint

bp.register_blueprint(bp2, url_prefix="/a", name="sub")
app.register_blueprint(bp, url_prefix="/a")
app.register_blueprint(bp, url_prefix="/b", name="alt")

assert client.get("/a/").data == b"bp.index"
assert client.get("/b/").data == b"alt.index"
assert client.get("/a/a/").data == b"bp.sub.index2"
assert client.get("/b/a/").data == b"alt.sub.index2"

0 comments on commit 3257b75

Please sign in to comment.