The DAG-JSON specification requires all map keys to be strings. _prepare_for_json() passes through non-string keys without validation.
Problem
In dag/codecs/dag_json.py:
def _prepare_for_json(node: Any) -> Any:
if is_cid(node):
return {_LINK_KEY: str(node)}
if isinstance(node, dict):
result = {}
for k, v in node.items():
result[k] = _prepare_for_json(v)
# ← Non-string keys are passed through without validation
return result
# ...
The DAG-JSON spec states:
"Map keys MUST be strings."
Additionally, json.dumps() will raise TypeError for non-string keys, but the error message won't mention DAG-JSON spec compliance.
Proposed Solution
Add validation in _prepare_for_json():
def _prepare_for_json(node: Any) -> Any:
if is_cid(node):
return {_LINK_KEY: str(node)}
if isinstance(node, dict):
result = {}
for k, v in node.items():
if not isinstance(k, str):
raise ValueError(
f"DAG-JSON map keys must be strings, got {type(k).__name__}: {k!r}"
)
result[k] = _prepare_for_json(v)
return result
# ...
Related
The DAG-JSON specification requires all map keys to be strings.
_prepare_for_json()passes through non-string keys without validation.Problem
In
dag/codecs/dag_json.py:The DAG-JSON spec states:
Additionally,
json.dumps()will raiseTypeErrorfor non-string keys, but the error message won't mention DAG-JSON spec compliance.Proposed Solution
Add validation in
_prepare_for_json():Related
dag/codecs/dag_json.py