swagger-api / swagger-api/swagger-ui

Default `Content-Type` not applied to complex object parts in `multipart/form-data`

Open
#9,548 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
29k
Forks
9.3k
Avg merge
2d 23h
Merged PRs (30d)
25

Description

Q&A
  • OS: macOS
  • Browser: Firefox
  • Version: 122
  • Method of installation: swagger-ui-dist from unpkg
  • Swagger-UI version: 5.11.2
  • Swagger/OpenAPI version: OpenAPI 3.0.3
Content & configuration

Example Swagger/OpenAPI definition:

openapi: 3.0.3
servers:
- url: http://127.0.0.1:8080
paths:
  /profile:
    post:
      requestBody:
        content:
          multipart/form-data:
            schema:
              required:
              - address
              - id
              - profileImage
              type: object
              properties:
                id:
                  type: string
                address:
                  type: object
                  properties:
                    street:
                      type: string
                    city:
                      type: string
                profileImage:
                  type: string
                  format: binary
      responses:
        "200":
          description: OK

Swagger-UI configuration options:

SwaggerUIBundle({
  url: 'http://127.0.0.1:8080/api-docs.yaml',
  dom_id: '#swagger-ui',
  presets: [
    SwaggerUIBundle.presets.apis,
    SwaggerUIStandalonePreset
  ],
  layout: "StandaloneLayout",
})
Describe the bug you're encountering

According to the OpenAPI specification and the docs page, the default Content-Type for complex values (objects) in a multipart/form-data request should be application/json.
However, in the provided example, the address property, which is an object, does not have application/json as its Content-Type in the generated request and curl command.

Currently, it's possible to manually set encoding.address.contentType in the definition as application/json to generate the expected curl command (thanks to issue #5356 was fixed).
However, based on the OpenAPI specification, I believe type=application/json should automatically be applied even if encoding is not explicitly included in the definition.

To reproduce...

Steps to reproduce the behavior:

  1. Fill the blanks for the /profile request
  2. Click on 'Execute'
  3. Generated curl command and payload are:
curl -X 'POST' \
  'http://127.0.0.1:8080/profile' \
  -H 'accept: */*' \
  -H 'Content-Type: multipart/form-data' \
  -F 'id=string' \
  -F 'address={
  "street": "string",
  "city": "string"
}' \
  -F 'profileImage=@zerobyte.txt;type=text/plain'
-----------------------------306494087715324971512255078872
Content-Disposition: form-data; name="id"

string
-----------------------------306494087715324971512255078872
Content-Disposition: form-data; name="address"

{
  "street": "string",
  "city": "string"
}
-----------------------------306494087715324971512255078872
Content-Disposition: form-data; name="profileImage"; filename="zerobyte.txt"
Content-Type: text/plain


-----------------------------306494087715324971512255078872--
Expected behavior
 curl -X 'POST' \
   'http://127.0.0.1:8080/profile' \
   -H 'accept: */*' \
   -H 'Content-Type: multipart/form-data' \
   -F 'id=string' \
   -F 'address={
   "street": "string",
   "city": "string"
-}' \
+};type=application/json' \
   -F 'profileImage=@zerobyte.txt;type=text/plain'
 -----------------------------306494087715324971512255078872
 Content-Disposition: form-data; name="id"
 
 string
 -----------------------------306494087715324971512255078872
 Content-Disposition: form-data; name="address"
+Content-Type: application/json
 
 {
   "street": "string",
   "city": "string"
 }
 -----------------------------306494087715324971512255078872
 Content-Disposition: form-data; name="profileImage"; filename="zerobyte.txt"
 Content-Type: text/plain
 
 
 -----------------------------306494087715324971512255078872--
Additional context or thoughts

EDIT: this is part (maybe duplicate) of #6462

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SwaggerUIBundle reproduction using the supplied OpenAPI 3.0.3 multipart example, then trace request generation for the address object and its generated curl command. Done means complex object parts include an application/json Content-Type while simple fields retain their expected defaults.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.