Describe the bug
When I use --use-annotated
, the default value for a RootModel field is a string. I would expect it to be the RootModel("value")
To Reproduce
Example schema:
{
"$defs": {
"AdminStateLeaf": {
"$ref": "#/$defs/AdminStateType",
"title": "AdminStateLeaf"
},
"AdminStateType": {
"$ref": "#/$defs/EnumerationEnum",
"title": "AdminStateType"
},
"EnumerationEnum": {
"enum": [
"enable",
"disable"
],
"title": "EnumerationEnum",
"type": "string"
}
},
"description": "List of configured device interfaces",
"properties": {
"interfaces:admin-state": {
"$ref": "#/$defs/AdminStateLeaf",
"default": "enable"
}
},
"title": "InterfacesListEntry",
"type": "object"
}
Used commandline:
$ datamodel-codegen --input-file-type jsonschema --input simple.json --output-model-type pydantic_v2.BaseModel --output model_simple.py --use-annotated
Expected behavior
I expect the default value to be the RootModel. Expected model (difference is in InterfacesListEntry
):
from __future__ import annotations
from enum import Enum
from typing import Optional
from pydantic import BaseModel, Field, RootModel
from typing_extensions import Annotated
class EnumerationEnum(Enum):
enable = 'enable'
disable = 'disable'
class AdminStateType(RootModel[EnumerationEnum]):
root: Annotated[EnumerationEnum, Field(title='AdminStateType')]
class AdminStateLeaf(RootModel[AdminStateType]):
root: Annotated[AdminStateType, Field(title='AdminStateLeaf')]
class InterfacesListEntry(BaseModel):
interfaces_admin_state: Annotated[
AdminStateLeaf, Field(AdminStateLeaf("enable"), alias="interfaces:admin-state")
]
Generated model:
from __future__ import annotations
from enum import Enum
from typing import Optional
from pydantic import BaseModel, Field, RootModel
from typing_extensions import Annotated
class EnumerationEnum(Enum):
enable = 'enable'
disable = 'disable'
class AdminStateType(RootModel[EnumerationEnum]):
root: Annotated[EnumerationEnum, Field(title='AdminStateType')]
class AdminStateLeaf(RootModel[AdminStateType]):
root: Annotated[AdminStateType, Field(title='AdminStateLeaf')]
class InterfacesListEntry(BaseModel):
interfaces_admin_state: Annotated[
Optional[AdminStateLeaf], Field(alias='interfaces:admin-state')
] = 'enable'
Version:
Additional context
The following are examples of the generated model (in the first part, the value is a string and not a RootModel):
>>> import model_simple
>>> model_simple.InterfacesListEntry.parse_obj({})
InterfacesListEntry(interfaces_admin_state='enable')
>>> model_simple.InterfacesListEntry.parse_obj({"interfaces:admin-state":"disable"})
InterfacesListEntry(interfaces_admin_state=AdminStateLeaf(root=AdminStateType(root=<EnumerationEnum.disable: 'disable'>)))
>>> model_simple.InterfacesListEntry.parse_obj({"interfaces:admin-state":"enable"})
InterfacesListEntry(interfaces_admin_state=AdminStateLeaf(root=AdminStateType(root=<EnumerationEnum.enable: 'enable'>)))
>>>
This is the same test using the expected code.
>>> import model_simple
>>> model_simple.InterfacesListEntry.parse_obj({})
InterfacesListEntry(interfaces_admin_state=AdminStateLeaf(root=AdminStateType(root=<EnumerationEnum.enable: 'enable'>)))
>>> model_simple.InterfacesListEntry.parse_obj({"interfaces:admin-state":"disable"})
InterfacesListEntry(interfaces_admin_state=AdminStateLeaf(root=AdminStateType(root=<EnumerationEnum.disable: 'disable'>)))
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too