Skip to content

Commit

Permalink
Merge pull request fastavro#167 from tebeka/issue_166
Browse files Browse the repository at this point in the history
ensure strings are not treated as an array of strings
  • Loading branch information
scottbelden authored Mar 1, 2018
2 parents de9287b + 80a021e commit baff228
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fastavro/_write.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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']):
Expand Down
1 change: 1 addition & 0 deletions fastavro/_write_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down
29 changes: 29 additions & 0 deletions tests/test_fastavro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit baff228

Please sign in to comment.