diff --git a/pyiceberg/types.py b/pyiceberg/types.py index 8fa745384d..bd0eb7a5e9 100644 --- a/pyiceberg/types.py +++ b/pyiceberg/types.py @@ -328,8 +328,8 @@ def __init__( data["type"] = data["type"] if "type" in data else field_type data["required"] = required data["doc"] = doc - data["initial-default"] = initial_default - data["write-default"] = write_default + data["initial-default"] = data["initial-default"] if "initial-default" in data else initial_default + data["write-default"] = data["write-default"] if "write-default" in data else write_default super().__init__(**data) def __str__(self) -> str: diff --git a/tests/conftest.py b/tests/conftest.py index 9160a1435d..ae6cff2d03 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -149,6 +149,35 @@ def table_schema_simple() -> Schema: ) +@pytest.fixture(scope="session") +def table_schema_with_full_nested_fields() -> Schema: + return schema.Schema( + NestedField( + field_id=1, + name="foo", + field_type=StringType(), + required=False, + doc="foo doc", + initial_default="foo initial", + write_default="foo write", + ), + NestedField( + field_id=2, name="bar", field_type=IntegerType(), required=True, doc="bar doc", initial_default=42, write_default=43 + ), + NestedField( + field_id=3, + name="baz", + field_type=BooleanType(), + required=False, + doc="baz doc", + initial_default=True, + write_default=False, + ), + schema_id=1, + identifier_field_ids=[2], + ) + + @pytest.fixture(scope="session") def table_schema_nested() -> Schema: return schema.Schema( diff --git a/tests/test_schema.py b/tests/test_schema.py index 4d894b0d03..d1fc19df77 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -421,17 +421,17 @@ def __getitem__(self, pos: int) -> Any: assert inner_accessor.get(container) == "name" -def test_serialize_schema(table_schema_simple: Schema) -> None: - actual = table_schema_simple.model_dump_json() - expected = """{"type":"struct","fields":[{"id":1,"name":"foo","type":"string","required":false},{"id":2,"name":"bar","type":"int","required":true},{"id":3,"name":"baz","type":"boolean","required":false}],"schema-id":1,"identifier-field-ids":[2]}""" +def test_serialize_schema(table_schema_with_full_nested_fields: Schema) -> None: + actual = table_schema_with_full_nested_fields.model_dump_json() + expected = """{"type":"struct","fields":[{"id":1,"name":"foo","type":"string","required":false,"doc":"foo doc","initial-default":"foo initial","write-default":"foo write"},{"id":2,"name":"bar","type":"int","required":true,"doc":"bar doc","initial-default":42,"write-default":43},{"id":3,"name":"baz","type":"boolean","required":false,"doc":"baz doc","initial-default":true,"write-default":false}],"schema-id":1,"identifier-field-ids":[2]}""" assert actual == expected -def test_deserialize_schema(table_schema_simple: Schema) -> None: +def test_deserialize_schema(table_schema_with_full_nested_fields: Schema) -> None: actual = Schema.model_validate_json( - """{"type": "struct", "fields": [{"id": 1, "name": "foo", "type": "string", "required": false}, {"id": 2, "name": "bar", "type": "int", "required": true}, {"id": 3, "name": "baz", "type": "boolean", "required": false}], "schema-id": 1, "identifier-field-ids": [2]}""" + """{"type": "struct", "fields": [{"id": 1, "name": "foo", "type": "string", "required": false, "doc": "foo doc", "initial-default": "foo initial", "write-default": "foo write"}, {"id": 2, "name": "bar", "type": "int", "required": true, "doc": "bar doc", "initial-default": 42, "write-default": 43}, {"id": 3, "name": "baz", "type": "boolean", "required": false, "doc": "baz doc", "initial-default": true, "write-default": false}], "schema-id": 1, "identifier-field-ids": [2]}""" ) - expected = table_schema_simple + expected = table_schema_with_full_nested_fields assert actual == expected