Skip to content

[BUG] [typescript-fetch] Invalid handling of date return type #23487

@tomsontom

Description

@tomsontom

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

If you define the response type of an API to return something of "type":"string" and "format":"date" the generated exposes the value as Date but looking at the source the JSON-Content is used as is and hence the returned value is of type "string".

openapi-generator version

7.22-SNAPSHOT

OpenAPI declaration file content or url
{
  "openapi": "3.0.1",
	"info": {
		"title": "RSD Test",
		"version": "1.0.0"
  },
  "paths": {
	"/api/date": {
      "get": {
        "description": "",
        "parameters": [],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "string",
                  "format": "date"
                }
              }
            }
          }
        }
      }
    }
  }
}
Generation Details

I generated using

docker run --rm \ 
-v $PWD:/local openapitools/openapi-generator-cli:latest generate \
-i /local/openapi.json \
-g typescript-fetch --additional-properties=importFileExtension=.js \
-o /local/src
Steps to reproduce
  • Run the generator
  • Inspect the generated source

The relevant code part generated from above is

    async apiDateGetRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Date>> {
        const requestOptions = await this.apiDateGetRequestOpts();
        const response = await this.request(requestOptions, initOverrides);

        if (this.isJsonMime(response.headers.get('content-type'))) {
            return new runtime.JSONApiResponse<Date>(response);
        } else {
            return new runtime.TextApiResponse(response) as any;
        }
    }
Related issues/PRs

None I'm aware of

Suggest a fix

If the return value is a "Date" a transformer has to be passed so the line should look like

return new runtime.JSONApiResponse<Date>(response, isoDate => new Date(isoDate));

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