be able to load top level names
MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -315,7 +315,9 @@ cdef _load_schema(schema, schema_dir, named_schemas, write_hint, injected_schema
raise e
if sub_schema["name"] not in injected_schemas:
- _inject_schema(schema, sub_schema)
+ injected_schema = _inject_schema(schema, sub_schema)
+ if isinstance(schema, str) or isinstance(schema, list):
+ schema = injected_schema[0]
injected_schemas.add(sub_schema["name"])
return _load_schema(
schema, schema_dir, schema_copy, write_hint, injected_schemas

MODIFY
fastavro/_schema_py.py
fastavro/_schema_py.py
@@ -506,7 +506,9 @@ def _load_schema(schema, schema_dir, named_schemas, write_hint, injected_schemas
raise e
if sub_schema["name"] not in injected_schemas:
- _inject_schema(schema, sub_schema)
+ injected_schema = _inject_schema(schema, sub_schema)
+ if isinstance(schema, str) or isinstance(schema, list):
+ schema = injected_schema[0]
injected_schemas.add(sub_schema["name"])
return _load_schema(
schema, schema_dir, schema_copy, write_hint, injected_schemas

ADD
tests/load_schema_test_13/A.avsc
+"B"
\ No newline at end of file

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

ADD
tests/load_schema_test_14/A.avsc
+"string"
\ No newline at end of file

ADD
tests/load_schema_test_15/A.avsc
+["B", "C"]
\ No newline at end of file

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

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

MODIFY
tests/test_schema.py
tests/test_schema.py
@@ -952,3 +952,48 @@ def test_load_schema_ordered():
],
}
assert loaded_schema == expected_schema
+
+
+def test_load_schema_top_level_names():
+ """https://github.com/fastavro/fastavro/issues/527"""
+ load_schema_dir = join(abspath(dirname(__file__)), "load_schema_test_13")
+ schema_path = join(load_schema_dir, "A.avsc")
+ loaded_schema = fastavro.schema.load_schema(schema_path, _write_hint=False)
+
+ expected_schema = {
+ "name": "B",
+ "type": "record",
+ "fields": [{"name": "foo", "type": "string"}],
+ }
+ assert loaded_schema == expected_schema
+
+
+def test_load_schema_top_level_primitive():
+ """https://github.com/fastavro/fastavro/issues/527"""
+ load_schema_dir = join(abspath(dirname(__file__)), "load_schema_test_14")
+ schema_path = join(load_schema_dir, "A.avsc")
+ loaded_schema = fastavro.schema.load_schema(schema_path, _write_hint=False)
+
+ expected_schema = "string"
+ assert loaded_schema == expected_schema
+
+
+def test_load_schema_union_names():
+ """https://github.com/fastavro/fastavro/issues/527"""
+ load_schema_dir = join(abspath(dirname(__file__)), "load_schema_test_15")
+ schema_path = join(load_schema_dir, "A.avsc")
+ loaded_schema = fastavro.schema.load_schema(schema_path, _write_hint=False)
+
+ expected_schema = [
+ {
+ "name": "B",
+ "type": "record",
+ "fields": [{"name": "foo", "type": "string"}],
+ },
+ {
+ "name": "C",
+ "type": "record",
+ "fields": [{"name": "bar", "type": "string"}],
+ },
+ ]
+ assert loaded_schema == expected_schema

be able to load top level names