force read the writer schema in the cython implementation
MODIFY
fastavro/_read.pyx
fastavro/_read.pyx
@@ -714,7 +714,9 @@ class file_reader:
else:
self.reader_schema = None
- self.writer_schema = parse_schema(self._schema, _write_hint=False)
+ self.writer_schema = parse_schema(
+ self._schema, _write_hint=False, _force=True
+ )
self._elems = None

MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -50,8 +50,10 @@ cpdef schema_name(schema, parent_ns):
return namespace, '{}.{}'.format(namespace, name)
-def parse_schema(schema, _write_hint=True):
- if isinstance(schema, dict) and "__fastavro_parsed" in schema:
+def parse_schema(schema, _write_hint=True, _force=False):
+ if _force:
+ return _parse_schema(schema, "", _write_hint)
+ elif isinstance(schema, dict) and "__fastavro_parsed" in schema:
return schema
else:
return _parse_schema(schema, "", _write_hint)

MODIFY
tests/test_fastavro.py
tests/test_fastavro.py
@@ -1766,3 +1766,74 @@ def test_reader_schema_attributes_throws_deprecation():
reader = fastavro.reader(stream)
with pytest.warns(DeprecationWarning):
reader.schema
+
+
+def test_writer_schema_always_read():
+ """https://github.com/fastavro/fastavro/issues/312"""
+ schema = {
+ "type": "record",
+ "name": "Outer",
+ "fields": [{
+ "name": "item",
+ "type": [{
+ "type": "record",
+ "name": "Inner1",
+ "fields": [{
+ "name": "id",
+ "type": {
+ "type": "record",
+ "name": "UUID",
+ "fields": [{
+ "name": "id",
+ "type": "string"
+ }]
+ },
+ "default": {
+ "id": ""
+ }
+ }, {
+ "name": "description",
+ "type": "string"
+ }, {
+ "name": "size",
+ "type": "int"
+ }]
+ }, {
+ "type": "record",
+ "name": "Inner2",
+ "fields": [{
+ "name": "id",
+ "type": "UUID",
+ "default": {
+ "id": ""
+ }
+ }, {
+ "name": "name",
+ "type": "string"
+ }, {
+ "name": "age",
+ "type": "long"
+ }]
+ }]
+ }]
+ }
+
+ records = [
+ {'item': {'description': 'test', 'size': 1}},
+ {'item': {'id': {'id': '#1'}, 'name': 'foobar', 'age': 12}}
+ ]
+
+ file = MemoryIO()
+
+ fastavro.writer(file, fastavro.parse_schema(schema), records)
+ file.seek(0)
+
+ # Clean the schema entries to simulate reading from a fresh process (no
+ # cached schemas)
+ del SCHEMA_DEFS['Outer']
+ del SCHEMA_DEFS['Inner1']
+ del SCHEMA_DEFS['Inner2']
+ del SCHEMA_DEFS['UUID']
+
+ # This should not raise a KeyError
+ fastavro.reader(file)

force read the writer schema in the cython implementation