Datamodel-code-generator: Union of array and null is not infered as optional array

Created on 2 Dec 2020  路  3Comments  路  Source: koxudaxi/datamodel-code-generator

Describe the bug
When inferring a model from a sample JSON, the union of an array ([..., ... ...]) and null is inferred to be Union[Any, List[...]] instead of Optional[List[...]]

To Reproduce

Example JSON:

{
    "items": [
        {
            "oofield": null
        },
        {
            "oofield": [1,2,3]
        }
    ]
}

Used commandline:

$ datamodel-codegen  --input sample.json --input-file-type json --output model.py

Expected behavior
Expected model:

class Item(BaseModel):
    oofield: Optional[List[int]]


class Model(BaseModel):
    items: List[Item]

Actually generated model:

class Item(BaseModel):
    oofield: Union[Any, List[int]]


class Model(BaseModel):
    items: List[Item]

Version:

  • OS: N/A
  • Python version: 3.8.6
  • datamodel-code-generator version: 0.6.8

Additional context
None

released

Most helpful comment

@koxudaxi
I have tried that. It works pretty well. Thanks!

Now I am closing this issue.

All 3 comments

@Gowee
Thank you for creating this issue.
OK, I will implement it.

@Gowee
I have released a new version as 0.6.13
The version is fixed the bug.

@koxudaxi
I have tried that. It works pretty well. Thanks!

Now I am closing this issue.

Was this page helpful?
0 / 5 - 0 ratings