validate that default values for enums are in the symbol list
MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -212,7 +212,7 @@ cdef _parse_schema(schema, namespace, expand, _write_hint, names, named_schemas)
raise SchemaParseException(f"redefined named type: {fullname}")
names.add(fullname)
- _validate_enum_symbols(schema["symbols"])
+ _validate_enum_symbols(schema)
named_schemas[fullname] = parsed_schema
@@ -531,7 +531,8 @@ def fingerprint(parsing_canonical_form, algorithm):
return h.hexdigest()
-def _validate_enum_symbols(symbols):
+def _validate_enum_symbols(schema):
+ symbols = schema["symbols"]
for symbol in symbols:
if not isinstance(symbol, str) or not re.match(SYMBOL_REGEX, symbol):
raise SchemaParseException(
@@ -539,3 +540,6 @@ def _validate_enum_symbols(symbols):
)
if len(symbols) != len(set(symbols)):
raise SchemaParseException("All symbols in an enum must be unique")
+
+ if "default" in schema and schema["default"] not in symbols:
+ raise SchemaParseException("Default value for enum must be in symbols list")

MODIFY
fastavro/_schema_py.py
fastavro/_schema_py.py
@@ -353,7 +353,7 @@ def _parse_schema(schema, namespace, expand, _write_hint, names, named_schemas):
raise SchemaParseException(f"redefined named type: {fullname}")
names.add(fullname)
- _validate_enum_symbols(schema["symbols"])
+ _validate_enum_symbols(schema)
named_schemas[fullname] = parsed_schema
@@ -785,7 +785,8 @@ def fingerprint(parsing_canonical_form, algorithm):
return h.hexdigest()
-def _validate_enum_symbols(symbols):
+def _validate_enum_symbols(schema):
+ symbols = schema["symbols"]
for symbol in symbols:
if not isinstance(symbol, str) or not re.match(SYMBOL_REGEX, symbol):
raise SchemaParseException(
@@ -793,3 +794,6 @@ def _validate_enum_symbols(symbols):
)
if len(symbols) != len(set(symbols)):
raise SchemaParseException("All symbols in an enum must be unique")
+
+ if "default" in schema and schema["default"] not in symbols:
+ raise SchemaParseException("Default value for enum must be in symbols list")

MODIFY
tests/test_schema.py
tests/test_schema.py
@@ -1138,3 +1138,28 @@ def test_enum_symbols_validation__uniqueness():
parse_schema(invalid_schema)
assert str(err.value) == "All symbols in an enum must be unique"
+
+
+def test_enum_with_bad_default():
+ """https://github.com/fastavro/fastavro/issues/563"""
+
+ schema = {
+ "type": "record",
+ "name": "test_schema",
+ "fields": [
+ {
+ "name": "test_enum",
+ "type": {
+ "name": "test_enum_type",
+ "type": "enum",
+ "symbols": ["NONE"],
+ "default": "UNKNOWN",
+ },
+ }
+ ],
+ }
+
+ with pytest.raises(
+ SchemaParseException, match="Default value for enum must be in symbols list"
+ ):
+ fastavro.parse_schema(schema)

validate that default values for enums are in the symbol list