fix issue where the reader schema wasn't getting parsed
MODIFY
fastavro/_read.pyx
fastavro/_read.pyx
@@ -658,16 +658,12 @@ class file_reader:
self.schema = json.loads(self.metadata['avro.schema'])
self.codec = self.metadata.get('avro.codec', 'null')
- self.reader_schema = reader_schema
-
- if self.schema == reader_schema:
- # No need for the reader schema if they are the same
- reader_schema = None
-
- self.writer_schema = parse_schema(self.schema, _write_hint=False)
-
if reader_schema:
self.reader_schema = parse_schema(reader_schema, _write_hint=False)
+ else:
+ self.reader_schema = None
+
+ self.writer_schema = parse_schema(self.schema, _write_hint=False)
self._elems = None
@@ -690,7 +686,7 @@ class reader(file_reader):
self._header,
self.codec,
self.writer_schema,
- reader_schema)
+ self.reader_schema)
class block_reader(file_reader):
@@ -701,7 +697,7 @@ class block_reader(file_reader):
self._header,
self.codec,
self.writer_schema,
- reader_schema)
+ self.reader_schema)
cpdef schemaless_reader(fo, writer_schema, reader_schema=None):

MODIFY
fastavro/_read_py.py
fastavro/_read_py.py
@@ -593,16 +593,12 @@ class file_reader:
self.schema = json.loads(self.metadata['avro.schema'])
self.codec = self.metadata.get('avro.codec', 'null')
- self.reader_schema = reader_schema
-
- if self.schema == reader_schema:
- # No need for the reader schema if they are the same
- reader_schema = None
-
- self.writer_schema = parse_schema(self.schema, _write_hint=False)
-
if reader_schema:
self.reader_schema = parse_schema(reader_schema, _write_hint=False)
+ else:
+ self.reader_schema = None
+
+ self.writer_schema = parse_schema(self.schema, _write_hint=False)
self._elems = None
@@ -644,7 +640,7 @@ class reader(file_reader):
self._header,
self.codec,
self.writer_schema,
- reader_schema)
+ self.reader_schema)
class block_reader(file_reader):
@@ -674,7 +670,7 @@ class block_reader(file_reader):
self._header,
self.codec,
self.writer_schema,
- reader_schema)
+ self.reader_schema)
def schemaless_reader(fo, writer_schema, reader_schema=None):

MODIFY
tests/test_fastavro.py
tests/test_fastavro.py
@@ -30,12 +30,16 @@ NO_DATA = {
}
-def roundtrip(schema, records):
+def roundtrip(schema, records, pass_schema_to_reader=False):
new_file = MemoryIO()
fastavro.writer(new_file, schema, records)
new_file.seek(0)
+ if pass_schema_to_reader:
+ reader = fastavro.reader(new_file, schema)
+ else:
reader = fastavro.reader(new_file)
+
new_records = list(reader)
return new_records
@@ -91,8 +95,8 @@ def test_file(filename):
# Test schema migration with the same schema
new_file = NoSeekMemoryIO(new_file_bytes)
- schema_migration_reader = fastavro.reader(new_file, reader.schema)
- assert schema_migration_reader.reader_schema == reader.schema
+ schema_migration_reader = fastavro.reader(new_file, reader.writer_schema)
+ assert schema_migration_reader.reader_schema == reader.writer_schema
new_records = list(schema_migration_reader)
assert new_records == records
@@ -1451,3 +1455,44 @@ def test_write_union_tuple_uses_namespaced_name():
# This passes because it uses the namespaced name
assert expected_data == roundtrip(schema, data)
+
+
+def test_passing_same_schema_to_reader():
+ """https://github.com/fastavro/fastavro/issues/244"""
+ schema = {
+ 'namespace': 'test.avro.training',
+ 'name': 'SomeMessage',
+ 'type': 'record',
+ 'fields': [{
+ 'name': 'is_error',
+ 'type': 'boolean',
+ 'default': False,
+ }, {
+ 'name': 'outcome',
+ 'type': [{
+ 'type': 'record',
+ 'name': 'SomeMessage',
+ 'fields': [],
+ }, {
+ 'type': 'record',
+ 'name': 'ErrorRecord',
+ 'fields': [{
+ 'name': 'errors',
+ 'type': {'type': 'map', 'values': 'string'},
+ 'doc': 'doc',
+ }]
+ }]
+ }]
+ }
+
+ records = [{
+ 'is_error': True,
+ 'outcome': {
+ 'errors': {
+ 'field_1': 'some_message',
+ 'field_2': 'some_other_message'
+ }
+ }
+ }]
+
+ assert records == roundtrip(schema, records, pass_schema_to_reader=True)

fix issue where the reader schema wasn't getting parsed