Is your feature request related to a problem? Please describe.
We have a lot of timedelta fields in our schemas and would like to take advantage of Pydantic's native handling of timedelta types. However, datamodel-code-generator
does not appear to support outputting timedelta
objects. Native timedelta
support would be a very useful addition.
Pydantic handles timedeltas natively. It also appears that msgspec
added timedelta
support recently (see jcrist/msgspec#475) so I don't think there would be anything blocking adding timedelta
support to all datamodel-code-generator
output types.
Describe the solution you'd like
I've seen two common JSON schema formats to represent timedeltas:
This one is used by Pydantic 1 and serializes the timedeltas as floats of seconds
{
"type": "object",
"properties": {
"granularity": {
"type": "number",
"format": "time-delta"
}
},
"required": ["granularity" ]
}
This one is used by Pydantic 2 and serializes the timedeltas as ISO 8106 duration strings
{
"type": "object",
"properties": {
"granularity": {
"type": "string",
"format": "duration",
"title": "Granularity"
}
},
"required": [
"granularity"
]
}
Ideally, datamodel-code-generator
would understand and output timedelta
fields for both of those schemas.
Describe alternatives you've considered
Leaving the current default behavior and having {"type": "number", "format": "time-delta"}
fall back to just a float
of seconds.
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