OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
22.02k stars 6.6k forks source link

[BUG] [JAVASCRIPT] Response type File no work #13947

Open Askeiter opened 2 years ago

Askeiter commented 2 years ago

Bug Report Checklist

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 commented 2 years ago

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

apagano-vue commented 6 months ago

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.

DanTulovsky commented 1 month ago

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