From f7a3bdc6db7aefeeb974c0b9800bbe64d152f842 Mon Sep 17 00:00:00 2001 From: Hsiaoming Yang Date: Tue, 1 May 2018 14:35:55 +0900 Subject: [PATCH 1/2] Fix blueprint route for "" --- flask/blueprints.py | 5 ++++- tests/test_blueprints.py | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/flask/blueprints.py b/flask/blueprints.py index 3bdb3d9e32..74bca275a7 100644 --- a/flask/blueprints.py +++ b/flask/blueprints.py @@ -66,7 +66,10 @@ def add_url_rule(self, rule, endpoint=None, view_func=None, **options): blueprint's name. """ if self.url_prefix is not None: - rule = '/'.join((self.url_prefix, rule.lstrip('/'))) + if rule: + rule = '/'.join((self.url_prefix, rule.lstrip('/'))) + else: + rule = self.url_prefix options.setdefault('subdomain', self.subdomain) if endpoint is None: endpoint = _endpoint_from_view_func(view_func) diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 46364cab4d..311d77f64d 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -116,6 +116,7 @@ def bp_forbidden(): @pytest.mark.parametrize(('prefix', 'rule', 'url'), ( + ('/foo', '', '/foo'), ('/foo/', '/bar', '/foo/bar'), ('/foo/', 'bar', '/foo/bar'), ('/foo', '/bar', '/foo/bar'), From b21b4d1608f5d83d7d6f503672f7eff46ed28f95 Mon Sep 17 00:00:00 2001 From: David Lord Date: Tue, 1 May 2018 13:29:48 -0700 Subject: [PATCH 2/2] add more merge cases --- flask/blueprints.py | 6 +++--- tests/test_blueprints.py | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/flask/blueprints.py b/flask/blueprints.py index 74bca275a7..5ce5561e4e 100644 --- a/flask/blueprints.py +++ b/flask/blueprints.py @@ -10,6 +10,7 @@ :license: BSD, see LICENSE for more details. """ from functools import update_wrapper +from werkzeug.urls import url_join from .helpers import _PackageBoundObject, _endpoint_from_view_func @@ -49,8 +50,6 @@ def __init__(self, blueprint, app, options, first_registration): url_prefix = self.options.get('url_prefix') if url_prefix is None: url_prefix = self.blueprint.url_prefix - if url_prefix: - url_prefix = url_prefix.rstrip('/') #: The prefix that should be used for all URLs defined on the #: blueprint. self.url_prefix = url_prefix @@ -67,7 +66,8 @@ def add_url_rule(self, rule, endpoint=None, view_func=None, **options): """ if self.url_prefix is not None: if rule: - rule = '/'.join((self.url_prefix, rule.lstrip('/'))) + rule = '/'.join(( + self.url_prefix.rstrip('/'), rule.lstrip('/'))) else: rule = self.url_prefix options.setdefault('subdomain', self.subdomain) diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 311d77f64d..465079cdd9 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -116,7 +116,12 @@ def bp_forbidden(): @pytest.mark.parametrize(('prefix', 'rule', 'url'), ( + ('', '/', '/'), + ('/', '', '/'), + ('/', '/', '/'), ('/foo', '', '/foo'), + ('/foo/', '', '/foo/'), + ('', '/bar', '/bar'), ('/foo/', '/bar', '/foo/bar'), ('/foo/', 'bar', '/foo/bar'), ('/foo', '/bar', '/foo/bar'),