Skip to content

Commit

Permalink
AVRO 3979: Fix schema compatibility location for type mismatch with w…
Browse files Browse the repository at this point in the history
…riter union types for Python
  • Loading branch information
Chainso committed Apr 23, 2024
1 parent d526ca8 commit 548fd12
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lang/py/avro/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ def calculate_compatibility(
raise AvroRuntimeException(f"Unknown schema type: {reader.type}")
if writer.type == SchemaType.UNION:
writer = cast(UnionSchema, writer)
for s in writer.schemas:
result = merge(result, self.get_compatibility(reader, s))
for i in range(len(writer.schemas)):
result = merge(result, self.get_compatibility(reader, writer.schemas[i], str(i), location))
return result
if reader.type in {SchemaType.NULL, SchemaType.BOOLEAN, SchemaType.INT}:
return merge(result, type_mismatch(reader, writer, location))
Expand Down
29 changes: 26 additions & 3 deletions lang/py/avro/test/test_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@
}
)
)
A_DINT_OPTIONAL_RECORD1 = parse(
json.dumps(
{
"type": SchemaType.RECORD,
"name": "Record1",
"fields": [
{"name": "a", "type": [SchemaType.NULL, SchemaType.INT], "default": None},
],
}
)
)
A_INT_B_DINT_RECORD1 = parse(
json.dumps(
{
Expand Down Expand Up @@ -1041,19 +1052,19 @@ def test_schema_compatibility_type_mismatch(self):
FLOAT_SCHEMA,
INT_LONG_FLOAT_DOUBLE_UNION_SCHEMA,
"reader type: float not compatible with writer type: double",
"/",
"/3",
),
(
LONG_SCHEMA,
INT_FLOAT_UNION_SCHEMA,
"reader type: long not compatible with writer type: float",
"/",
"/1",
),
(
INT_SCHEMA,
INT_FLOAT_UNION_SCHEMA,
"reader type: int not compatible with writer type: float",
"/",
"/1",
),
# (INT_LIST_RECORD, LONG_LIST_RECORD, "reader type: int not compatible with writer type: long", "/fields/0/type"),
(
Expand All @@ -1062,6 +1073,18 @@ def test_schema_compatibility_type_mismatch(self):
"reader type: null not compatible with writer type: int",
"/",
),
(
INT_SCHEMA,
INT_STRING_UNION_SCHEMA,
"reader type: int not compatible with writer type: string",
"/1"
),
(
A_DINT_RECORD1,
A_DINT_OPTIONAL_RECORD1,
"reader type: int not compatible with writer type: null",
"/fields/0/type/0"
)
]
for reader, writer, message, location in incompatible_pairs:
result = ReaderWriterCompatibilityChecker().get_compatibility(reader, writer)
Expand Down

0 comments on commit 548fd12

Please sign in to comment.