fix bug with schema evolution in unions
MODIFY
fastavro/_read.pyx
fastavro/_read.pyx
@@ -71,7 +71,10 @@ cpdef match_types(writer_type, reader_type):
if isinstance(writer_type, list) or isinstance(reader_type, list):
return True
if isinstance(writer_type, dict) or isinstance(reader_type, dict):
+ try:
return match_schemas(writer_type, reader_type)
+ except SchemaResolutionError:
+ return False
if writer_type == reader_type:
return True
# promotion cases
@@ -98,12 +101,8 @@ cpdef match_schemas(w_schema, r_schema):
# If the reader is a union, ensure one of the new schemas is the same
# as the writer
for schema in r_schema:
- try:
if match_types(w_schema, schema):
return schema
- except SchemaResolutionError:
- # Try to match the next schema in the union
- pass
else:
raise SchemaResolutionError(error_msg)
else:

MODIFY
fastavro/_read_py.py
fastavro/_read_py.py
@@ -66,7 +66,10 @@ def match_types(writer_type, reader_type):
if isinstance(writer_type, list) or isinstance(reader_type, list):
return True
if isinstance(writer_type, dict) or isinstance(reader_type, dict):
+ try:
return match_schemas(writer_type, reader_type)
+ except SchemaResolutionError:
+ return False
if writer_type == reader_type:
return True
# promotion cases
@@ -93,12 +96,8 @@ def match_schemas(w_schema, r_schema):
# If the reader is a union, ensure one of the new schemas is the same
# as the writer
for schema in r_schema:
- try:
if match_types(w_schema, schema):
return schema
- except SchemaResolutionError:
- # Try to match the next schema in the union
- pass
else:
raise SchemaResolutionError(error_msg)
else:

MODIFY
tests/test_schema_evolution.py
tests/test_schema_evolution.py
@@ -276,3 +276,97 @@ def test_schema_migrate_record_to_union():
new_schema_null_first, binary
)
assert output_using_new_schema_null_first == record
+
+
+def test_union_of_lists_evolution_with_doc():
+ """https://github.com/fastavro/fastavro/issues/486"""
+ original_schema = {
+ "name": "test_union_of_lists_evolution_with_doc",
+ "type": "record",
+ "fields": [
+ {
+ "name": "id",
+ "type": [
+ "null",
+ {
+ "name": "some_record",
+ "type": "record",
+ "fields": [{"name": "field", "type": "string"}],
+ },
+ ],
+ }
+ ],
+ }
+
+ new_schema = {
+ "name": "test_union_of_lists_evolution_with_doc",
+ "type": "record",
+ "fields": [
+ {
+ "name": "id",
+ "type": [
+ "null",
+ {
+ "name": "some_record",
+ "type": "record",
+ "doc": "some documentation",
+ "fields": [{"name": "field", "type": "string"}],
+ },
+ ],
+ }
+ ],
+ }
+
+ record = {"id": {"field": "foo"}}
+
+ binary = avro_to_bytes_with_schema(original_schema, record)
+
+ output_using_new_schema = bytes_with_schema_to_avro(new_schema, binary)
+ assert output_using_new_schema == record
+
+
+def test_union_of_lists_evolution_with_extra_type():
+ """https://github.com/fastavro/fastavro/issues/486"""
+ original_schema = {
+ "name": "test_union_of_lists_evolution_with_extra_type",
+ "type": "record",
+ "fields": [
+ {
+ "name": "id",
+ "type": [
+ "null",
+ {
+ "name": "some_record",
+ "type": "record",
+ "fields": [{"name": "field", "type": "string"}],
+ },
+ ],
+ }
+ ],
+ }
+
+ new_schema = {
+ "name": "test_union_of_lists_evolution_with_extra_type",
+ "type": "record",
+ "fields": [
+ {
+ "name": "id",
+ "type": [
+ "null",
+ {
+ "name": "some_record",
+ "type": "record",
+ "fields": [{"name": "field", "type": "string"}],
+ },
+ "string",
+ ],
+ }
+ ],
+ }
+
+ record = {"id": {"field": "foo"}}
+
+ binary = avro_to_bytes_with_schema(original_schema, record)
+
+ output_using_new_schema = bytes_with_schema_to_avro(new_schema, binary)
+ assert output_using_new_schema == record

fix bug with schema evolution in unions