fix bug where records should match but they were not
MODIFY
fastavro/_read.pyx
fastavro/_read.pyx
@@ -127,6 +127,9 @@ cpdef match_schemas(w_schema, r_schema):
"aliases", []
):
return r_schema
+ elif w_type not in AVRO_TYPES and r_type in NAMED_TYPES:
+ if match_types(w_type, r_schema["name"]):
+ return r_schema["name"]
elif match_types(w_type, r_type):
return r_schema
raise SchemaResolutionError(error_msg)
@@ -686,7 +689,7 @@ cpdef _read_data(
record_type = extract_record_type(writer_schema)
- if reader_schema and record_type in AVRO_TYPES:
+ if reader_schema:
# If the schemas are the same, set the reader schema to None so that no
# schema resolution is done for this call or future recursive calls
if writer_schema == reader_schema:

MODIFY
fastavro/_read_py.py
fastavro/_read_py.py
@@ -117,6 +117,9 @@ def match_schemas(w_schema, r_schema):
"aliases", []
):
return r_schema
+ elif w_type not in AVRO_TYPES and r_type in NAMED_TYPES:
+ if match_types(w_type, r_schema["name"]):
+ return r_schema["name"]
elif match_types(w_type, r_type):
return r_schema
raise SchemaResolutionError(error_msg)
@@ -576,7 +579,7 @@ def read_data(
record_type = extract_record_type(writer_schema)
- if reader_schema and record_type in AVRO_TYPES:
+ if reader_schema:
# If the schemas are the same, set the reader schema to None so that no
# schema resolution is done for this call or future recursive calls
if writer_schema == reader_schema:

MODIFY
tests/test_fastavro.py
tests/test_fastavro.py
@@ -2882,3 +2882,79 @@ def test_non_string_types_raise_type_error():
new_file = BytesIO()
with pytest.raises(TypeError, match="must be string"):
fastavro.schemaless_writer(new_file, schema, record)
+
+
+def test_name_changes_to_full_record():
+ """https://github.com/fastavro/fastavro/issues/601"""
+ writer_schema = {
+ "type": "record",
+ "name": "test",
+ "fields": [
+ {
+ "name": "test1",
+ "type": {
+ "type": "record",
+ "name": "my_record",
+ "fields": [{"name": "field1", "type": "string"}],
+ },
+ },
+ {"name": "test2", "type": "my_record"},
+ ],
+ }
+
+ reader_schema = {
+ "type": "record",
+ "name": "test",
+ "fields": [
+ {
+ "name": "test2",
+ "type": {
+ "type": "record",
+ "name": "my_record",
+ "fields": [{"name": "field1", "type": "string"}],
+ },
+ },
+ ],
+ }
+
+ records = [{"test1": {"field1": "foo"}, "test2": {"field1": "bar"}}]
+ output = roundtrip(writer_schema, records, reader_schema=reader_schema)
+ assert output == [{"test2": {"field1": "bar"}}]
+
+
+def test_record_names_must_match():
+ """https://github.com/fastavro/fastavro/issues/601"""
+ writer_schema = {
+ "type": "record",
+ "name": "test",
+ "fields": [
+ {
+ "name": "test1",
+ "type": {
+ "type": "record",
+ "name": "my_record",
+ "fields": [{"name": "field1", "type": "string"}],
+ },
+ },
+ {"name": "test2", "type": "my_record"},
+ ],
+ }
+
+ reader_schema = {
+ "type": "record",
+ "name": "test",
+ "fields": [
+ {
+ "name": "test2",
+ "type": {
+ "type": "record",
+ "name": "different_name",
+ "fields": [{"name": "field1", "type": "string"}],
+ },
+ },
+ ],
+ }
+
+ records = [{"test1": {"field1": "foo"}, "test2": {"field1": "bar"}}]
+ with pytest.raises(SchemaResolutionError):
+ roundtrip(writer_schema, records, reader_schema=reader_schema)

fix bug where records should match but they were not