Datamodel-code-generator: Unexpected behavior with allOf and type: object

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

Describe the bug
Hi again! Now I'm trying to generate pydantic model from the schema including allOf and "type": "object" fields at the top level.
And I got an unexpected model.

To Reproduce

Example schema:


{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "test",
  "description": "test",
  "type": "object",
  "allOf": [
    {
      "$ref": "base_test.json#/definitions/first"
    }
  ]
}

base_test.json:


{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "base_test.json",
  "description": "base test",
  "type": "object",
  "definitions": {
    "first": {
      "type": "object",
      "required": [
        "second"
      ],
      "properties": {
        "second": {
          "type": "string",
          "description": "Second",
          "examples": [
            "second"
          ]
        }
      }
    }
  }
}

Used commandline:

$ datamodel-codegen --input test.json --class-name Test --output model.py --input-file-type jsonschema

Generated model:

# generated by datamodel-codegen:
#   filename:  test.json
#   timestamp: 2021-01-25T19:11:09+00:00

from __future__ import annotations

from pydantic import BaseModel, Field


class Test(BaseModel):
    """
    test
    """

    pass


class First(BaseModel):
    second: str = Field(..., description='Second', examples=['second'])

Expected behavior
The problem can be solved by removing "type": "object" field:


{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "test",
  "description": "test",
  "allOf": [
    {
      "$ref": "base_test.json#/definitions/first"
    }
  ]
}

Generated model:

# generated by datamodel-codegen:
#   filename:  test.json
#   timestamp: 2021-01-25T19:15:56+00:00

from __future__ import annotations

from pydantic import BaseModel, Field


class First(BaseModel):
    second: str = Field(..., description='Second', examples=['second'])


class Test(First):
    """
    test
    """

    pass

Version:

  • OS: Ubuntu 16.04
  • Python version: 3.7
  • datamodel-code-generator version: 0.6.25
bug

Most helpful comment

@koxudaxi , it is working as expected. Thank you!

All 2 comments

@MikeZaharov
Thank you for your report.
I have fixed the problem.
And, I released a new version 0.6.26

@koxudaxi , it is working as expected. Thank you!

Was this page helpful?
0 / 5 - 0 ratings