fix bug where load schema was creating unions of unions
MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -260,8 +260,14 @@ cdef _load_schema(schema, schema_dir):
raise e
if isinstance(schema, dict):
+ if isinstance(sub_schema, list):
+ return _load_schema(sub_schema + [schema], schema_dir)
+ else:
return _load_schema([sub_schema, schema], schema_dir)
else:
# schema is already a list
+ if isinstance(sub_schema, list):
+ return _load_schema(sub_schema + schema, schema_dir)
+ else:
schema.insert(0, sub_schema)
return _load_schema(schema, schema_dir)

MODIFY
fastavro/_schema_py.py
fastavro/_schema_py.py
@@ -387,8 +387,14 @@ def _load_schema(schema, schema_dir):
raise e
if isinstance(schema, dict):
+ if isinstance(sub_schema, list):
+ return _load_schema(sub_schema + [schema], schema_dir)
+ else:
return _load_schema([sub_schema, schema], schema_dir)
else:
# schema is already a list
+ if isinstance(sub_schema, list):
+ return _load_schema(sub_schema + schema, schema_dir)
+ else:
schema.insert(0, sub_schema)
return _load_schema(schema, schema_dir)

ADD
tests/load_schema_test/A.avsc
+{
+ "name": "A",
+ "type": "record",
+ "fields": [
+ {"name": "b", "type": ["null", "B"], "default": null},
+ {"name": "d", "type": ["null", "D"], "default": null}
+ ]
+}
\ No newline at end of file

ADD
tests/load_schema_test/B.avsc
+{
+ "name": "B",
+ "type": "record",
+ "fields": [
+ {"name": "c", "type": ["null", "C"], "default": null}
+ ]
+}
\ No newline at end of file

ADD
tests/load_schema_test/C.avsc
+{
+ "name": "C",
+ "type": "record",
+ "fields": [
+ {"name": "foo", "type": "string"}
+ ]
+}
\ No newline at end of file

ADD
tests/load_schema_test/D.avsc
+{
+ "name": "D",
+ "type": "record",
+ "fields": [
+ {"name": "bar", "type": "string"}
+ ]
+}
\ No newline at end of file

ADD
tests/load_schema_test_2/A.avsc
+[
+ {
+ "name": "A",
+ "type": "record",
+ "fields": [
+ {"name": "b", "type": ["null", "B"], "default": null}
+ ]
+ },
+ {
+ "name": "D",
+ "type": "record",
+ "fields": [
+ {"name": "baz", "type": "string"}
+ ]
+ }
+]
\ No newline at end of file

ADD
tests/load_schema_test_2/B.avsc
+{
+ "name": "B",
+ "type": "record",
+ "fields": [
+ {"name": "c", "type": ["null", "C"], "default": null}
+ ]
+}
\ No newline at end of file

ADD
tests/load_schema_test_2/C.avsc
+{
+ "name": "C",
+ "type": "record",
+ "fields": [
+ {"name": "foo", "type": "string"}
+ ]
+}
\ No newline at end of file

MODIFY
tests/test_fastavro.py
tests/test_fastavro.py
@@ -261,6 +261,24 @@ def test_missing_schema():
fastavro.schema.load_schema(schema_path)
+def test_load_schema_does_not_make_unions_of_unions():
+ """https://github.com/fastavro/fastavro/issues/443"""
+ load_schema_dir = join(abspath(dirname(__file__)), 'load_schema_test')
+ schema_path = join(load_schema_dir, 'A.avsc')
+ loaded_schema = fastavro.schema.load_schema(schema_path)
+ for schema in loaded_schema:
+ assert not isinstance(schema, list)
+
+
+def test_load_schema_does_not_make_unions_of_unions_2():
+ """https://github.com/fastavro/fastavro/issues/443"""
+ load_schema_dir = join(abspath(dirname(__file__)), 'load_schema_test_2')
+ schema_path = join(load_schema_dir, 'A.avsc')
+ loaded_schema = fastavro.schema.load_schema(schema_path)
+ for schema in loaded_schema:
+ assert not isinstance(schema, list)
+
+
def test_default_values():
schema = {
"type": "record",

fix bug where load schema was creating unions of unions