handle case where an enum or fixed is found but already resolved
MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -356,6 +356,12 @@ cdef _inject_schema(outer_schema, inner_schema, namespace="", is_injected=False)
outer_schema["values"] = return_schema
return outer_schema, injected
+ elif schema_type == "enum":
+ return outer_schema, is_injected
+
+ elif schema_type == "fixed":
+ return outer_schema, is_injected
+
elif schema_type == "record" or schema_type == "error":
# records
namespace, _ = schema_name(outer_schema, namespace)
@@ -379,3 +385,9 @@ cdef _inject_schema(outer_schema, inner_schema, namespace="", is_injected=False)
elif schema_type in PRIMITIVES:
return outer_schema, is_injected
+
+ else:
+ raise Exception(
+ "Internal error; "
+ + "You should raise an issue in the fastavro github repository"
+ )

MODIFY
fastavro/_schema_py.py
fastavro/_schema_py.py
@@ -532,6 +532,12 @@ def _inject_schema(outer_schema, inner_schema, namespace="", is_injected=False):
outer_schema["values"] = return_schema
return outer_schema, injected
+ elif schema_type == "enum":
+ return outer_schema, is_injected
+
+ elif schema_type == "fixed":
+ return outer_schema, is_injected
+
elif schema_type == "record" or schema_type == "error":
# records
namespace, _ = schema_name(outer_schema, namespace)
@@ -555,3 +561,9 @@ def _inject_schema(outer_schema, inner_schema, namespace="", is_injected=False):
elif schema_type in PRIMITIVES:
return outer_schema, is_injected
+
+ else:
+ raise Exception(
+ "Internal error; "
+ + "You should raise an issue in the fastavro github repository"
+ )

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

ADD
tests/load_schema_test_8/namespace.B.avsc
+{
+ "type": "enum",
+ "name": "B",
+ "namespace": "namespace",
+ "symbols": ["THIS", "THAT"]
+}
\ No newline at end of file

ADD
tests/load_schema_test_8/namespace.C.avsc
+{
+ "type": "enum",
+ "name": "C",
+ "namespace": "namespace",
+ "symbols": ["AND_THIS", "AND_THAT"]
+}
\ No newline at end of file

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

ADD
tests/load_schema_test_9/namespace.B.avsc
+{
+ "name": "namespace.B",
+ "type": "fixed",
+ "size": 4
+}
\ No newline at end of file

ADD
tests/load_schema_test_9/namespace.C.avsc
+{
+ "name": "namespace.C",
+ "type": "fixed",
+ "size": 8
+}
\ No newline at end of file

MODIFY
tests/test_schema.py
tests/test_schema.py
@@ -772,3 +772,65 @@ def test_load_schema_resolve_namespace():
],
}
assert loaded_schema == expected_schema
+
+
+def test_load_schema_resolve_namespace_enums():
+ """https://github.com/fastavro/fastavro/issues/490"""
+ load_schema_dir = join(abspath(dirname(__file__)), "load_schema_test_8")
+ schema_path = join(load_schema_dir, "A.avsc")
+ loaded_schema = fastavro.schema.load_schema(schema_path, _write_hint=False)
+
+ expected_schema = {
+ "name": "namespace.A",
+ "type": "record",
+ "fields": [
+ {
+ "name": "b",
+ "type": {
+ "name": "namespace.B",
+ "type": "enum",
+ "symbols": ["THIS", "THAT"],
+ },
+ },
+ {
+ "name": "c",
+ "type": {
+ "name": "namespace.C",
+ "type": "enum",
+ "symbols": ["AND_THIS", "AND_THAT"],
+ },
+ },
+ ],
+ }
+ assert loaded_schema == expected_schema
+
+
+def test_load_schema_resolve_namespace_fixed():
+ """https://github.com/fastavro/fastavro/issues/490"""
+ load_schema_dir = join(abspath(dirname(__file__)), "load_schema_test_9")
+ schema_path = join(load_schema_dir, "A.avsc")
+ loaded_schema = fastavro.schema.load_schema(schema_path, _write_hint=False)
+
+ expected_schema = {
+ "name": "namespace.A",
+ "type": "record",
+ "fields": [
+ {
+ "name": "b",
+ "type": {
+ "name": "namespace.B",
+ "type": "fixed",
+ "size": 4,
+ },
+ },
+ {
+ "name": "c",
+ "type": {
+ "name": "namespace.C",
+ "type": "fixed",
+ "size": 8,
+ },
+ },
+ ],
+ }
+ assert loaded_schema == expected_schema

handle case where an enum or fixed is found but already resolved