OpenAPITools / OpenAPITools/openapi-generator

[BUG][PYTHON] data-time format returning datetime obj (datetime.datetime(2017, 1, 2, 6, 24, 17, tzinfo=TzInfo(UTC)))) instead of ISO format

Open
#18,292 0 comments 1 reaction 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • [y] Have you validated the input using an OpenAPI validator (example)?
  • [y] Have you tested with the latest master to confirm the issue still exists?
  • [y] Have you searched for related issues/PRs?
  • [y] What's the actual output vs expected output?
    Actual Output: date_time_end=datetime.datetime(2017, 1, 2, 6, 24, 17, tzinfo=TzInfo(UTC)))
    Expected Output: '2017-07-21T17:32:28Z'
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

OpenAPI Generator is not formatting date-time properties as expected in the generated code.
You've specified the format "date-time" with "format: date-time" in the schema, but the output is a datetime object instead of the desired ISO 8601 string format ('YYYY-MM-DDTHH:MM:SSZ')..

openapi-generator version

which version of openapi-generator are you using, is it a regression? v7.4.0

OpenAPI declaration file content or url
dateTimeEnd:
          type: string
          format: date-time
Generation Details
docker run --rm \
-v ${PWD}:/local openapitools/openapi-generator-cli:v7.4.0 generate \
-i  /local/products.yaml \
--package-name productsapi_client \
-g python \
-o /local/python/productsapi_client
cd python/productsapi_client
poetry add parameterized

Tried adding the additional-properties=datetimeFormat=%Y-%m-%dT%H:%M:%SZ flag.

Steps to reproduce

The API response on swagger docs returns this format - %Y-%m-%dT%H:%M:%S%z
Whereas if I create client by schema docs from same API swagger page then client returns - datetime.datetime(2017, 1, 2, 6, 24, 17, tzinfo=TzInfo(UTC)))

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/16284
https://github.com/britned/empire-platform-api/issues/4
https://github.com/OpenAPITools/openapi-generator/issues/14503

Suggest a fix

When I am changing my products.yaml to

        dateTimeEnd:
          type: string
          format: '^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}Z$'

then it seems to work but format: date-time should also work fine.

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 products.yaml schema and the generated Python client produced by the documented v7.4.0 Docker command. Reproduce the date-time response using the provided schema, then trace where the client handles the date-time field and compare the returned datetime object with the expected ISO 8601 string. Done means format: date-time produces the expected string without requiring a regex workaround.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.