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

[BUG] [JAVASCRIPT] Response type File no work

Open Askeiter opened this issue 3 years ago • 3 comments

Bug Report Checklist

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

I am trying to download an xlsx file, generating the client api for a vuejs v2 project, but the autogenerated response type is not processed by the generated client api class

openapi-generator version

I have updated to the latest version of "@openapitools/openapi-generator-cli": "^2.5.2",

OpenAPI declaration file content or url
{
  "/custom-forecast/{hotelId}/excel":{
     "get":{
        "tags":[
           "Custom Forecast"
        ],
        "operationId":"getCustomForecastByHotelId",
        "parameters":[
           {
              "name":"hotelId",
              "in":"path",
              "required":true,
              "schema":{
                 "type":"integer",
                 "format":"int32"
              }
           }
        ],
        "responses":{
           "200":{
              "description":"OK",
              "content":{
                 "application/octet-stream":{
                    "schema":{
                       "type":"string",
                       "format":"binary"
                    }
                 }
              }
           }
        }
     },
     "put":{
        "tags":[
           "Custom Forecast"
        ],
        "operationId":"updateCustomForecastByHotelId",
        "parameters":[
           {
              "name":"hotelId",
              "in":"path",
              "required":true,
              "schema":{
                 "type":"integer",
                 "format":"int32"
              }
           }
        ],
        "requestBody":{
           "content":{
              "multipart/form-data":{
                 "schema":{
                    "required":[
                       "excel"
                    ],
                    "type":"object",
                    "properties":{
                       "excel":{
                          "type":"string",
                          "format":"binary"
                       }
                    }
                 }
              }
           }
        },
        "responses":{
           "200":{
              "description":"OK"
           }
        }
     }
  }
}
Generation Details

To generate the code, run the following command node node_modules/@openapitools/openapi-generator-cli/main.js generate -i openapi.json -o src/openapi/ -g javascript -c openapi-generator-config.json

Steps to reproduce

the previous command generates the api class to call back the method to obtain the file declares that the response type is a File type image the only statement i could find is the one shown below that extends blob image

when I execute the call it ends up calling the ApiClient class image

In this class, nothing is done with the return type defined and it delegates the transformation of the response to the deserialize method.

image

but this engine is unable to infer the type and ends up returning the response.text image image

Suggest a fix

I understand that if the code generator says that the return type is a file, take that into account, or allow for the expected type as an optional parameter when generating the calling method.

Askeiter avatar Nov 07 '22 16:11 Askeiter

I finally had to do a mix with the code generated by openapi and make a javascript fetch call for it to work correctly

image

let url = customForecatApi.apiClient.buildUrl(
            '/custom-forecast/{hotelId}/excel',
            {
              hotelId: this.$root.hotelId,
            },
            null
          )
          let auth = customForecatApi.apiClient.authentications['bearer-token']
          let token = typeof auth.accessToken === 'function' ? auth.accessToken() : auth.accessToken
          fetch(url, {
            method: 'GET',
            headers: new Headers({
              Authorization: 'Bearer ' + token,
              accept: 'application/octet-stream',
            }),
          })
            .then((response) => {
              return response.blob()
            })
            .then((blob) => {
              var url = window.URL.createObjectURL(blob)
              var a = document.createElement('a')
              a.href = url
              a.download = `customForecastFromHotel${this.$root.hotelId}.xlsx`
              document.body.appendChild(a) // we need to append the element to the dom -> otherwise it will not work in firefox
              a.click()
              a.remove() //afterwards we remove the element again
            })

If I use the generated code from openapi, the response is a text that I have not been able to save correctly as an xlxs

image

Askeiter avatar Nov 07 '22 18:11 Askeiter

I can confirm we are hitting the exact same issue which makes it impossible to make multipart/form-data requests with any kind of file/buffer or binary data, which is a pretty severe bug.

apagano-vue avatar May 16 '24 15:05 apagano-vue

I think this is the same issue I have trying to serve mp3 files..

DanTulovsky avatar Oct 18 '24 20:10 DanTulovsky