Róbert Kocsis
Róbert Kocsis

Reputation: 63

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?

Upvotes: 3

Views: 2014

Answers (1)

R&#243;bert Kocsis
R&#243;bert Kocsis

Reputation: 63

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.

Upvotes: 1

Related Questions