|
21 | 21 | import uuid |
22 | 22 | from collections.abc import Iterable |
23 | 23 | from copy import copy |
24 | | -from typing import Annotated, Any, Literal |
| 24 | +from typing import TYPE_CHECKING, Annotated, Any, Literal |
25 | 25 |
|
26 | | -from pydantic import Field, field_serializer, field_validator, model_validator |
| 26 | +from pydantic import Field, field_serializer, field_validator, model_serializer, model_validator |
27 | 27 | from pydantic import ValidationError as PydanticValidationError |
28 | 28 |
|
29 | 29 | from pyiceberg.exceptions import ValidationError |
|
49 | 49 | from pyiceberg.utils.config import Config |
50 | 50 | from pyiceberg.utils.datetime import datetime_to_millis |
51 | 51 |
|
| 52 | +if TYPE_CHECKING: |
| 53 | + from pydantic.functional_serializers import ModelWrapSerializerWithoutInfo |
| 54 | + |
52 | 55 | CURRENT_SNAPSHOT_ID = "current-snapshot-id" |
53 | 56 | CURRENT_SCHEMA_ID = "current-schema-id" |
54 | 57 | SCHEMAS = "schemas" |
@@ -618,6 +621,14 @@ def construct_refs(self) -> TableMetadata: |
618 | 621 | encryption_keys: list[EncryptedKey] = Field(alias="encryption-keys", default_factory=list) |
619 | 622 | """An optional list of encryption keys used for table encryption.""" |
620 | 623 |
|
| 624 | + @model_serializer(mode="wrap") |
| 625 | + def serialize_model(self, handler: ModelWrapSerializerWithoutInfo) -> dict[str, Any]: |
| 626 | + """Set custom serializer to leave out `encryption-keys` when it is empty.""" |
| 627 | + serialized: dict[str, Any] = handler(self) |
| 628 | + if not self.encryption_keys: |
| 629 | + serialized.pop("encryption-keys", None) |
| 630 | + return serialized |
| 631 | + |
621 | 632 | def model_dump_json(self, exclude_none: bool = True, exclude: Any | None = None, by_alias: bool = True, **kwargs: Any) -> str: |
622 | 633 | raise NotImplementedError("Writing V3 is not yet supported, see: https://github.com/apache/iceberg-python/issues/1551") |
623 | 634 |
|
|
0 commit comments