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.57k stars 6.52k forks source link

[REQ] Python client support for additional ISO-8601 datetime formats #16284

Open jasondamour opened 1 year ago

jasondamour commented 1 year ago

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:

  1. Arbitrary fractional seconds precision
  2. Z as 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:

  1. strftime() only supports 6 digits (microseconds) %f, which is the method used by openapi generator today a. They offer another method isoformat(timespec) where timespec can indicate precision, but then this does not allow arbitrary formatting
  2. The only timezone indicactor supported today is the +00:00 format, no support for Z suffix 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. Using strftime(), its possible to add a hardcoded Z in the string (i.e. strftime("%Y-%m-%dT%H:%M:%SZ"). However this conflicts with the issue above, which requires using isoformat(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:

  1. datetimeIsoformat: bool - If true, then instead of strftime() here, use isoformat()
  2. datetimeIsoformatTimespec: str - If datetimeIsoformat=True and not empty, then pass timespec to isoformat().
  3. datetimeUTCDesignator: bool - If true and resulting datetime string ends in +00:00, then replace +00:00 with Z (regardless of the value of datetimeIsoformat)

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

jasondamour commented 1 year ago

@spacether or @krjakbrjak, Could I kindly bug you for your thoughts and triage? If no issue with the proposal, I'll begin implementing it.

jasondamour commented 1 year ago

Oh, I started looking into the src more closely for the implementation, and realized theres more to it! The datetimeFormat configOption is only applied to queryParams. I'm particularly interested in fixing this for bodyParams, but it probably needs to apply to datetime fields in any param location!