From 8368872ccd439dc5089ba0626eb5d691085c4a6d Mon Sep 17 00:00:00 2001 From: Josh Michael Karamuth Date: Wed, 26 Oct 2022 14:22:00 +0400 Subject: [PATCH] Fix subdomain inheritance for nested blueprints. Fixes #4834 --- CHANGES.rst | 2 ++ src/flask/blueprints.py | 9 +++++++++ tests/test_blueprints.py | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 6c3ff32c..5bb26d22 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -9,6 +9,8 @@ Version 2.2.3 Unreleased +- Fix subdomain inheritance for nested blueprints. :issue:`4834` + Version 2.2.2 ------------- diff --git a/src/flask/blueprints.py b/src/flask/blueprints.py index 104f8acf..10c218d5 100644 --- a/src/flask/blueprints.py +++ b/src/flask/blueprints.py @@ -453,6 +453,15 @@ class Blueprint(Scaffold): for blueprint, bp_options in self._blueprints: bp_options = bp_options.copy() bp_url_prefix = bp_options.get("url_prefix") + bp_subdomain = bp_options.get("subdomain") + + if bp_subdomain is None: + bp_subdomain = blueprint.subdomain + + if state.subdomain is not None and bp_subdomain is None: + bp_options["subdomain"] = state.subdomain + elif bp_subdomain is not None: + bp_options["subdomain"] = bp_subdomain if bp_url_prefix is None: bp_url_prefix = blueprint.url_prefix diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 1bf130f5..2081ff23 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -950,6 +950,27 @@ def test_nesting_url_prefixes( assert response.status_code == 200 +def test_nesting_subdomains(app, client) -> None: + subdomain = "api" + parent = flask.Blueprint("parent", __name__) + child = flask.Blueprint("child", __name__) + + @child.route("/child/") + def index(): + return "child" + + parent.register_blueprint(child) + app.register_blueprint(parent, subdomain=subdomain) + + client.allow_subdomain_redirects = True + + domain_name = "domain.tld" + app.config["SERVER_NAME"] = domain_name + response = client.get("/child/", base_url="http://api." + domain_name) + + assert response.status_code == 200 + + def test_unique_blueprint_names(app, client) -> None: bp = flask.Blueprint("bp", __name__) bp2 = flask.Blueprint("bp", __name__)