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

Type annotation falls back to Any if property schema is partially overwridden

Open simontaurus opened this issue 1 year ago • 0 comments

Describe the bug Importing a schema and partial overwrite it (e.g., set a different default value) leads to lost type information

To Reproduce

schemaA

{
    "type": "object",
    "title": "SchemaA",
    "properties": {
        "test": {
            "title": "test",
            "type": "string",
            "default": "default_value1"
        }
    }
}

schemaB.json

{
    "allOf": [
        {
            "$ref": "SchemaA.json"
        }
    ],
    "type": "object",
    "title": "SchemaB",
    "properties": {
        "test": {
            "default": "default_value2"
        }
    }
}

Used commandline:

$ datamodel-codegen --input schemaB.json --output model.py

Result model.py: type of test in SchemaB is resetted to Any

class SchemaA(BaseModel):
    test: Optional[str] = Field('default_value1', title='test')

class SchemaB(SchemaA):
    test: Optional[Any] = 'default_value2'

Expected behavior type of test in SchemaB is still str

class SchemaA(BaseModel):
    test: Optional[str] = Field('default_value1', title='test')

class SchemaB(SchemaA):
    test: Optional[str] = 'default_value2'

Version:

  • OS: Windows 10
  • Python version: 3.11 | 3.9
  • datamodel-code-generator version: 0.25.6 | 0.26.0

simontaurus avatar Sep 08 '24 14:09 simontaurus