Fix blueprint nested url_prefix
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This ensures that the url_prefix is correctly applied, no matter if set during the registration override or when constructing the blueprint.
authored by pgjones · committed by pgjones · parent a541c2a · Browse files · 2 files·+28 added−42 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+28 added−42 removed
- tests:+25 added−41 removed
- other:+3 added−1 removed
Provenance
- Commit
99afbb2- Tree
e3de9f4- Author
pgjones <philip.graham.jones@googlemail.com>- Committer
pgjones <philip.graham.jones@googlemail.com>- Parent
a541c2a
2 files changed+28 added−42 removed
modified:src/flask/blueprints.py+3 added-1 removed
352 unmodified lines353 if state.url_prefix is not None and bp_url_prefix is not None:354 bp_options["url_prefix"] = (355 state.url_prefix.rstrip("/") + "/" + bp_url_prefix.lstrip("/")356 )357- else:357+ elif bp_url_prefix is not None:358+ bp_options["url_prefix"] = bp_url_prefix359+ elif state.url_prefix is not None:360 bp_options["url_prefix"] = state.url_prefix361 362 bp_options["name_prefix"] = options.get("name_prefix", "") + self.name + "."363 blueprint.register(app, bp_options)206 unmodified linesmodified:tests/test_blueprints.py+25 added-41 removed
835 unmodified lines836 assert client.get("/parent/child/no").data == b"Parent no"837 assert client.get("/parent/child/grandchild/no").data == b"Grandchild no"838 839 840-def test_nested_blueprint_url_prefix(app, client):841- parent = flask.Blueprint("parent", __name__, url_prefix="/parent")842- child = flask.Blueprint("child", __name__, url_prefix="/child")843- grandchild = flask.Blueprint("grandchild", __name__, url_prefix="/grandchild")844- apple = flask.Blueprint("apple", __name__, url_prefix="/apple")840+@pytest.mark.parametrize(841+ "parent_init, child_init, parent_registration, child_registration",842+ [843+ ("/parent", "/child", None, None),844+ ("/parent", None, None, "/child"),845+ (None, None, "/parent", "/child"),846+ ("/other", "/something", "/parent", "/child"),847+ ],848+)849+def test_nesting_url_prefixes(850+ parent_init,851+ child_init,852+ parent_registration,853+ child_registration,854+ app,855+ client,856+) -> None:857+ parent = flask.Blueprint("parent", __name__, url_prefix=parent_init)858+ child = flask.Blueprint("child", __name__, url_prefix=child_init)859 846- @parent.route("/")847- def parent_index():848- return "Parent"849-860 @child.route("/")851- def child_index():852- return "Child"861+ def index():862+ return "index"863 854- @grandchild.route("/")855- def grandchild_index():856- return "Grandchild"864+ parent.register_blueprint(child, url_prefix=child_registration)865+ app.register_blueprint(parent, url_prefix=parent_registration)866 858- @apple.route("/")859- def apple_index():860- return "Apple"861-862- child.register_blueprint(grandchild)863- child.register_blueprint(apple, url_prefix="/orange") # test overwrite864- parent.register_blueprint(child)865- app.register_blueprint(parent)866-867- assert client.get("/parent/").data == b"Parent"868- assert client.get("/parent/child/").data == b"Child"869- assert client.get("/parent/child/grandchild/").data == b"Grandchild"870- assert client.get("/parent/child/orange/").data == b"Apple"871-872-873-def test_nested_blueprint_url_prefix_only_parent_prefix(app, client):874- parent = flask.Blueprint("parent", __name__)875- child = flask.Blueprint("child", __name__)876-877- @child.route("/child-endpoint")878- def child_index():879- return "Child"880-881- parent.register_blueprint(child)882- app.register_blueprint(parent, url_prefix="/parent")883-884- assert client.get("/parent/child-endpoint").data == b"Child"867+ response = client.get("/parent/child/")868+ assert response.status_code == 200