ensure strings are not treated as an array of strings
MODIFY
fastavro/_write.pyx
fastavro/_write.pyx
@@ -458,7 +458,7 @@ cpdef validate(object datum, schema):
return datum in schema['symbols']
if record_type == 'array':
- if not isinstance(datum, Iterable):
+ if not isinstance(datum, Iterable) or is_str(datum):
return False
for d in datum:
if not validate(d, schema['items']):

MODIFY
fastavro/_write_py.py
fastavro/_write_py.py
@@ -348,6 +348,7 @@ def validate(datum, schema):
if record_type == 'array':
return (
isinstance(datum, Iterable) and
+ not is_str(datum) and
all(validate(d, schema['items']) for d in datum)
)

MODIFY
tests/test_fastavro.py
tests/test_fastavro.py
@@ -1316,3 +1316,32 @@ def test_doubles_set_to_zero_on_windows():
}]
assert records == roundtrip(schema, records)
+
+
+def test_string_not_treated_as_array():
+ """https://github.com/tebeka/fastavro/issues/166"""
+
+ schema = {
+ 'type': 'record',
+ 'fields': [{
+ 'name': 'description',
+ "type": [
+ "null",
+ {
+ "type": "array",
+ "items": "string"
+ },
+ "string"
+ ],
+ }],
+ "name": "description",
+ "doc": "A description of the thing."
+ }
+
+ records = [{
+ 'description': 'value',
+ }, {
+ 'description': ['an', 'array']
+ }]
+
+ assert records == roundtrip(schema, records)

ensure strings are not treated as an array of strings