Skip to content

Commit

Permalink
fix namespace handling
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbelden committed May 4, 2023
1 parent 215105c commit 2bc6f77
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
10 changes: 5 additions & 5 deletions fastavro/_schema.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ cpdef schema_name(schema, parent_ns):
)

namespace = schema.get("namespace", parent_ns)
if not namespace:
return namespace, name
elif "." in name:
return "", name
else:
if "." in name:
return name.rsplit(".", 1)[0], name
elif namespace:
return namespace, f"{namespace}.{name}"
else:
return "", name


cpdef expand_schema(schema):
Expand Down
10 changes: 5 additions & 5 deletions fastavro/_schema_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ def schema_name(schema: DictSchema, parent_ns: str) -> Tuple[str, str]:
)

namespace = schema.get("namespace", parent_ns)
if not namespace:
return namespace, name
elif "." in name:
return "", name
else:
if "." in name:
return name.rsplit(".", 1)[0], name
elif namespace:
return namespace, f"{namespace}.{name}"
else:
return "", name


def expand_schema(schema: Schema) -> Schema:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,3 +1334,24 @@ def test_special_case_defaults(field_type, default_value):
}

fastavro.parse_schema(schema)


def test_namespace_respected():
"""https://github.com/fastavro/fastavro/issues/690"""
schema = {
"type": "record",
"name": "explicit_namespace.foo",
"fields": [
{
"name": "field1",
"type": {
"type": "record",
"name": "bar",
"fields": [{"name": "bar_field", "type": "int"}],
},
},
{"name": "field2", "type": "explicit_namespace.bar"},
],
}

fastavro.parse_schema(schema)

0 comments on commit 2bc6f77

Please sign in to comment.