Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Bug Fixes
::

* Adjust field tests for Python 3.14 (``staticmethod`` around ``functools.partial`` used as a class attribute). [python-restx]
* ``Api.add_namespace(ns, path='/')`` (or any path ending in ``/``) no longer produces double-slash rules in the URL map (#74) [jbbqqf]

.. _section-1.3.1:
1.3.1
Expand Down
5 changes: 5 additions & 0 deletions flask_restx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ def get_ns_path(self, ns):

def ns_urls(self, ns, urls):
path = self.get_ns_path(ns) or ns.path
# Naive `path + url` would produce double slashes when path ends
# in "/" (or is just "/") and url starts with "/". This is the
# documented usage for mounting a namespace at the API root
# (#74), so normalise the join instead of rejecting it.
path = path.rstrip("/")
return [path + url for url in urls]

def add_namespace(self, ns, path=None):
Expand Down
30 changes: 30 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,36 @@ class TestResource(restx.Resource):
with app.test_request_context():
assert url_for("test_resource") == "/api_test/test/"

def test_ns_path_no_double_slash(self, app):
# add_namespace(ns, path=...) used to do a naive `path + url` join,
# so passing path="/" produced rules like "//foo" (see #74), and
# "/api/" similarly produced "/api//foo". Normalise the join so
# neither shape leaks into the URL map.
api = restx.Api()

ns_root = restx.Namespace("ns_root")

@ns_root.route("/foo", endpoint="ns_root_foo")
class FooRoot(restx.Resource):
pass

ns_trailing = restx.Namespace("ns_trailing")

@ns_trailing.route("/foo", endpoint="ns_trailing_foo")
class FooTrailing(restx.Resource):
pass

api.add_namespace(ns_root, path="/")
api.add_namespace(ns_trailing, path="/api/")
api.init_app(app)

rule_paths = sorted(str(r) for r in app.url_map.iter_rules())
for r in rule_paths:
assert "//" not in r, f"rule has double slash: {r!r}"
with app.test_request_context():
assert url_for("ns_root_foo") == "/foo"
assert url_for("ns_trailing_foo") == "/api/foo"

def test_multiple_ns_with_authorizations(self, app):
api = restx.Api()
a1 = {"apikey": {"type": "apiKey", "in": "header", "name": "X-API"}}
Expand Down