Skip to content

Commit

Permalink
Merge pull request #865 from Aiven-Open/keejon/fix-flatten-unions
Browse files Browse the repository at this point in the history
fix: flatten_unions for array and map union types
  • Loading branch information
jjaakola-aiven authored May 10, 2024
2 parents 5d58aa3 + 0a6c17a commit 7680026
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions karapace/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ def flatten_unions(schema: avro.schema.Schema, value: Any) -> Any:
def get_name(obj) -> str:
if isinstance(obj, avro.schema.PrimitiveSchema):
return obj.fullname
if isinstance(obj, (avro.schema.ArraySchema, avro.schema.MapSchema)):
return obj.type
return obj.name

f = next((s for s in schema.schemas if get_name(s) in value), None)
Expand Down
31 changes: 24 additions & 7 deletions tests/unit/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@
"name": "attr2",
"type": ["null", "string"],
},
{
"name": "attrArray",
"type": ["null", {"type": "array", "items": "string"}],
},
{
"name": "attrMap",
"type": ["null", {"type": "map", "values": "string"}],
},
{
"name": "attrRecord",
"type": ["null", {"type": "record", "name": "Record", "fields": [{"name": "attr1", "type": "string"}]}],
},
],
}
),
Expand Down Expand Up @@ -128,13 +140,18 @@ async def test_happy_flow(default_config_path: Path):
assert mock_registry_client.method_calls == [call.get_schema("top"), call.get_schema_for_id(1)]


def test_flatten_unions_record() -> None:
record = {"attr1": {"string": "sample data"}, "attr2": None}
flatten_record = {"attr1": "sample data", "attr2": None}
assert flatten_unions(TYPED_AVRO_SCHEMA.schema, record) == flatten_record

record = {"attr1": None, "attr2": None}
assert flatten_unions(TYPED_AVRO_SCHEMA.schema, record) == record
@pytest.mark.parametrize(
["record", "flattened_record"],
[
[{"attr1": {"string": "sample data"}, "attr2": None}, {"attr1": "sample data", "attr2": None}],
[{"attr1": None, "attr2": None}, {"attr1": None, "attr2": None}],
[{"attrArray": {"array": ["item1", "item2"]}}, {"attrArray": ["item1", "item2"]}],
[{"attrMap": {"map": {"k1": "v1", "k2": "v2"}}}, {"attrMap": {"k1": "v1", "k2": "v2"}}],
[{"attrRecord": {"Record": {"attr1": "test"}}}, {"attrRecord": {"attr1": "test"}}],
],
)
def test_flatten_unions_record(record, flattened_record) -> None:
assert flatten_unions(TYPED_AVRO_SCHEMA.schema, record) == flattened_record


def test_flatten_unions_array() -> None:
Expand Down

0 comments on commit 7680026

Please sign in to comment.