swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.49k stars 8.96k forks source link

Multipart request doc example for OpenAPI 3.0 fails with err "not of type 'object' - 'address'" #7560

Open chrisinmtown opened 3 years ago

chrisinmtown commented 3 years ago

Using Python3.8, flask 1.1.4, connexion 2.9.0 and swagger-ui-bundle 0.0.9 I cannot make the example work from https://swagger.io/docs/specification/describing-request-body/multipart-requests/ and I pasted that spec below. Please forgive if this is a dupe, I searched other open issues for multipart but didn't spot an exact match to this behavior.

Q&A (please complete the following information)

Content & configuration

Example Swagger/OpenAPI definition -- exactly the spec from the Swagger web site, URL above

    requestBody:
      content: 
        multipart/form-data: # Media type
          schema:            # Request payload
            type: object
            properties:      # Request parts
              id:            # Part 1 (string value)
                type: string
                format: uuid
              address:       # Part2 (object)
                type: object
                properties:
                  street:
                    type: string
                  city:
                    type: string
              profileImage:  # Part 3 (an image)
                type: string
                format: binary

Swagger-UI configuration options: (none)

Describe the bug you're encountering

The multipart form is not uploaded as I expect. I click "Try it out" in the UI, put in valid data. The window shows this cURL command:

curl -X 'POST' \
  'http://localhost:5000/api/v1/request_file' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'address={
  "city": "string",
  "street": "string"
}' \
  -F 'id=abc' \
  -F 'profileImage=@my-req-1.txt;type=text/plain'

But an error comes back:

{
  "detail": "'{\\r\\n  \"city\": \"string\",\\r\\n  \"street\": \"string\"\\r\\n}' is not of type 'object' - 'address'",
  "status": 400,
  "title": "Bad Request",
  "type": "about:blank"
}

The server logs this error:

2021-10-18T15:10:06-0400 ERROR connexion.decorators.validation http://localhost:5000/api/v1/request_file validation error: '{\r\n  "city": "string",\r\n  "street": "string"\r\n}' is not of type 'object' - 'address'

To reproduce...

Steps to reproduce the behavior:

  1. Start the server
  2. Go to the endpoint with this content model
  3. Click on Try it out
  4. Browse to a file for upload; leave the suggested string-parameter values
  5. Click execute
  6. See the error in the response window area

Expected behavior

I was hoping the documentation example would work :)

chrisinmtown commented 3 years ago

Hope this is the right issue tracker. Please advise if this is a doc problem (not software problem) in which case prolly I should have created an issue here instead: https://github.com/swagger-api/swagger.io/issues

char0n commented 3 years ago

Hi @chrisinmtown yes it's the right issue tracker.

We have to first determine if the problem is in swagger-ui or in swagger-client which actually executes requests for swagger-ui.

We started to use new library for form data lately, so this might be related with it.

wojtekm462 commented 2 years ago

I encountered similar issue where I reckon reason is performed JSON.stringify on the JSON object and as result the form field is supplied with string and not object. That causes validation failure and 400 as result from connexion. Check my reported issue https://github.com/swagger-api/swagger-ui/issues/7625

R-Rijnbeek commented 8 months ago

I had the same issue. But put extra information in the encoding section:

encoding:  
  profileImage:  
    contentType: image/png, image/jpeg
  address:
    contentType: application/json

Fix the issue