Datamodel-code-generator: Incorrect code is generated from nested schemas

Created on 15 Jan 2021  路  2Comments  路  Source: koxudaxi/datamodel-code-generator

Describe the bug
If you generate code from similar schema definitions with nested structures, some schema definitions may be ignored.

To Reproduce

Example schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "ObjectA": {
      "properties": {
        "data": {
          "items": {
            "properties": {
              "keyA": {
                "type": "string"
              }
            },
            "type": "object"
          },
          "type": "array"
        }
      },
      "type": "object"
    },
    "ObjectB": {
      "properties": {
        "data": {
          "items": {
            "properties": {
              "keyB": {
                "type": "string"
              }
            },
            "type": "object"
          },
          "type": "array"
        }
      },
      "type": "object"
    }
  }
}

Used commandline:

$ datamodel-codegen --input schema.json

Expected behavior
For an object with keyA and keyB, we expect two objects to be created.
However, only the Datum with keyB was generated.

class Model(BaseModel):
    __root__: Any


class Datum(BaseModel):
    keyA: Optional[str] = None


class Datum1(BaseModel):
    keyB: Optional[str] = None


class ObjectA(BaseModel):
    data: Optional[List[Datum]] = None


class ObjectB(BaseModel):
    data: Optional[List[Datum1]] = None

Actual behavior

class Model(BaseModel):
    __root__: Any


class Datum(BaseModel):
    keyB: Optional[str] = None


class ObjectA(BaseModel):
    data: Optional[List[Datum]] = None


class ObjectB(BaseModel):
    data: Optional[List[Datum]] = None

Version:

  • OS: macOS 10.15.7
  • Python version: 3.8.3
  • datamodel-code-generator version: 0.6.18
released

Most helpful comment

@koxudaxi
Thank you for fixed and released.
I tried the new version and confirm that it works as expected.

All 2 comments

@yacchi
Thank you for creating this issue.
I fixed it. I have released a new version as 0.6.19

@koxudaxi
Thank you for fixed and released.
I tried the new version and confirm that it works as expected.

Was this page helpful?
0 / 5 - 0 ratings