Skip to content

Commit c8502b3

Browse files
kevinjqliuCopilot
andauthored
REST: Don't URL-encode view names in create_view and register_view bodies (#4151)
* REST: Send raw view name in create_view and register_view bodies create_view and register_view took the view name from _split_identifier_for_path, which URL-quotes it for use as a path segment, and put it in the JSON request body. A view named "my view" was created as "my%20view". Use the raw name, as create_table and register_table already do. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Test REST identifiers that need URL encoding against the REST fixture Round-trip namespace, table, and view names containing spaces, reserved URL characters, and non-ASCII text through path, query, and body parameters. This catches encoding mistakes like the double-encoded list_namespaces parent and the encoded view name in create_view. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Cover commit_table in REST identifier integration test commit_table is the only caller of the TableIdentifier branch of _split_identifier_for_path, so table updates had no coverage for names that need URL encoding. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Clarify why % is omitted from REST identifier integration test Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Keep _split_identifier_for_path for the register_view path Matches create_view and register_table: path from _split_identifier_for_path, raw name for the body. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Cover register_view in REST identifier integration test Register the created view under a second name that needs URL encoding, now that View exposes its metadata location (#4088). Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent a1a43c5 commit c8502b3

3 files changed

Lines changed: 92 additions & 7 deletions

File tree

‎pyiceberg/catalog/rest/__init__.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ def create_view(
13121312
location = location.rstrip("/")
13131313

13141314
request = CreateViewRequest(
1315-
name=namespace_and_view["view"],
1315+
name=self._identifier_to_validated_tuple(identifier)[-1],
13161316
location=location,
13171317
view_schema=fresh_schema,
13181318
view_version=view_version,
@@ -1826,15 +1826,14 @@ def view_exists(self, identifier: str | Identifier) -> bool:
18261826
def register_view(self, identifier: str | Identifier, metadata_location: str) -> View:
18271827
self._check_endpoint(Capability.V1_REGISTER_VIEW)
18281828
namespace_and_view = self._split_identifier_for_path(identifier, IdentifierKind.VIEW)
1829-
namespace = namespace_and_view["namespace"]
1830-
view = namespace_and_view["view"]
1829+
identifier_tuple = self._identifier_to_validated_tuple(identifier)
18311830
if self.table_exists(identifier):
1832-
raise TableAlreadyExistsError(f"Table {namespace}.{view} already exists")
1831+
raise TableAlreadyExistsError(f"Table {'.'.join(identifier_tuple)} already exists")
18331832

1834-
request = RegisterViewRequest(name=view, metadata_location=metadata_location)
1833+
request = RegisterViewRequest(name=identifier_tuple[-1], metadata_location=metadata_location)
18351834
serialized_json = request.model_dump_json().encode(UTF8)
18361835
response = self._session.post(
1837-
self.url(Endpoints.register_view, namespace=namespace),
1836+
self.url(Endpoints.register_view, namespace=namespace_and_view["namespace"]),
18381837
data=serialized_json,
18391838
)
18401839
try:

‎tests/catalog/test_rest.py‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +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
26+
from urllib.parse import parse_qs, quote, urlsplit
2727

2828
import pytest
2929
from pydantic import ValidationError
@@ -1806,6 +1806,27 @@ def test_create_view_200(rest_mock: Mocker, table_schema_simple: Schema, example
18061806
assert actual.config == example_view_metadata_rest_json["config"]
18071807

18081808

1809+
def test_create_view_name_is_not_encoded_in_body(
1810+
rest_mock: Mocker, table_schema_simple: Schema, example_view_metadata_rest_json: dict[str, Any]
1811+
) -> None:
1812+
rest_mock.post(
1813+
f"{TEST_URI}v1/namespaces/fokko/views",
1814+
json=example_view_metadata_rest_json,
1815+
status_code=200,
1816+
request_headers=TEST_HEADERS,
1817+
)
1818+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
1819+
view_version = ViewVersion(
1820+
schema_id=1,
1821+
summary={"engine-name": "spark", "engineVersion": "3.3"},
1822+
representations=[],
1823+
default_namespace=["default"],
1824+
)
1825+
for view_name in ["my view", "dept/view", "100%", "\u65e5\u672c"]:
1826+
catalog.create_view(identifier=("fokko", view_name), schema=table_schema_simple, view_version=view_version)
1827+
assert rest_mock.last_request.json()["name"] == view_name
1828+
1829+
18091830
def test_create_view_409(
18101831
rest_mock: Mocker,
18111832
table_schema_simple: Schema,
@@ -2831,6 +2852,24 @@ def test_register_view_200(rest_mock: Mocker, example_view_metadata_rest_json: d
28312852
assert actual.config == example_view_metadata_rest_json["config"]
28322853

28332854

2855+
def test_register_view_name_is_not_encoded_in_body(rest_mock: Mocker, example_view_metadata_rest_json: dict[str, Any]) -> None:
2856+
rest_mock.post(
2857+
f"{TEST_URI}v1/namespaces/default/register-view",
2858+
json=example_view_metadata_rest_json,
2859+
status_code=200,
2860+
request_headers=TEST_HEADERS,
2861+
)
2862+
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
2863+
for view_name in ["my view", "dept/view", "100%", "\u65e5\u672c"]:
2864+
rest_mock.head(
2865+
f"{TEST_URI}v1/namespaces/default/tables/{quote(view_name, safe='')}",
2866+
status_code=404,
2867+
request_headers=TEST_HEADERS,
2868+
)
2869+
catalog.register_view(identifier=("default", view_name), metadata_location="s3://warehouse/database/view/metadata.json")
2870+
assert rest_mock.last_request.json()["name"] == view_name
2871+
2872+
28342873
def test_register_view_409_view(rest_mock: Mocker) -> None:
28352874
rest_mock.head(
28362875
f"{TEST_URI}v1/namespaces/default/tables/registered_view",

‎tests/integration/test_catalog.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,53 @@ def test_rest_drop_view(
719719
assert not rest_catalog.view_exists(identifier)
720720

721721

722+
@pytest.mark.integration
723+
def test_rest_identifiers_with_special_characters(
724+
rest_catalog: RestCatalog, table_schema_simple: Schema, example_view_metadata_v1: dict[str, Any]
725+
) -> None:
726+
"""Names that need URL encoding must round-trip unchanged through path, query, and body parameters."""
727+
# "%" is omitted because the REST fixture's Jetty rejects "%25" in URL paths ("400 Ambiguous URI path encoding")
728+
for name in ["with space", "a+b&c=d", "\u65e5\u672c"]:
729+
parent = (f"{name}_{uuid.uuid4().hex}",)
730+
namespace = (*parent, name)
731+
table_identifier = (*namespace, f"table {name}")
732+
view_identifier = (*namespace, f"view {name}")
733+
registered_view_identifier = (*namespace, f"registered view {name}")
734+
735+
rest_catalog.create_namespace(parent)
736+
rest_catalog.create_namespace(namespace, properties={"key": name})
737+
assert parent in rest_catalog.list_namespaces()
738+
assert rest_catalog.list_namespaces(parent) == [namespace]
739+
assert rest_catalog.namespace_exists(namespace)
740+
assert rest_catalog.load_namespace_properties(namespace)["key"] == name
741+
742+
rest_catalog.create_table(table_identifier, table_schema_simple)
743+
assert rest_catalog.table_exists(table_identifier)
744+
assert rest_catalog.list_tables(namespace) == [table_identifier]
745+
table = rest_catalog.load_table(table_identifier)
746+
assert table.name() == table_identifier
747+
with table.transaction() as tx:
748+
tx.set_properties(key=name)
749+
assert rest_catalog.load_table(table_identifier).properties["key"] == name
750+
751+
view = View(view_identifier, ViewMetadata.model_validate(example_view_metadata_v1))
752+
created_view = rest_catalog.create_view(view_identifier, view.schema(), view.current_version())
753+
assert rest_catalog.view_exists(view_identifier)
754+
assert rest_catalog.list_views(namespace) == [view_identifier]
755+
756+
assert created_view.metadata_location is not None
757+
rest_catalog.register_view(registered_view_identifier, created_view.metadata_location)
758+
assert rest_catalog.view_exists(registered_view_identifier)
759+
assert sorted(rest_catalog.list_views(namespace)) == sorted([view_identifier, registered_view_identifier])
760+
761+
rest_catalog.drop_view(registered_view_identifier)
762+
rest_catalog.drop_view(view_identifier)
763+
rest_catalog.drop_table(table_identifier)
764+
rest_catalog.drop_namespace(namespace)
765+
rest_catalog.drop_namespace(parent)
766+
assert not rest_catalog.namespace_exists(parent)
767+
768+
722769
@pytest.mark.integration
723770
def test_rest_custom_namespace_separator(rest_catalog: RestCatalog, table_schema_simple: Schema) -> None:
724771
"""

0 commit comments

Comments
 (0)