Skip to content

Commit eb30d8a

Browse files
authored
REST: Stop double-encoding the parent namespace in list_namespaces (#4112)
list_namespaces passes the parent namespace through requests' params, which URL-encodes query values. The namespace parts were already quoted with _encode_namespace_path, so any part containing a space, '/', '%' or non-ASCII character was encoded twice and the server received e.g. "my%20namespace" instead of "my namespace". Join the raw parts with the namespace separator instead, matching RESTUtil.namespaceToQueryParam in the Java client.
1 parent 208fd51 commit eb30d8a

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎pyiceberg/catalog/rest/__init__.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,8 @@ def list_namespaces(self, namespace: str | Identifier = ()) -> list[Identifier]:
16761676

16771677
while True:
16781678
if namespace_tuple:
1679-
params["parent"] = self._encode_namespace_path(namespace_tuple)
1679+
# Query parameters are URL-encoded by requests, so the parts must not be quoted here as well
1680+
params["parent"] = self._namespace_separator.join(namespace_tuple)
16801681
if page_token:
16811682
params["pageToken"] = page_token
16821683
response = self._session.get(self.url(Endpoints.list_namespaces), params=params)

‎tests/catalog/test_rest.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from contextlib import contextmanager
2424
from typing import Any, cast
2525
from unittest import mock
26+
from urllib.parse import parse_qs, urlsplit
2627

2728
import pytest
2829
from requests import Request
@@ -972,6 +973,42 @@ def test_list_namespace_with_parent_200(rest_mock: Mocker) -> None:
972973
]
973974

974975

976+
@pytest.mark.parametrize(
977+
"parent",
978+
[
979+
("accounting",),
980+
("accounting", "tax"),
981+
("my namespace",),
982+
("dept/finance", "tax"),
983+
("100%",),
984+
("\u65e5\u672c",),
985+
],
986+
)
987+
def test_list_namespace_with_parent_is_encoded_once(rest_mock: Mocker, parent: tuple[str, ...]) -> None:
988+
rest_mock.get(
989+
f"{TEST_URI}v1/namespaces",
990+
json={"namespaces": [[*parent, "child"]]},
991+
status_code=200,
992+
request_headers=TEST_HEADERS,
993+
)
994+
assert RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN).list_namespaces(parent) == [(*parent, "child")]
995+
query = parse_qs(urlsplit(rest_mock.last_request.url).query)
996+
assert query["parent"] == ["\x1f".join(parent)]
997+
998+
999+
def test_list_namespace_with_parent_custom_separator(rest_mock: Mocker) -> None:
1000+
rest_mock.get(
1001+
f"{TEST_URI}v1/namespaces",
1002+
json={"namespaces": [["my namespace", "tax", "child"]]},
1003+
status_code=200,
1004+
request_headers=TEST_HEADERS,
1005+
)
1006+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN, **{"namespace-separator": "."})
1007+
assert catalog.list_namespaces(("my namespace", "tax")) == [("my namespace", "tax", "child")]
1008+
query = parse_qs(urlsplit(rest_mock.last_request.url).query)
1009+
assert query["parent"] == ["my namespace.tax"]
1010+
1011+
9751012
def test_list_namespaces_paginated_200(rest_mock: Mocker) -> None:
9761013
# First page with next-page-token
9771014
rest_mock.get(

0 commit comments

Comments
 (0)