This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git
The following commit(s) were added to refs/heads/main by this push:
new 9fc6a0816 REST: Read an explicit null config, storage-credentials,
defaults or overrides as empty (#4353)
9fc6a0816 is described below
commit 9fc6a0816c5038a0b65d0af925ea51ea721748be
Author: Sohail Shaikh <[email protected]>
AuthorDate: Wed Nov 25 20:01:19 2026 +0000
REST: Read an explicit null config, storage-credentials, defaults or
overrides as empty (#4353)
* REST: Read an explicit null config, storage-credentials, defaults or
overrides as empty
A REST server that serializes unset fields as null made loading a table or
view, or constructing the catalog, fail. Java reads a null config as empty.
* Use annotated types for null-tolerant REST fields
---
pyiceberg/catalog/rest/__init__.py | 28 +++++++++++++++++++++-------
tests/catalog/test_rest.py | 26 ++++++++++++++++++++++++++
2 files changed, 47 insertions(+), 7 deletions(-)
diff --git a/pyiceberg/catalog/rest/__init__.py
b/pyiceberg/catalog/rest/__init__.py
index 4f9a6230a..b876b857f 100644
--- a/pyiceberg/catalog/rest/__init__.py
+++ b/pyiceberg/catalog/rest/__init__.py
@@ -23,15 +23,17 @@ import sys
import time
import uuid
from collections import deque
-from collections.abc import Mapping
+from collections.abc import Callable, Mapping
from enum import Enum, StrEnum
from typing import (
TYPE_CHECKING,
+ Annotated,
Any,
+ TypeVar,
)
from urllib.parse import quote, unquote
-from pydantic import ConfigDict, Field, TypeAdapter, field_validator
+from pydantic import BeforeValidator, ConfigDict, Field, TypeAdapter,
field_validator
from requests import HTTPError, PreparedRequest, Response, Session
from requests.adapters import DEFAULT_RETRIES, HTTPAdapter
from tenacity import RetryCallState, retry, retry_if_exception_type,
stop_after_attempt
@@ -375,11 +377,23 @@ _RETRY_ARGS = {
}
+T = TypeVar("T")
+
+
+def _empty_if_none(factory: Callable[[], Any]) -> BeforeValidator:
+ return BeforeValidator(lambda v: factory() if v is None else v)
+
+
+# A JSON null is read as an empty list / map, like Java's `hasNonNull`
+NullableList = Annotated[list[T], _empty_if_none(list)]
+NullableProperties = Annotated[Properties, _empty_if_none(dict)]
+
+
class TableResponse(IcebergBaseModel):
metadata_location: str | None = Field(alias="metadata-location",
default=None)
metadata: TableMetadata
- config: Properties = Field(default_factory=dict)
- storage_credentials: list[StorageCredential] =
Field(alias="storage-credentials", default_factory=list)
+ config: NullableProperties = Field(default_factory=dict)
+ storage_credentials: NullableList[StorageCredential] =
Field(alias="storage-credentials", default_factory=list)
labels: Labels = Field(default_factory=Labels)
@@ -390,7 +404,7 @@ class LoadCredentialsResponse(IcebergBaseModel):
class ViewResponse(IcebergBaseModel):
metadata_location: str = Field(..., alias="metadata-location")
metadata: ViewMetadata
- config: Properties = Field(default_factory=dict)
+ config: NullableProperties = Field(default_factory=dict)
labels: Labels = Field(default_factory=Labels)
@@ -443,8 +457,8 @@ class RegisterViewRequest(IcebergBaseModel):
class ConfigResponse(IcebergBaseModel):
- defaults: Properties | None = Field(default_factory=dict)
- overrides: Properties | None = Field(default_factory=dict)
+ defaults: NullableProperties = Field(default_factory=dict)
+ overrides: NullableProperties = Field(default_factory=dict)
endpoints: set[Endpoint] | None = Field(default=None)
idempotency_key_lifetime: str | None =
Field(alias="idempotency-key-lifetime", default=None)
diff --git a/tests/catalog/test_rest.py b/tests/catalog/test_rest.py
index 0d8ca0025..f2d1da8c8 100644
--- a/tests/catalog/test_rest.py
+++ b/tests/catalog/test_rest.py
@@ -51,6 +51,7 @@ from pyiceberg.catalog.rest import (
SIGV4_MAX_RETRIES_DEFAULT,
SNAPSHOT_LOADING_MODE,
Capability,
+ ConfigResponse,
Endpoint,
HttpMethod,
RestCatalog,
@@ -3925,6 +3926,31 @@ def test_load_responses_parse_labels(
assert response_type.model_validate({**payload, "labels":
invalid}).labels == Labels()
+def test_load_responses_with_null_config_and_storage_credentials(
+ example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any],
example_view_metadata_rest_json: dict[str, Any]
+) -> None:
+ # Like Java, an explicit null config reads as an empty config
+ table_response = TableResponse.model_validate(
+ {**example_table_metadata_with_snapshot_v1_rest_json, "config": None,
"storage-credentials": None}
+ )
+ assert table_response.config == {}
+ assert table_response.storage_credentials == []
+
+ view_response =
ViewResponse.model_validate({**example_view_metadata_rest_json, "config": None})
+ assert view_response.config == {}
+
+
+def test_config_response_with_null_defaults_and_overrides() -> None:
+ config_response = ConfigResponse.model_validate({"defaults": None,
"overrides": None})
+ assert config_response.defaults == {}
+ assert config_response.overrides == {}
+
+
+def test_catalog_with_null_config_defaults_and_overrides(rest_mock: Mocker) ->
None:
+ rest_mock.get(f"{TEST_URI}v1/config", json={"defaults": None, "overrides":
None}, status_code=200)
+ assert RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN).uri == TEST_URI
+
+
def test_table_labels_from_rest_responses(
rest_mock: Mocker, example_table_metadata_with_snapshot_v1_rest_json:
dict[str, Any], table_schema_simple: Schema
) -> None: