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

[jsonschema -> Pydantic V2] Models generated by objects with $ref ignore additional keywords.

Open ilovelinux opened this issue 8 months ago • 0 comments
trafficstars

Describe the bug

datamodel-code-generator doesn't merge the properties of the referenced array with the properties given by the additional keywords, which is allowed by the jsonschema specification.

For more details & reproducible example: https://github.com/ilovelinux/datamodel-code-generator-issue-2330

To Reproduce

Example schema:

inputs/commons.schema.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/commons.schema.json",
  "title": "Commons",
  "description": "Commons objects",
  "$defs": {
    "defaultArray": {
      "type": "array",
      "minLength": 1,
      "maxLength": 100
    }
  }
}

inputs/products.schema.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/products.schema.json",
  "title": "Products",
  "description": "The products in the catalog",
  "$ref": "commons.schema.json#/$defs/defaultArray",
  "items": {
    "type": "string"
  }
}

Used commandline:

$ datamodel-codegen --input inputs/ --input-file-type jsonschema --output outputs/ --output-model-type pydantic_v2.BaseModel

Expected behavior

The products model's root, is supposed to be a list of [1, 100] string elements, it is instead a generic list of [1, 100] elements.

Version:

  • OS: Linux
  • Python version: Python 3.12
  • datamodel-code-generator version: 0.28.2

Additional context

For more details & reproducible example: https://github.com/ilovelinux/datamodel-code-generator-issue-2330

ilovelinux avatar Mar 03 '25 12:03 ilovelinux