Skip to content

multipart/form-data with mixed @RequestParts fails to provide correct Content-Type in HTTP request #396

Closed
@stefan-huettemann

Description

@stefan-huettemann

Hi. Great work with springdoc-openapi!

PROBLEM

With sprindoc 1.2.29 and spring-boot 2.2.4 I run into a problem with a rest controller:

@RequestMapping(
            method = PUT,
            consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
            produces = {MediaType.APPLICATION_JSON_VALUE}
    )
public ResponseEntity<?> put(
            @PathVariable("config") final String config,
            @RequestPart(value = "configuration") final Configuration configuration,
            @RequestPart(value = "file") final MultipartFile aFile) {
        // -> Configuration is any data object that will be uploaded as JSON
    }

spring-doc produces the following swagger config:

"requestBody": {
      "content": {
        "multipart/form-data": {
          "schema": {
            "type": "object",
            "properties": {
              "configuration": {
                "$ref": "#/components/schemas/Configuration"
              },
              "file": {
                "type": "string",
                "format": "binary"
              }
            }
          }
        }
      }
    }

which results in a HTTP multipart request of the form:

PUT /sasmd/api/v1/configurations/266f8aa5-1a78-4a95-afae-2f5774cda90a HTTP/1.1

<-- redacted-->

Content-Type: multipart/form-data; boundary=---------------------------13787589471453071099581927209

<-- redacted-->

-----------------------------13787589471453071099581927209
Content-Disposition: form-data; name="configuration"

{
  "date": "2019-12-17T10:45:52.727+0100",
  "some": "json"
}
-----------------------------13787589471453071099581927209
Content-Disposition: form-data; name="file"; filename="UKF-121.zip"
Content-Type: application/zip

<file-contents-base64-removed>

-----------------------------13787589471453071099581927209--

This fails with spring boot 415: HttpMediaTypeNotSupportedException: Content type 'application/octet-stream'

The correct HTTP request would be to have a Content-Type: application/json declaration for the first part:

PUT /sasmd/api/v1/configurations/266f8aa5-1a78-4a95-afae-2f5774cda90a HTTP/1.1

<-- redacted-->

Content-Type: multipart/form-data; boundary=---------------------------13787589471453071099581927209

<-- redacted-->

-----------------------------13787589471453071099581927209
Content-Disposition: form-data; name="configuration"
Content-Type: application/json

{
  "date": "2019-12-17T10:45:52.727+0100",
  "some": "json"
}
-----------------------------13787589471453071099581927209
Content-Disposition: form-data; name="file"; filename="UKF-121.zip"
Content-Type: application/zip

<file-contents-base64-removed>

-----------------------------13787589471453071099581927209--

Questions:

  1. I have no idea what the correct swagger config would look like to provide a Content-Type: application/jsonfor the first part
  2. I fail to overwrite swagger config using swaggers own @RequestBody annotation...

Working Solution in swagger config

The following swagger config would provide a possible solution

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

This result in a swagger-ui with two file-upload buttons. Using a file-upload button the content-type of the mutlipart-request gets set (if the user uploads a .JSON file for parameter "configuration").

BUT: as said above - I fail to produce any such swagger configuration using swagger annotations.

Well ... any idea how to resolve this?

Best,
-Stefan

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions