datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Incorrect model output for JSON Schemas with nested arrays and `--field-constraints`

Open kylebebak opened this issue 2 years ago • 0 comments

Describe the bug

  • Running dmcg with --field-constraints on JSON Schema with nested arrays produces incorrect output for all model types
  • Nested type information is lost, and replaced with typing.Any

To Reproduce

Example schema:

{
  "type": "object",
  "properties": {
    "a": {
      "type": "array",
      "items": {
        "type": "array",
        "items": {"type": "number"},
        "minItems": 2,
        "maxItems": 2
      }
    }
  },
  "required": ["a"]
}

The following produces correct output, i.e. a has type List[List[float]]:

datamodel-codegen --input test.json --input-file-type jsonschema --output-model-type typing.TypedDict --output model.py
datamodel-codegen --input test.json --input-file-type jsonschema --output model.py  # This of course doesn't use `pydantic.Field` in model output

The following does not, i.e. a has type List[AItem], where AItem = List[Any]:

datamodel-codegen --input test.json --input-file-type jsonschema --output-model-type typing.TypedDict --output model.py --field-constraints
datamodel-codegen --input test.json --input-file-type jsonschema --output model.py --field-constraints

Expected behavior

Passing --field-constraints shouldn't "erase" the type of the nested array items, i.e. we should get AItem = List[float] instead of AItem = List[Any]

Version:

  • OS: MacOS 13.4.1
  • Python version: 3.11.3
  • datamodel-code-generator version 0.21.5

kylebebak avatar Sep 12 '23 19:09 kylebebak