Openapi-generator: [typescript] Dates are reported as Date type in model but not serialized

Created on 29 Aug 2018  路  8Comments  路  Source: OpenAPITools/openapi-generator

Description

When the format of a string property of a model is "date-time" or "date", the generated model in Typescript have that property of Date type, but no conversion is performed after the fetch of the response, so that property is actually a string. I've tried both with typescript-angular and typescript-fetch.

openapi-generator version

3.2.2

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: TestApi
  version: 1.0.0
paths:
  /test:
    get:
      summary: Test
      operationId: testApi
      responses:
        "200":
          description: Ok
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Response"
components:
  schemas:
    Response:
      properties:
        id:
          type: string
        timestamp:
          type: string
          format: date-time
Command line used for generation
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v3.2.2 generate -i /local/swagger.yaml -g typescript-angular -o /local/ts-angular -DngVersion=6.0
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v3.2.2 generate -i /local/swagger.yaml -g typescript-fetch -o /local/ts-fetch
Suggest a fix/enhancement

There are two possible solutions:

  • Adding a conversion mechanism for converting string values to their right type. This is done for example by the javascript generator (with the function convertToType)
  • Use string as a type for date-time and date properties

I think that the first solution is the most convenient for the end user, but it is no very simple to implement. I would implement it once for every typescript generator (issue https://github.com/OpenAPITools/openapi-generator/issues/802). For now, the second solution should be used.

Most helpful comment

I added support for this in my fork on #569 today. It serializes and deserializes dates based on new Date(value) and date.toISOString().

I think any work in #802 should make use of date format specifiers and allow a wider range of date strings to work, as well as a flag to disable automatic date handling (i.e. use strings instead) for users with complex use-cases.

All 8 comments

@Fredx87 you are right, dates should be deserialized or the type should be string.
In our project, we choose the second option with the following configuration in the pom.xml:

                                <configuration>
                                    <language>typescript-angular</language>
                                     ...
                                    <typeMappings>
                                        <typeMapping>DateTime=string</typeMapping>
                                    </typeMappings>
                                </configuration>

But I think the type should be changed to string by default. Or maybe there is an easy way to deserialize the string into Date objects?
@TiFu @Place1 what is your opinion on that?

@Fredx87 would you like to give it a try and support @TiFu in his typescript-refactoring #802?

I added support for this in my fork on #569 today. It serializes and deserializes dates based on new Date(value) and date.toISOString().

I think any work in #802 should make use of date format specifiers and allow a wider range of date strings to work, as well as a flag to disable automatic date handling (i.e. use strings instead) for users with complex use-cases.

fixed with #569

@macjohnny, the original issue reported here mentions both typescript-angular and typescript-fetch.
However, #569 only fixes typescript-fetch.
It seems that the issue with typescript-angular still exists. Am I missing anything here?

With typescript-angular there is no serialization/deserialization, so you need to add a typemapping and map it to strong, as mentioned above.

With typescript-angular there is no serialization/deserialization, so you need to add a typemapping and map it to strong, as mentioned above.

I believe the typemapping for date / date-time must be string in the abstract typescript codegen.
And only specific ts generators that really implement serialization/deserialization (like typescript-fetch) should map to Date type in typemapping by default

@amakhrov I agree, only generators that implement a conversion should use Date

Was this page helpful?
0 / 5 - 0 ratings