add support for specifying a default value for enums (see
MODIFY
fastavro/_read.pyx
fastavro/_read.pyx
@@ -319,6 +319,10 @@ cdef read_enum(fo, writer_schema, reader_schema=None):
index = read_long(fo)
symbol = writer_schema['symbols'][index]
if reader_schema and symbol not in reader_schema['symbols']:
+ default = reader_schema.get("default")
+ if default:
+ return default
+ else:
symlist = reader_schema['symbols']
msg = '%s not found in reader symbol list %s' % (symbol, symlist)
raise SchemaResolutionError(msg)

MODIFY
fastavro/_read_py.py
fastavro/_read_py.py
@@ -259,6 +259,10 @@ def read_enum(fo, writer_schema, reader_schema=None):
index = read_long(fo)
symbol = writer_schema['symbols'][index]
if reader_schema and symbol not in reader_schema['symbols']:
+ default = reader_schema.get("default")
+ if default:
+ return default
+ else:
symlist = reader_schema['symbols']
msg = '%s not found in reader symbol list %s' % (symbol, symlist)
raise SchemaResolutionError(msg)

MODIFY
tests/test_fastavro.py
tests/test_fastavro.py
@@ -700,28 +700,6 @@ def test_schema_migration_maps_failure():
list(new_reader)
-def test_schema_migration_enum_failure():
- schema = {
- "type": "enum",
- "name": "test",
- "symbols": ["FOO", "BAR"],
- }
-
- new_schema = {
- "type": "enum",
- "name": "test",
- "symbols": ["BAZ", "BAR"],
- }
-
- new_file = MemoryIO()
- records = ["FOO"]
- fastavro.writer(new_file, schema, records)
- new_file.seek(0)
- new_reader = fastavro.reader(new_file, new_schema)
- with pytest.raises(fastavro.read.SchemaResolutionError):
- list(new_reader)
-
-
def test_schema_migration_schema_mismatch():
schema = {
"type": "record",

MODIFY
tests/test_schema_evolution.py
tests/test_schema_evolution.py
@@ -69,3 +69,50 @@ def test_evolution_add_field_without_default():
with pytest.raises(SchemaResolutionError):
record_bytes_a = avro_to_bytes_with_schema(schema_dict_a, record_a)
bytes_with_schema_to_avro(schema_dict_a_c, record_bytes_a)
+
+
+def test_enum_evolution_no_default_failure():
+ original_schema = {
+ "type": "enum",
+ "name": "test",
+ "symbols": ["FOO", "BAR"],
+ }
+
+ new_schema = {
+ "type": "enum",
+ "name": "test",
+ "symbols": ["BAZ", "BAR"],
+ }
+
+ original_records = ["FOO"]
+
+ bio = BytesIO()
+ fastavro.writer(bio, original_schema, original_records)
+ bio.seek(0)
+
+ with pytest.raises(fastavro.read.SchemaResolutionError):
+ list(fastavro.reader(bio, new_schema))
+
+
+def test_enum_evolution_using_default():
+ original_schema = {
+ "type": "enum",
+ "name": "test",
+ "symbols": ["A", "B"],
+ }
+
+ new_schema = {
+ "type": "enum",
+ "name": "test",
+ "symbols": ["C", "D"],
+ "default": "C",
+ }
+
+ original_records = ["A"]
+
+ bio = BytesIO()
+ fastavro.writer(bio, original_schema, original_records)
+ bio.seek(0)
+
+ new_records = list(fastavro.reader(bio, new_schema))
+ assert new_records == ["C"]

add support for specifying a default value for enums (see AVRO-1340)