1

I am using the latest version of openapi-ui 1.6.7 and I can't make a file upload endpoint work. This is my configuration of the parameter :

    @PostMapping(
        consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
        produces = MediaType.APPLICATION_JSON_VALUE
    )
    @Operation(
        summary = "Create a new FileResource",
        requestBody = @RequestBody(description = "File to upload")
    )
    public ResponseEntity<FileResourceIdPublicApiDto> create(
        @Parameter(
            description = "File to upload",
            required = true
        )
        @RequestPart
        MultipartFile file

When I use the "Try out" button in the generated swagger UI, I get a 415 Unsupported Media Type error. The request headers has content-type : application/x-www-form-urlencoded

I think this is where the error comes from. The generated json from OpenApi looks like this :

{
  "operationId": "create_4",
  "parameters": [
   ...
  ],
  "requestBody": {
    "content": {
      "multipart/form-data": {
        "schema": {
          "required": [
            "file"
          ],
          "type": "object",
          "properties": {
            "file": {
              "type": "string",
              "format": "binary",
              "description": "File to upload"
            }
          }
        }
      }
    },
    "description": "File to upload"
  },
  "responses": {
    "200": {
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/FileResourceId"
          }
        }
      },
      "description": "OK"
    }
  },
  "summary": "Create a new FileResource",
  "tags": [
    "File Resource"
  ]
}

What am I missing to send a correct request with form-data content-type ?

Jonas Kistler
  • 21
  • 1
  • 3

2 Answers2

1

It’s a combination of two things: Defining “consumes = multipart” and using RequestParam instead of RequestPart.

This wasn’t required when using springfox Swagger 2.0.

It’s really irritating that there is no good migration guide written for 2.0 -> 3.0.

saran3h
  • 12,353
  • 4
  • 42
  • 54
0

For me replacing RequestPart to RequestParam did the job! btw I was using openapi-ui 1.6.4.