Skip to content

Version >= 0.38 breaks allof class hierarchy #2645

@simontaurus

Description

@simontaurus

Describe the bug
before v0.38.0 allOf generates a class hierarchy, from v0.38.0 upwards (including latest 0.42.2) this is no longer the case.

To Reproduce

Example schema:

{
  "$defs": {
    "Thing": {
      "title": "Thing",
      "type": "object",
      "required": [
        "type",
        "name"
      ],
      "properties": {
        "type": {
          "type": "string",
          "default": "playground:Thing",
          "options": {
            "hidden": true
          }
        },
        "name": {
          "type": "string",
          "description": "The things name",
          "minLength": 1,
          "default": "A Thing"
        }
      }
    }
  },
  "allOf": [
    {
      "$ref": "#/$defs/Thing"
    }
  ],
  "title": "Person",
  "type": "object",
  "required": [
    "name"
  ],
  "properties": {
    "type": {
      "$comment": "Already defined in playground:Thing -> we override just the default",
      "default": "playground:Person"
    },
    "name": {
      "description": "First and Last name",
      "minLength": 4,
      "default": "John Doe"
    },
    "age": {
      "type": "integer"
    }
  }
}

Used commandline:

datamodel-codegen--input schema.json --output model.py

Expected behavior

*** Result model.py for v0.37.0

class Thing(BaseModel):
    type: str
    name: constr(min_length=1) = Field(..., description='The things name')


class Person(Thing):
    type: Optional[Any] = 'playground:Person'
    name: Any = Field(..., description='First and Last name')
    age: Optional[int] = None

*** Result model.py for >=v0.38.0

class Person(BaseModel):
    type: str
    name: constr(min_length=1) = Field(..., description='The things name')
    age: Optional[int] = None


class Thing(BaseModel):
    type: str
    name: constr(min_length=1) = Field(..., description='The things name')

Version:

  • OS: Win11
  • Python version: 3.12
  • datamodel-code-generator version: >=v0.38.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions