ensure aliases are a list
MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -7,7 +7,7 @@ import json
from ._six import iteritems
from ._schema_common import (
PRIMITIVES, UnknownType, SchemaParseException, RESERVED_PROPERTIES,
- SCHEMA_DEFS,
+ SCHEMA_DEFS, OPTIONAL_FIELD_PROPERTIES, RESERVED_FIELD_PROPERTIES,
)
@@ -142,12 +142,22 @@ cdef _parse_schema(schema, namespace, _write_hint):
def parse_field(field, namespace, _write_hint):
parsed_field = {
- "name": field["name"],
+ key: value
+ for key, value in iteritems(field)
+ if key not in RESERVED_FIELD_PROPERTIES
}
- if "default" in field:
- parsed_field["default"] = field["default"]
+ for prop in OPTIONAL_FIELD_PROPERTIES:
+ if prop in field:
+ parsed_field[prop] = field[prop]
+
+ # Aliases must be a list
+ aliases = parsed_field.get("aliases", [])
+ if not isinstance(aliases, list):
+ msg = "aliases must be a list, not {}".format(aliases)
+ raise SchemaParseException(msg)
+ parsed_field["name"] = field["name"]
parsed_field["type"] = _parse_schema(field["type"], namespace, _write_hint)
return parsed_field

MODIFY
fastavro/_schema_common.py
fastavro/_schema_common.py
@@ -24,6 +24,17 @@ RESERVED_PROPERTIES = {
'doc',
}
+OPTIONAL_FIELD_PROPERTIES = {
+ "doc",
+ "aliases",
+ "default",
+}
+
+RESERVED_FIELD_PROPERTIES = {
+ "type",
+ "name"
+} | OPTIONAL_FIELD_PROPERTIES
+
class UnknownType(ValueError):
def __init__(self, name):

MODIFY
fastavro/_schema_py.py
fastavro/_schema_py.py
@@ -7,7 +7,7 @@ import json
from .six import iteritems
from ._schema_common import (
PRIMITIVES, UnknownType, SchemaParseException, RESERVED_PROPERTIES,
- SCHEMA_DEFS,
+ SCHEMA_DEFS, OPTIONAL_FIELD_PROPERTIES, RESERVED_FIELD_PROPERTIES,
)
@@ -167,12 +167,22 @@ def _parse_schema(schema, namespace, _write_hint):
def parse_field(field, namespace, _write_hint):
parsed_field = {
- "name": field["name"],
+ key: value
+ for key, value in iteritems(field)
+ if key not in RESERVED_FIELD_PROPERTIES
}
- if "default" in field:
- parsed_field["default"] = field["default"]
+ for prop in OPTIONAL_FIELD_PROPERTIES:
+ if prop in field:
+ parsed_field[prop] = field[prop]
+
+ # Aliases must be a list
+ aliases = parsed_field.get("aliases", [])
+ if not isinstance(aliases, list):
+ msg = "aliases must be a list, not {}".format(aliases)
+ raise SchemaParseException(msg)
+ parsed_field["name"] = field["name"]
parsed_field["type"] = _parse_schema(field["type"], namespace, _write_hint)
return parsed_field

MODIFY
tests/test_schema.py
tests/test_schema.py
@@ -74,3 +74,34 @@ def test_unknown_type():
with pytest.raises(UnknownType):
parse_schema(schema)
+
+
+def test_aliases_are_preserved():
+ schema = {
+ "type": "record",
+ "name": "test_parse_schema",
+ "fields": [{
+ "name": "field",
+ "type": "string",
+ "aliases": ["test"],
+ }],
+ }
+
+ parsed_schema = parse_schema(schema)
+ assert "aliases" in parsed_schema["fields"][0]
+
+
+def test_aliases_is_a_list():
+ """https://github.com/fastavro/fastavro/issues/206"""
+ schema = {
+ "type": "record",
+ "name": "test_parse_schema",
+ "fields": [{
+ "name": "field",
+ "type": "string",
+ "aliases": "foobar",
+ }],
+ }
+
+ with pytest.raises(SchemaParseException):
+ parse_schema(schema)

ensure aliases are a list