Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openapi generator angular not generating multipart formdata endpoint correctly. (useForm is false by default)

This is the endpoint I have:

@PostMapping(value = "/file-upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public List<FileReference> handleFileUpload(
        @RequestPart(value = "file", name = "file") MultipartFile[] file, @ApiIgnore HttpSession session) {
    return service.handleFileUpload(
            Arrays.stream(file).map(MultipartFileWithUUID::new).collect(Collectors.toList()),
            session);
}

This is the generated endpoint in the swagger.json (swagger 2.0):

...
  "post": {
        "tags": [
          "damage-report-controller"
        ],
        "summary": "handleFileUpload",
        "operationId": "handleFileUploadUsingPOST",
        "consumes": [
          "multipart/form-data"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "file",
            "in": "formData",
            "required": false,
            "type": "array",
            "items": {
              "type": "file"
            },
            "collectionFormat": "multi"
          }
        ],
...

And here is the generated function:

 public handleFileUploadUsingPOST(file?: Array<Blob> ...) {

let headers = this.defaultHeaders;

header settings etc...

        // to determine the Content-Type header
        const consumes: string[] = [
            'multipart/form-data'
        ];


        const canConsumeForm = this.canConsumeForm(consumes);

        let formParams: { append(param: string, value: any): any; };
        let useForm = false;

...
        if (useForm) {
            formParams = new FormData();
        } else {
            formParams = new HttpParams({encoder: this.encoder});
        }
...
}

The error I have is 415: Unsupported media type.

I don't know how it should be generated correctly, but I changed let useForm; to true and it works, so my guess that let useForm = canConsumeForm(consumes) because canConsumeForm returns a boolean.

What should I change so it gets generated correctly?

like image 326
Róbert Kocsis Avatar asked Jan 27 '26 20:01

Róbert Kocsis


1 Answers

In case anyone reads this, I haven't found the proper solution using swagger 2.0, so I updated to openapi 3.0 and that fixed the problem.

Apparently swagger 2.0 doesn't support uploading an array of files, even though the only problem was that the generated service didn't use existing functions properly.

like image 176
Róbert Kocsis Avatar answered Jan 29 '26 10:01

Róbert Kocsis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!