quicktype
quicktype copied to clipboard
Python: JSON schema string formats date/time use datetime.datetime instead of datetime.date/datetime.time
Given the following input JSON schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/Example",
"definitions": {
"Example": {
"type": "object",
"additionalProperties": false,
"properties": {
"d": { "type": "string", "format": "date" },
"t": { "type": "string", "format": "time" },
"dt": { "type": "string", "format": "date-time" }
},
"required": ["d","t","dt"]
}
}
}
The Python generator generates a class like this:
@dataclass
class Example:
d: datetime
dt: datetime
t: datetime
# methods snipped
It uses datetime.datetime as type for all properties, even though I think datetime.date and datetime.time would be more appropriate, respectively.
In particular, serializing an instance of the generated class to a dict produces:
>>> Example(d=datetime.utcnow(),t=datetime.utcnow(),dt=datetime.utcnow()).to_dict()
{'d': '2022-07-20T18:59:48.036119', 'dt': '2022-07-20T18:59:48.036124', 't': '2022-07-20T18:59:48.036124'}
AFAICT, this does not match the JSON schema.
Is there a particular reason why the Python generator treats date-time, date and time the same?