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

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

Open
4 of 6 tasks
jonnytest1 opened this issue Jan 24, 2025 · 1 comment
Open
4 of 6 tasks

Comments

@jonnytest1
Copy link

jonnytest1 commented Jan 24, 2025

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"
}
@jonnytest1
Copy link
Author

an alternative would be to make the models each a class , then the class that is handling the response model can do the Date transformation in the constructor

@jonnytest1 jonnytest1 changed the title [BUG] [typescript-angular] no convertion for Date format in response [BUG] [typescript-angular] no conversion for Date format in response Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant