OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.77k stars 6.57k forks source link

[BUG][PYTHON] Object of type datetime is not JSON serializable even with additional-properties=datetimeFormat='%Y-%m-%dT%H:%M:%SZ'" #19517

Open pdefoort opened 2 months ago

pdefoort commented 2 months ago

Release : openapi-generator-cli.jar 7.8.0 OS : Windows server

In my OAS 2.0 file / I have the following property definition

      expectedBeginDate:
        type: string
        format: date-time
Description

Server wants ISO8601 pattern "AAAA-MM-JJTHH:MM:SSZ"

In my python script, I set the property expectedBeginDate in an instance of object model generated by client generator with a string respecting the pattern above (iso8601).

But when I call the to_json() method on the generated model I get "TypeError: Object of type datetime is not JSON serializable".

However I use in command line of openapi-generator-cli.jar the configuration option datetimeFormat as defined in https://openapi-generator.tech/docs/generators/python/ => additional-properties=datetimeFormat='%Y-%m-%dT%H:%M:%SZ'".

Syntax get from issue : 18292 https://github.com/OpenAPITools/openapi-generator/issues/18292

I thought that the option datetimeformat would provide a string in JSON for expectedBeginDate property.

Is it a bug or is it by design ?

Workaround used : "expectedBeginDate": { "type": "string", "format": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$" },

retroandchill commented 2 months ago

I'm seeing this issue as well. It seems like json.dumps is having issues serializing the date and datetime types.