ipld / ipld/py-ipld-dag

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

Open
#25 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
13
Forks
9
PR merge metrics
No merged PRs in 30d

Description

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`:

```python
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](https://ipld.io/specs/codecs/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()`:

```python
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

- DAG-JSON spec: https://ipld.io/specs/codecs/dag-json/spec/
- File: `dag/codecs/dag_json.py`

Contributor guide

Open the contributing guide

Research direction

Read _prepare_for_json() in dag/codecs/dag_json.py, focusing on the dict-handling path and its current error behavior. Done means non-string map keys are rejected with a ValueError that identifies the key type and value, while string-keyed maps continue through DAG-JSON preparation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.