fix namespace issue with embedded records
MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -101,14 +101,14 @@ cdef _parse_schema(schema, namespace, _write_hint):
elif schema_type == "enum":
_, fullname = schema_name(schema, namespace)
- SCHEMA_DEFS[fullname] = schema
+ SCHEMA_DEFS[fullname] = parsed_schema
parsed_schema["name"] = fullname
parsed_schema["symbols"] = schema["symbols"]
elif schema_type == "fixed":
_, fullname = schema_name(schema, namespace)
- SCHEMA_DEFS[fullname] = schema
+ SCHEMA_DEFS[fullname] = parsed_schema
parsed_schema["name"] = fullname
parsed_schema["size"] = schema["size"]
@@ -116,7 +116,7 @@ cdef _parse_schema(schema, namespace, _write_hint):
elif schema_type == "record" or schema_type == "error":
# records
namespace, fullname = schema_name(schema, namespace)
- SCHEMA_DEFS[fullname] = schema
+ SCHEMA_DEFS[fullname] = parsed_schema
fields = []
for field in schema.get('fields', []):

MODIFY
fastavro/_schema_py.py
fastavro/_schema_py.py
@@ -126,14 +126,14 @@ def _parse_schema(schema, namespace, _write_hint):
elif schema_type == "enum":
_, fullname = schema_name(schema, namespace)
- SCHEMA_DEFS[fullname] = schema
+ SCHEMA_DEFS[fullname] = parsed_schema
parsed_schema["name"] = fullname
parsed_schema["symbols"] = schema["symbols"]
elif schema_type == "fixed":
_, fullname = schema_name(schema, namespace)
- SCHEMA_DEFS[fullname] = schema
+ SCHEMA_DEFS[fullname] = parsed_schema
parsed_schema["name"] = fullname
parsed_schema["size"] = schema["size"]
@@ -141,7 +141,7 @@ def _parse_schema(schema, namespace, _write_hint):
elif schema_type == "record" or schema_type == "error":
# records
namespace, fullname = schema_name(schema, namespace)
- SCHEMA_DEFS[fullname] = schema
+ SCHEMA_DEFS[fullname] = parsed_schema
fields = []
for field in schema.get('fields', []):

MODIFY
tests/test_fastavro.py
tests/test_fastavro.py
@@ -1521,3 +1521,50 @@ def test_helpful_error_when_a_single_record_is_passed_to_writer():
fastavro.writer(new_file, schema, record)
assert "argument should be an iterable, not dict" in str(exc)
+
+
+def test_embedded_records_get_namespaced_correctly():
+ schema = {
+ 'namespace': 'test',
+ 'name': 'OuterName',
+ 'type': 'record',
+ 'fields': [{
+ 'name': 'data',
+ 'type': [{
+ 'type': 'record',
+ 'name': 'UUID',
+ 'fields': [{
+ 'name': 'uuid',
+ 'type': 'string'
+ }]
+ }, {
+ 'type': 'record',
+ 'name': 'Abstract',
+ 'fields': [{
+ 'name': 'uuid',
+ 'type': 'UUID',
+ }],
+ }, {
+ 'type': 'record',
+ 'name': 'Concrete',
+ 'fields': [{
+ 'name': 'abstract',
+ 'type': 'Abstract'
+ }, {
+ 'name': 'custom',
+ 'type': 'string',
+ }],
+ }]
+ }]
+ }
+
+ records = [{
+ 'data': {
+ 'abstract': {
+ 'uuid': {'uuid': 'some_uuid'}
+ },
+ 'custom': 'some_string'
+ }
+ }]
+
+ assert records == roundtrip(schema, records)

fix namespace issue with embedded records