Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript] Dates are reported as Date type in model but not serialized #926

Closed
Fredx87 opened this issue Aug 29, 2018 · 14 comments
Closed

Comments

@Fredx87
Copy link

Fredx87 commented Aug 29, 2018

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 #802). For now, the second solution should be used.

@macjohnny
Copy link
Member

macjohnny commented Aug 30, 2018

@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?

@macjohnny
Copy link
Member

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

@Place1
Copy link
Contributor

Place1 commented Aug 30, 2018

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.

@macjohnny
Copy link
Member

fixed with #569

@amakhrov
Copy link
Contributor

@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?

@macjohnny
Copy link
Member

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

@amakhrov
Copy link
Contributor

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

@macjohnny
Copy link
Member

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

@kasperpeulen
Copy link
Contributor

This issue is still not fixed if you use withoutRuntimeChecks in typescript-fetch.

@johnwc
Copy link

johnwc commented Aug 6, 2022

We are using typescript-axios, and we have an interceptor that handles the de/serialization of the Date object. How do we tell the typescript-axios generator to use Date type instead of string?

@Phyllon
Copy link

Phyllon commented Oct 12, 2022

I still have the issue with "@openapitools/openapi-generator-cli": "2.5.2", and typescript-fetch. Is there anything I can do?

@dominiksta
Copy link

dominiksta commented Dec 19, 2022

This issue still seems to persist for arrays of date-time formatted strings. (Using the docker version 6.2.1).

Should anyone else stumble upon this issue using the CLI version instead of maven, you can still "fix" this by appending --type-mappings=Date=string to your generator command.

@lavalamp-
Copy link

lavalamp- commented Aug 16, 2023

This remains a problem!!

I have an object definition as follows:

{
    "required": [
        "summary",
        "dates"
    ],
    "type": "object",
    "properties": {
        "summary": {
            "title": "Summary",
            "type": "string",
            "minLength": 1
        },
        "dates": {
            "type": "array",
            "items": {
                "type": "string",
                "format": "date"
            }
        }
    }
}

The resulting typescript-fetch generated code does not correctly decode the date array:

export function NoteSummarizationEntryFromJSONTyped(
  json: any,
  ignoreDiscriminator: boolean
): NoteSummarizationEntry {
  if (json === undefined || json === null) {
    return json;
  }
  return {
    summary: json["summary"],
    dates: json["dates"],
  };
}

It should instead do something along the lines of...

export function NoteSummarizationEntryFromJSONTyped(
  json: any,
  ignoreDiscriminator: boolean
): NoteSummarizationEntry {
  if (json === undefined || json === null) {
    return json;
  }
  return {
    summary: json["summary"],
    dates: json["dates"].map((date: string) => new Date(date)),
  };
}

@NinjaPerson24119
Copy link

NinjaPerson24119 commented Feb 5, 2024

I just ran into this problem myself, so it's still not fixed. Dates in my responses were throwing errors because they were actually strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants