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

Fix the Issue: multipart/form-data with Nested Objects

Open GFoniX opened this issue 2 years ago • 2 comments

Content & configuration

Swagger/OpenAPI definition:

openapi: 3.0.0
paths:
 /api/test:
   post:
     operationId: TestController_test
     parameters: []
     requestBody:
       required: true
       content:
         multipart/form-data:
           schema:
             $ref: '#/components/schemas/TestDto'
         application/json:
           schema:
             $ref: '#/components/schemas/TestDto'
         application/x-www-form-urlencoded:
           schema:
             $ref: '#/components/schemas/TestDto'
     responses:
       '201':
         description: ''
         content:
           application/json:
             schema:
               $ref: '#/components/schemas/TestDto'
info:
 title: TESTAPI
 description: Routes description for test API
 version: '1.0'
 contact: {}
tags: []
servers: []
components:
 securitySchemes:
   bearer:
     scheme: bearer
     bearerFormat: JWT
     type: http
 schemas:
   BarDto:
     type: object
     properties:
       bar:
         type: string
     required:
       - bar
   FooDto:
     type: object
     properties:
       foo:
         type: string
       bar:
         $ref: '#/components/schemas/BarDto'
     required:
       - foo
       - bar
   TestDto:
     type: object
     properties:
       test:
         type: string
       foo:
         $ref: '#/components/schemas/FooDto'
     required:
       - test
       - foo

Is your feature request related to a problem?

When I attempt to send a multipart/form-request, it sends the nested object as string (JSON) instead of form-data. For example, when swagger describe the request using cURL, I encounter the following issue.

curl -X 'POST' \
  'http://localhost:3000/api/test' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'test=test' \
  -F 'foo={
  "foo": "string",
  "bar": {
    "bar": "string"
  }
}'

Describe the solution you'd like

I'd like to format the nested object in multipart/form-data as shown in this cURL request.

curl -X 'POST' \
  'http://localhost:3000/api/test' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'test=test' \
  -F 'foo[foo]=string' \
  -F 'foo[bar][bar]=string'

Describe alternatives you've considered

If the request is multipart/form-data and the data is in object format, we can format the nested object as follows.

Additional context

I believe there might be an issue. When I create an API and attempt to use multipart/form-data, the typical way to send data with tools like Postman or via Fetch/Axios involves a structure like 'foo[foo]...'. However, Swagger sends it in string (JSON) format, which doesn't work correctly in my application. I think it should be sent in the former format.

GFoniX avatar Oct 05 '23 13:10 GFoniX

I can work on this issue.

marcelorafaelfeil avatar Oct 06 '23 09:10 marcelorafaelfeil

Great, I've already started exploring how to contribute on my end. I've tried to understand the project and managed to make some modifications to the curl request (though it didn't change the request structure to the API documentation). However, I wasn't sure about how you'd like to approach changing the design (for instance, making Swagger accept a list of values rather than a single string, similar to form datas but in a nested manner). So, I decided to wait for now to get a better understanding of the expectations.

GFoniX avatar Oct 06 '23 09:10 GFoniX