TCH
TCH

Reputation: 604

HTTP error 415 Unsupported Media Type with service generated with OpenApi generator

I use openapi-generator-cli in an Angular project to generate all services from a Java API.

// package.json
"@openapitools/openapi-generator-cli": "2.11.0"
$ openapi-generator-cli version
5.3.0

It works great in most of the cases, but I struggle to make it works for a file upload. I always get an HTTP error 415 Unsupported Media Type
Java (Spring boot) endpoint looks like this

    @PostMapping(value = "file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        public void importFile(@RequestParam("file") MultipartFile file) {
            this.importController.importFile(file);
        }

Open api descriptor from this endpoint looks like this

    "requestBody": {
              "content": {
                "multipart/form-data": {
                  "schema": {
                    "required": ["file"],
                    "type": "object",
                    "properties": { "file": { "type": "string", "format": "binary" } }
                  }
                }
              }
            },

Generated service code in js (Angular project) looks like this


    public importFile(
        file: Blob,
        observe: any = 'body',
        reportProgress: boolean = false,
        options?: { httpHeaderAccept?: undefined; context?: HttpContext }
      ): Observable<any> {   
        // some code about HTTP Headers
        // some code about HttpContext
    
        // theses variables seem to be unused !
        const consumes: string[] = ['multipart/form-data'];
        const canConsumeForm = this.canConsumeForm(consumes);
    
        let localVarFormParams: { append(param: string, value: any): any };
        let localVarUseForm = false; // always false
        let localVarConvertFormParamsToString = false; // always false
        if (localVarUseForm) {
          localVarFormParams = new FormData();
        } else {
          // always pass here
          localVarFormParams = new HttpParams({ encoder: this.encoder });
        }
    
        // some code about response type
    
        return this.httpClient.post<any>(
          `${this.configuration.basePath}/api/priv/imports/file`,
          localVarConvertFormParamsToString ? localVarFormParams.toString() : localVarFormParams,
          {
            // options
          }
        );
      }

If I manually call the Java endpoint with the following code, it works perfectly

const formData: FormData = new FormData();
formData.append('file', file);
this.httpClient.post(`${this.configuration.basePath}/api/priv/imports/file`, formData)

But if I call the generated service from OpenApi, then I get the HTTP error 415 Unsupported Media Type :

const formData: FormData = new FormData();
formData.append('file', file);
this.importResourceService.importFile(formData as any)

I noticed that in the generated service code, some variables are unused or always false, localVarFormParams does not even contains the file.
If I just replace localVarConvertFormParamsToString ? localVarFormParams.toString() : localVarFormParams by file then it works but I do not want to update the generated code manually !
So I wonder what I could change to make OpenApi to generate a working code to upload file on server.

Upvotes: 1

Views: 348

Answers (0)

Related Questions