3

I'm working on a Spring boot application which has a controller to help upload Multipart files:

@PostMapping("/files")
public ResponseEntity<?> uploadFiles(
        @RequestParam("file") MultipartFile[] file, String comment) 
        throws IOException, ExecutionException, InterruptedException {
    log.debug("Total files to store: {}", file.length);
    log.debug("comment: {}", comment);
    fileService.storeFile(Arrays.asList(file), comment);
    return ResponseEntity.ok(environment.getProperty("file.upload.success"));
}

Problem: Somehow OpenDocAPI (swagger) doesn't understand this payload as file.

It shows this field as String if I mention @RequestParam("file") MultipartFile file or String[] if I use array of MultipartFiles.

My Spring boot parent:

<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>

The spring doc openapi dependency:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.9</version>
</dependency>

The swagger page:

enter image description here

The result page when I click on Try it out:

enter image description here

The Execute button doesn't work

Any idea what am I missing in the controller?

PS - I tried with mentioning:

@PostMapping(value = "/files", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})

But still OpenAPI would only treat it as String.

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
sanjeev
  • 458
  • 1
  • 6
  • 15
  • The `file` parameter showing up as having the "string" type is correct (in OAS 3.0, files are defined as binary strings). What's incorrect is that it shows up as a query parameter instead of a request body field. Should the annotation maybe be [`@RequestPart`](https://springdoc.org/#is-file-upload-supported) instead of `@RequestParam`? – Helen Jul 15 '22 at 08:45
  • Tried with @RequestPart as well. Same behavior and the "Execute" button doesn't work. – sanjeev Jul 20 '22 at 14:53
  • 2
    Hi. Any updates on this issue? – ILya Cyclone Nov 15 '22 at 14:35

2 Answers2

0

You could use @RequestPart with MediaType.MULTIPART_FORM_DATA_VALUE.

Can be combined with @Parameter from OpenAPI (io.swagger.v3.oas.annotations):

@PostMapping("/files", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<?> uploadFiles(
    @Parameter(content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE))
    @RequestPart(value = "file") MultipartFile file,
    @RequestPart(value = "comment", required = false ) String comment
) throws IOException, ExecutionException, InterruptedException {
    log.debug("Total files to store: {}", file.length);
    log.debug("comment: {}", comment);
    fileService.storeFile(Arrays.asList(file), comment);
    return ResponseEntity.ok(environment.getProperty("file.upload.success"));
}

On swagger-ui should show multipart/form-data selected on right:

enter image description here


Reference:

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
0

Along the line of Rafael's post, this worked for me:

public ResponseEntity<String> xslTransform(
           @Parameter(description = "Upload a xsl and a xml file", content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE))
           @RequestPart(value = "Upload 2 files", required = true)
           MultipartFile[] files) {
........
   }

I use openapi version 1.6.15. the only issue is that I cannot set the maximum number of items in the array. Users might add more items than necessary.

anh
  • 1