-
-
Notifications
You must be signed in to change notification settings - Fork 422
Description
Describe the bug
When the OpenAPI specification uses type: string with format: date-time, the generated Pydantic model allows integer values for the date-time field, even though according to the OpenAPI specification (and RFC 3339), only valid ISO 8601 date-time strings should be accepted. This means values like TestObject(-1) or TestObject(1234) are incorrectly accepted as valid date-time.
See OpenAPI and RFC 3339.
To Reproduce
Example :
openapi: 3.1.1
info:
title: Test API
version: 1.0.0
paths: {}
components:
schemas:
TestObject:
description: My test object
type: string
format: date-time
using this command
$ datamodel-codegen --input openapi.yaml --input-file-type openapi --output openapi.py --output-model-type pydantic_v2.BaseModel
this is generated:
from __future__ import annotations
from pydantic import AwareDatetime, Field, RootModel
class TestObject(RootModel[AwareDatetime]):
root: AwareDatetime = Field(..., description='My test object')
With this model, it is possible to do:
TestObject(-1) # This should raise a validation error but does not.Expected behavior
When the OpenAPI specification uses format: date-time, the generated Pydantic model should only accept valid ISO 8601 date-time strings.
Only valid ISO 8601 strings should be accepted for the date-time variant, and negative integers should not be accepted at all.
Version:
- OS: Linux
- Python version: 3.12
- datamodel-code-generator version: 0.33.3