Skip to content

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

Open
@jonnytest1

Description

@jonnytest1

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: [email protected]
  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"
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions