swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Encoding attribute is not respected on the request

Open alejandrofloresm opened this issue 6 years ago • 11 comments

Q&A

  • OS: macOS
  • Browser: chrome
  • Version: 74
  • Method of installation: composer
  • Swagger-UI version: 3.10.0
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Example Swagger/OpenAPI definition:

openapi: 3.0.0
info:
  title: 'Test Swagger'
  description: 'Test Swagger'
  contact:
    email: [email protected]
  version: '1'
paths:
  /api/v1/profile:
    post:
      tags:
        - Profile
      summary: 'profile of the user user'
      requestBody:
        description: 'Profile that needs to be stored'
        content:
          multipart/mixed:
            schema:
              properties:
                avatar_image:
                  description: avatar_image
                  type: file
                profile:
                  type: object
                  $ref: '#/components/schemas/Profile'
              type: object
            encoding:
              profile:
                contentType: application/json
      responses:
        201:
          description: 'Profile that was registered.'
          content:
            application/json:
              schema:
                type: object
        422:
          description: 'Unprocessable Entity.'
          content:
            application/json:
              schema:
                type: object
      security:
        -
          passport:
            - '*'
components:
  schemas:
    Profile:
      title: Profile
      description: 'profile model'
      required:
        - email
        - telephone
      properties:
        email:
          schema: Profile
          description: 'Email of the profile'
          type: string
        name:
          schema: Profile
          description: 'Name of the profile'
          type: string
      type: object
  securitySchemes:
    passport:
      type: oauth2
      description: 'Laravel passport oauth2 security.'
      in: header
      scheme: http
      flows:
        password:
          authorizationUrl: 'http://swagger.test/oauth/authorize'
          tokenUrl: 'http://swagger.test/oauth/token'
          refreshUrl: 'http://swagger.test/token/refresh'
          scopes: {  }
tags:
  -
    name: Profile
    description: 'Operations for the user profile'

Describe the bug you're encountering

To reproduce...

Steps to reproduce the behavior:

  1. Fill the blanks for the /api/v1/profile request
  2. Make the request
  3. The requests sends the following payload:
------WebKitFormBoundaryaSzepCtBw5Aiaiue
Content-Disposition: form-data; name="avatar_image"; filename="the-avatar_image.png"
Content-Type: image/png


------WebKitFormBoundaryaSzepCtBw5Aiaiue
Content-Disposition: form-data; name="profile"

{
  "email": "[email protected]",
  "name": "my name"
}
------WebKitFormBoundaryaSzepCtBw5Aiaiue-- 

Expected behavior

The payload should be

------WebKitFormBoundaryaSzepCtBw5Aiaiue
Content-Disposition: form-data; name="avatar_image"; filename="the-avatar_image.png"
Content-Type: image/png


------WebKitFormBoundaryaSzepCtBw5Aiaiue
Content-Disposition: form-data; name="profile"
Content-Type: application/json
{
  "email": "[email protected]",
  "name": "my name"
}
------WebKitFormBoundaryaSzepCtBw5Aiaiue-- 

Check that the profile attribute contains it's content-type as "application/json".

alejandrofloresm avatar May 17 '19 01:05 alejandrofloresm

I have the same issue, please solve this.

sungtaek avatar Aug 30 '19 02:08 sungtaek

Related (or duplicate): #4826, #6462

hkosova avatar Sep 02 '19 09:09 hkosova

Any ETA on when will this be fixed?

kargath avatar Nov 26 '19 14:11 kargath

PRs welcome.

webron avatar Nov 26 '19 15:11 webron

Same issue, please solve it

RomanOrlov2 avatar Sep 05 '20 14:09 RomanOrlov2

Have same issue.

XhstormR avatar Nov 15 '20 08:11 XhstormR

Is there a workaround?

Legion2 avatar Nov 25 '20 13:11 Legion2

Same issue here

Warkanlock avatar Mar 24 '21 22:03 Warkanlock

Hello,

Same here. In fact it seems that All the "encoding definition" is NOT taken into account by swagger editor :(

dprincethg avatar Jun 24 '21 13:06 dprincethg

Same problem here :(

tamis-laan avatar Mar 05 '22 08:03 tamis-laan

Refer this workaround: https://github.com/swagger-api/swagger-ui/issues/6462

nikhilraj6692 avatar Jun 26 '22 19:06 nikhilraj6692

We are having same problem. Has this been resolved in SwaggerHub Cloud Enterprise ??

LouCastaldo avatar Nov 09 '22 01:11 LouCastaldo

Hi everybody,

This issue has been addressed upstream in https://github.com/swagger-api/swagger-js/pull/3078


OpenAPI 3.x.y definition:

{
  "openapi": "3.0.0",
  "paths": {
    "/upload/": {
      "post": {
        "tags": [
          "upload"
        ],
        "operationId": "upload",
        "requestBody": {
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "options": {
                    "type": "object",
                    "properties": {
                      "some_array": {
                        "type": "array",
                        "items": {
                          "type": "string"
                        }
                      },
                      "max_bar": {
                        "type": "integer",
                        "default": 300
                      }
                    }
                  }
                }
              },
              "encoding": {
                "options": {
                  "contentType": "application/json; charset=utf-8"
                }
              }
            }
          }
        }
      }
    }
  }
}

Here is how the multipart/form-data request looked BEFORE:

POST / HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="options"

{"some_array":["string"],"max_bar":300}

Here is how the multipart/form-data request looked AFTER:

POST / HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="options"; filename="blob"
Content-Type: application/json

{"some_array":["string"],"max_bar":300}

Every boundary now contains specific Content-Type header if defined. Note that filename is also present in every Content-Disposition header and always contains value of blob. This inclusion of filename cannot be avoided as this is the only way how to assign specific Content-Type for every boundary.

char0n avatar Aug 01 '23 07:08 char0n

Addressed by https://github.com/swagger-api/swagger-ui/pull/9105

char0n avatar Aug 01 '23 13:08 char0n

Addressed by #9105

Hi everybody.

Is any of you still experiencing the issue ? I am using org.webjars:swagger-ui:5.10.3 and the problem does not seem to be resolved. (Should have been fixed in version v5.3.0)

swagger-ui-version Screenshot 2024-02-13 at 10 37 37

LaFrimousse avatar Feb 13 '24 09:02 LaFrimousse