openapi-typescript-code-generator icon indicating copy to clipboard operation
openapi-typescript-code-generator copied to clipboard

Bug: Union keys with union response types don't work when each response status has different media type

Open tkow opened this issue 1 year ago • 0 comments

Steps To Reproduce

Remove this comment out , switch yaml from remote instead of local.

run bun i && bun ./gen.ts && npx tsc --noEmit ./src/client.ts in repo.

/accounts/{accountId}/urlscanner/scan/{scanId}/screenshot:
  get:
   ...
    200:
      content:
        "image/png":
    202:
      content:
        "application/json":

This generate types

type ResponseContentType$urlscanner$get$scan$screenshot = "image/png" |  "application/json
export const urlscanner$get$scan$screenshot = <RequestOption>(apiClient: ApiClient<RequestOption>) => <ResponseContentType extends ResponseContentType$urlscanner$get$scan$screenshot>(params: Params$urlscanner$get$scan$screenshot<ResponseContentType>, option?: RequestOption): Promise<(Response$urlscanner$get$scan$screenshot$Status$200 | Response$urlscanner$get$scan$screenshot$Status$202)[ResponseContentType]>

This emits an error because ResponseContentType keys require both types above to have all properties .

The current behavior

As ever described.

The expected behavior

Generate each ResponseContentType and union

<200ResponseContentType, 202ResponseContentType> ...
  Promise<
    Response$urlscanner$get$scan$screenshot$Status$200[200ResponseContentType] | 
    Response$urlscanner$get$scan$screenshot$Status$202)[202ResponseContentType]
>

or

  Promise<
    Response$urlscanner$get$scan$screenshot$Status$200[keyof  Response$urlscanner$get$scan$screenshot$Status$200] | 
    Response$urlscanner$get$scan$screenshot$Status$202[keyof Response$urlscanner$get$scan$screenshot$Status$202]
>

or

  <ResponseContentType extends {200: keyof 200Response, 202: keyof 202Response}>
  Promise<
    Response$urlscanner$get$scan$screenshot$Status$200[ResponseContentType['200']] | 
    Response$urlscanner$get$scan$screenshot$Status$202)[ResponseContentType['202']]
>

Final approach looks suitable for me.

tkow avatar Feb 12 '24 09:02 tkow