Skip to content

[Code scan] JSON Schema uses array items for dict-style repeated arguments #118

Description

@njzjz

This issue was found by a Codex global code scan of the repository.

Affected code:

dargs/dargs/json_schema.py

Lines 116 to 128 in b4db564

if not argument.repeat:
data["properties"] = properties
data["required"] = required
if allof:
data["allOf"] = allof
else:
data["items"] = {
"type": "object",
"properties": properties,
"required": required,
}
if allof:
data["items"]["allOf"] = allof

Problem:
For repeat=True, schema generation always puts subfield validation under items. That works for list-style repeated arguments, but for dict-style repeated arguments the top-level type is "object"; JSON Schema ignores items on objects.

Reproducer:

from dargs import Argument
from dargs.json_schema import generate_json_schema
from jsonschema import validate

arg = Argument("base", dict, [Argument("sub1", int), Argument("sub2", str)], repeat=True)
schema = generate_json_schema(arg)
validate({"item1": {"sub1": 1, "sub2": None}}, schema)
validate({"item1": {"sub1": 1}}, schema)
print(schema)

Observed behavior:
Both invalid objects validate because the generated schema has type: ["object"] plus items, and the item schema is ignored.

Expected behavior:
Dict-style repeated arguments should use object-key validation, for example additionalProperties or patternProperties, so every repeated entry is checked against the subfield schema.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    In Progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions