OpenAPITools / OpenAPITools/openapi-generator
[REQ] Python client support for additional ISO-8601 datetime formats
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
The python client ("python-nextgen" as of v6.6.0, "python" in v7+) allows specifying the datetimeFormat, but python datetime standard library does not cover the whole ISO-8601 spec, or even RFC-3339. In order for the openapi generator client to be compatible with as many systems as possible, some additional format options should be provided.
The specific issues I would like to address here are:
- Arbitrary fractional seconds precision
Zas a timezone suffix for UTC timezones
Both ISO-8601 and RFC-3339 allow the above two formats, but datetime does not support outputting them and has no intention of supporting them "for simplicity".
For example, take 2023-06-08T04:03:13.578Z. Note the 3 digit (millisecond) precision, and the Z timezone suffix. This is a totally valid ISO-8601 string, and datetime can parse it:
Python 3.11.3 (main, Apr 7 2023, 20:13:31) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
>>> from datetime import datetime
>>> datetime.fromisoformat('2023-06-08T04:03:13.578Z')
datetime.datetime(2023, 6, 8, 4, 3, 13, 578000, tzinfo=datetime.timezone.utc)
However, there is no current possible way to output the same format using the datetime library, due to 2 restrictions:
strftime()only supports 6 digits (microseconds)%f, which is the method used by openapi generator today
a. They offer another methodisoformat(timespec)wheretimespeccan indicate precision, but then this does not allow arbitrary formatting- The only timezone indicactor supported today is the
+00:00format, no support forZsuffix
a. Support is proposed here, but who knows how long it will take to get merged and wont be available in older python releases
b. Usingstrftime(), its possible to add a hardcodedZin the string (i.e.strftime("%Y-%m-%dT%H:%M:%SZ"). However this conflicts with the issue above, which requires usingisoformat(timespec)for franctional precision control
Therefore, it is up to openapi-generator to close the gaps to make datetime more compatible with RFC-3339.
Describe the solution you'd like
To address fractional second precision and Z timezone indicator, add 3 new configOptions to the python generator:
datetimeIsoformat: bool- Iftrue, then instead ofstrftime()here, useisoformat()datetimeIsoformatTimespec: str- IfdatetimeIsoformat=Trueand not empty, then passtimespectoisoformat().datetimeUTCDesignator: bool- Iftrueand resulting datetime string ends in+00:00, then replace+00:00withZ(regardless of the value ofdatetimeIsoformat)
Describe alternatives you've considered
If possible, a single configOption which allows defining custom transformations like [:-3] + 'Z' to strip digits and append Z, or could be used for any possible transformations
Additional context
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/python/api.mustache at the datetime formatting code around line 196, then trace where Python generator configOptions are declared. Implement the three requested options and verify generated clients support fractional precision and the UTC Z designator without breaking existing formatting behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100