OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-angular] no conversion for Date format in response

Open
#20,536 1 comment 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?
  • 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

using typescipt-angular doesn transform date-time string formats to Date , even though its typed as Date

openapi-generator version
OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Swagger Petstore - OpenAPI 3.0
  termsOfService: http://swagger.io/terms/
  contact:
    email: apiteam@swagger.io
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.11
paths:
  /pet:
    get:
      tags:
        - pet
      summary: Finds Pets by status
      description: Multiple status values can be provided with comma separated strings
      operationId: get
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                 $ref: '#/components/schemas/Pet'
components:
  schemas:
    Pet:
      type: object
      properties:
        time:
          type: string
          format: date-time
Generation Details

typescript-angular

Steps to reproduce

openapi-generator-cli generate

Related issues/PRs
Suggest a fix

in https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache
pipe the response into a map() and convert all the date-time formated fields to a Date


      type DatePathObject = "date" | {
          [path: string]: DatePathObject
      }


     /** the idea for the datePathObject is that its an object that has all the date formats that are in the response spec to we only transform those that are listed
      */
    private mapResponseRecursive(response, datePathObject: DatePathObject): any {
        if (response == null) {
            return response;
        }

        if (typeof response === "object" && typeof datePathObject === "object") {
            if (Array.isArray(response)) {
                // instead of [number] there should probably something more unique - maybe even a symbol
                return response.map(subEntry => this.mapResponseRecursive(subEntry, datePathObject[`[number]`]))
            }
            for (const key of Object.keys(response)) {
                if (key in datePathObject) {
                    response[key] = this.mapResponseRecursive(response[key], datePathObject[key])
                }
            }
        }
        if (typeof response == "string" && datePathObject == "date") {
            return new Date(response)
        }
        return response
    }



      return this.httpClient.request{{^isResponseFile}}<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>{{/isResponseFile}}('{{httpMethod}}', `${this.configuration.basePath}${localVarPath}`,
            {
               .... 
            }
        )
       .pipe(map(response=> this.mapResponseRecursive(response,/**get path data from spec*/)))

for the example spec above the datePathObject would be this:

{
   "time":  "date"
}


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 modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache and reproduce the issue using the OpenAPI declaration in the report with openapi-generator-cli generate. Compare the generated client’s typed date-time response with its runtime value; done means date-time fields in the response are converted to Date values as described, with the generated client still matching the schema types.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.