fix issue where _inject_schema was confused about a previously injected schema
MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -334,10 +334,9 @@ cdef _inject_schema(outer_schema, inner_schema, namespace="", is_injected=False)
if outer_schema == inner_schema["name"]:
return inner_schema, True
else:
- raise Exception(
- "Internal error; "
- + "You should raise an issue in the fastavro github repository"
- )
+ # Hit a named schema that has already been loaded previously. Return
+ # the outer_schema so we keep looking
+ return outer_schema, is_injected
else:
# Remaining valid schemas must be dict types
schema_type = outer_schema["type"]

MODIFY
fastavro/_schema_py.py
fastavro/_schema_py.py
@@ -510,10 +510,9 @@ def _inject_schema(outer_schema, inner_schema, namespace="", is_injected=False):
if outer_schema == inner_schema["name"]:
return inner_schema, True
else:
- raise Exception(
- "Internal error; "
- + "You should raise an issue in the fastavro github repository"
- )
+ # Hit a named schema that has already been loaded previously. Return
+ # the outer_schema so we keep looking
+ return outer_schema, is_injected
else:
# Remaining valid schemas must be dict types
schema_type = outer_schema["type"]

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

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

ADD
tests/load_schema_test_10/C.avsc
+{
+ "name": "C",
+ "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_10/D.avsc
+{
+ "name": "D",
+ "type": "record",
+ "fields": [
+ {"name": "bar", "type": "string"}
+ ]
+}
\ No newline at end of file

MODIFY
tests/test_schema.py
tests/test_schema.py
@@ -834,3 +834,60 @@ def test_load_schema_resolve_namespace_fixed():
],
}
assert loaded_schema == expected_schema
+
+
+def test_load_schema_bug():
+ """https://github.com/fastavro/fastavro/issues/494"""
+ load_schema_dir = join(abspath(dirname(__file__)), "load_schema_test_10")
+ schema_path = join(load_schema_dir, "A.avsc")
+ loaded_schema = fastavro.schema.load_schema(schema_path, _write_hint=False)
+
+ expected_schema = {
+ "name": "A",
+ "type": "record",
+ "fields": [
+ {
+ "name": "b",
+ "type": [
+ "null",
+ {
+ "name": "B",
+ "type": "record",
+ "fields": [{"name": "foo", "type": "string"}],
+ },
+ ],
+ "default": None,
+ },
+ {
+ "name": "c",
+ "type": [
+ "null",
+ {
+ "name": "C",
+ "type": "record",
+ "fields": [
+ {
+ "name": "b",
+ "type": ["null", "B"],
+ "default": None,
+ },
+ {
+ "name": "d",
+ "type": [
+ "null",
+ {
+ "name": "D",
+ "type": "record",
+ "fields": [{"name": "bar", "type": "string"}],
+ },
+ ],
+ "default": None,
+ },
+ ],
+ },
+ ],
+ "default": None,
+ },
+ ],
+ }
+ assert loaded_schema == expected_schema

fix issue where _inject_schema was confused about a previously injected schema