fix append bug
MODIFY
fastavro/_write.pyx
fastavro/_write.pyx
@@ -616,7 +616,7 @@ cdef class Writer:
avro_reader = reader(self.fo)
header = avro_reader._header
- self.schema = parse_schema(avro_reader.writer_schema)
+ self.schema = parse_schema(avro_reader.writer_schema, self._named_schemas)
codec = avro_reader.metadata.get("avro.codec", "null")
self.sync_marker = header["sync"]

MODIFY
fastavro/_write_py.py
fastavro/_write_py.py
@@ -478,7 +478,8 @@ class Writer(GenericWriter):
avro_reader = reader(self.encoder._fo)
header = avro_reader._header
- self.schema = parse_schema(avro_reader.writer_schema)
+ self._named_schemas = {}
+ self.schema = parse_schema(avro_reader.writer_schema, self._named_schemas)
codec = avro_reader.metadata.get("avro.codec", "null")

ADD
tests/load_schema_test_16/namespace.match.avsc
+{
+ "name": "namespace.match",
+ "type": "record",
+ "fields": [
+ {"name": "id", "type": "string"},
+ {"name": "team1", "type": "namespace.team"},
+ {"name": "team2", "type": "namespace.team"}
+ ]
+}
\ No newline at end of file

ADD
tests/load_schema_test_16/namespace.team.avsc
+{
+ "name": "namespace.team",
+ "type": "record",
+ "fields": [
+ {"name": "name", "type": "string"}
+ ]
+}
\ No newline at end of file

MODIFY
tests/test_fastavro.py
tests/test_fastavro.py
@@ -1604,17 +1604,12 @@ def test_appending_records_wrong_mode_fails(tmpdir):
fastavro.writer(new_file, schema, [{"field": "bar"}])
-def test_appending_records_different_schema_fails(tmpdir):
+def test_appending_records_different_schema_works(tmpdir):
"""https://github.com/fastavro/fastavro/issues/276"""
schema = {
"type": "record",
"name": "test_appending_records_different_schema_fails",
- "fields": [
- {
- "name": "field",
- "type": "string",
- }
- ],
+ "fields": [{"name": "field", "type": "string"}],
}
test_file = str(tmpdir.join("test.avro"))
@@ -1625,16 +1620,65 @@ def test_appending_records_different_schema_fails(tmpdir):
different_schema = {
"type": "record",
"name": "test_appending_records",
+ "fields": [{"name": "field", "type": "int"}],
+ }
+
+ with open(test_file, "a+b") as new_file:
+ fastavro.writer(new_file, different_schema, [{"field": "bar"}])
+
+
+def test_appending_records_different_schema_works_2(tmpdir):
+ """https://github.com/fastavro/fastavro/issues/276"""
+ schema = {
+ "type": "record",
+ "name": "test_appending_records_different_schema_fails",
"fields": [
+ {"name": "field", "type": "string"},
{
- "name": "field",
- "type": "int",
+ "name": "field2",
+ "type": {
+ "type": "record",
+ "name": "subrecord",
+ "fields": [{"name": "subfield", "type": "string"}],
+ },
+ },
+ {"name": "field3", "type": "subrecord"},
+ ],
+ }
+
+ test_file = str(tmpdir.join("test.avro"))
+
+ with open(test_file, "wb") as new_file:
+ fastavro.writer(
+ new_file,
+ schema,
+ [
+ {
+ "field": "foo",
+ "field2": {"subfield": "foo2"},
+ "field3": {"subfield": "foo3"},
}
],
+ )
+
+ different_schema = {
+ "type": "record",
+ "name": "test_appending_records",
+ "fields": [{"name": "field", "type": "int"}],
}
with open(test_file, "a+b") as new_file:
- fastavro.writer(new_file, different_schema, [{"field": "bar"}])
+ fastavro.writer(
+ new_file,
+ different_schema,
+ [
+ {
+ "field": "bar",
+ "field2": {"subfield": "bar2"},
+ "field3": {"subfield": "bar3"},
+ }
+ ],
+ )
def test_appending_records_null_schema_works(tmpdir):
@@ -1642,12 +1686,7 @@ def test_appending_records_null_schema_works(tmpdir):
schema = {
"type": "record",
"name": "test_appending_records_different_schema_fails",
- "fields": [
- {
- "name": "field",
- "type": "string",
- }
- ],
+ "fields": [{"name": "field", "type": "string"}],
}
test_file = str(tmpdir.join("test.avro"))

MODIFY
tests/test_schema.py
tests/test_schema.py
+from io import BytesIO
from os.path import join, abspath, dirname
import pytest
import fastavro
@@ -1206,3 +1207,25 @@ def test_enum_with_bad_default():
SchemaParseException, match="Default value for enum must be in symbols list"
):
fastavro.parse_schema(schema)
+
+
+def test_foobar():
+ """https://github.com/fastavro/fastavro/issues/608"""
+ load_schema_dir = join(abspath(dirname(__file__)), "load_schema_test_16")
+ schema_path = join(load_schema_dir, "namespace.match.avsc")
+ loaded_schema = fastavro.schema.load_schema(schema_path)
+
+ record = {
+ "id": "123",
+ "team1": {"name": "A"},
+ "team2": {"name": "B"},
+ }
+
+ bio = BytesIO()
+ fastavro.writer(bio, loaded_schema, [record])
+ fastavro.writer(bio, loaded_schema, [record])
+
+ bio.seek(0)
+
+ records = list(fastavro.reader(bio))
+ assert records == [record, record]

fix append bug