OpenAPITools / OpenAPITools/openapi-generator
[BUG] [typescript-fetch] Array of date-time items does not get mapped to Date Objects contrary to as the type indicates
Nobody has claimed this yet.
- 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?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Currently (tested with the docker images v6.2.0 and :latest) if I have an array of date-time items in any property in a component, it's values are not mapped to JS/TS Date objects as is the case with singular date-time properties (i.e. not in an array).
This would be okay, if the generated type would indicate the correct type. It indicates a Date-Array though, letting us dev's make wrong assumptions, the linter not catching the error, eventually leading to a runtime error, because the value returned from the backend was never converted to a Date object.
openapi-generator version
We are using v6.2.0 but the issue still exists in the :latest docker image.
OpenAPI declaration file content or url
---
openapi: 3.0.1
info:
title: Example API V1
version: v1
paths:
"/":
get:
responses:
200:
description: Returns a Test object
content:
application/json:
schema:
"$ref": "#/components/schemas/Test"
components:
schemas:
Test:
type: object
properties:
my_date_time:
type: string
format: date-time
my_date_time_array:
type: array
items:
type: string
format: date-time
Generation Details
If I run the following command on the schema above:
docker run --platform linux/amd64 openapitools/openapi-generator-cli:latest generate --generator-name typescript-fetch --additional-properties=typescriptThreePlus=true --input-spec /local/swagger/service_test.yaml --model-name-prefix TestApi --output /local/services/test/api-client
I get new model file with the name of TestApiTest.ts generated.
It contains the following iterface:
export interface TestApiTest {
/**
*
* @type {Date}
* @memberof TestApiTest
*/
myDateTime?: Date;
/**
*
* @type {Array<Date>}
* @memberof TestApiTest
*/
myDateTimeArray?: Array<Date>;
}
and the following function to convert it from JSON:
export function TestApiTestFromJSONTyped(json: any, ignoreDiscriminator: boolean): TestApiTest {
if ((json === undefined) || (json === null)) {
return json;
}
return {
'myDateTime': !exists(json, 'my_date_time') ? undefined : (new Date(json['my_date_time'])),
'myDateTimeArray': !exists(json, 'my_date_time_array') ? undefined : json['my_date_time_array'],
};
}
In this function, the single date-time field gets wrapped in a new Date() call, whereas the date-time array just gets taken from the json data, without converting it's values to Date objects.
Steps to reproduce
1.) Save the yaml above to a file called 'service_test.yaml'
2.) Run the docker command above (adjust the paths to your needs)
3.) Check the generated model in the output directory
Related issues/PRs
No similar issue could be found.
Suggest a fix
The function to convert from JSON should read something like this:
export function TestApiTestFromJSONTyped(json: any, ignoreDiscriminator: boolean): TestApiTest {
if ((json === undefined) || (json === null)) {
return json;
}
return {
'myDateTime': !exists(json, 'my_date_time') ? undefined : (new Date(json['my_date_time'])),
'myDateTimeArray': !exists(json, 'my_date_time_array') ? undefined : json['my_date_time_array'].map((value: string) => new Date(value)),
};
}
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 running the documented Docker generation command with the supplied service_test.yaml and inspect the generated TestApiTest.ts, especially TestApiTestFromJSONTyped. Compare the singular and array date-time handling; done means generated date-time arrays are converted to Date objects while preserving the declared Array type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100