OpenAPITools / OpenAPITools/openapi-generator

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

Open
#19,517 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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$"
},

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated Python model's to_json() path and the datetimeFormat additional property, using the reported expectedBeginDate date-time example as the reproduction. Compare how a string assigned to a date-time field is represented before serialization and check the Python generator documentation for the intended option behavior. Done means the behavior is documented or corrected and covered by a focused regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, python
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.