OpenAPITools / OpenAPITools/openapi-generator
[BUG][Typescript fetch] JSON-serialization does not obay timezone for date fields
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
I'm located in Austria which is GMT+1 and in daylight-saving period it is GMT+2. When I have this API definition
openapi: 3.0.0
...
components:
schemas:
MemberApplicationForm:
type: object
properties:
birthdate:
type: string
format: date
and set a Javascript Date new Date('2022-06-05T00:00:00') then the toISOString() method returns 2022-06-04T22:00:00.000Z which is used as a bases for date formatting in JSON. Therefore the date is '2022-06-04' instead of '2022-06-05'!
Files affected:
- modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache
- modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache
openapi-generator version
6.0.0
OpenAPI declaration file content or url
Generation Details
Maven build:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.0.0</version>
<executions>
<execution>
<id>generate-administration-api</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api/administration/v1.yaml</inputSpec>
<output>${project.build.directory}/generated-sources</output>
<generatorName>spring</generatorName>
<apiPackage>at.demo.administration.api.v1</apiPackage>
<modelPackage>at.demo.administration.api.v1</modelPackage>
<generateSupportingFiles>true</generateSupportingFiles>
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
<configOptions>
<sourceFolder>openapi</sourceFolder>
<interfaceOnly>true</interfaceOnly>
<useSpringController>true</useSpringController>
<dateLibrary>java8</dateLibrary>
<useBeanValidation>true</useBeanValidation>
</configOptions>
<addCompileSourceRoot>true</addCompileSourceRoot>
</configuration>
</execution>
</executions>
</plugin>
Steps to reproduce
See description
Related issues/PRs
This PR also belongs to date/time in Typescript fetch but does not fix that issue: https://github.com/OpenAPITools/openapi-generator/pull/11685/commits/2cde020568701b92b3a347d26fc8e746c172bbb0#
Suggest a fix
const offset = yourDate.getTimezoneOffset()
yourDate = new Date(yourDate.getTime() - (offset*60*1000))
return yourDate.toISOString().substr(0,10);
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 by reading modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache and modelGeneric.mustache, then reproduce the date serialization case from the issue using a local timezone with daylight saving. Trace how date fields are converted through toISOString(). Done means generated TypeScript fetch clients serialize a local date without shifting it to the previous calendar day.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100