Skip to content

DAG-JSON doesn't reject non-string map keys — violates DAG-JSON spec #25

Description

@sumanjeet0012

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions