Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated CURL seems wrong for mime/multipart uploads that have JSON parts - missing 'type=' option, wrong quotes. #4826

Closed
shockey opened this issue Aug 22, 2018 · 4 comments

Comments

@shockey
Copy link
Contributor

shockey commented Aug 22, 2018

From @dvelitchkov on August 21, 2018 0:15

Running swagger editor from docker container, no customizations.

Q&A (please complete the following information)

  • OS: macOS
  • Browser: chrome
  • Version: 68.0.3440.106
  • Method of installation: docker
  • Swagger-Editor version: latest
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Example Swagger/OpenAPI definition:

openapi: '3.0.0'
info:
  title: "Foo"
  version: 1.0.0
  
servers:
  - url: https://foo/v1/api
    
paths:
  /upload/:
    post:
      tags:
      - "upload"
      operationId: "upload"

      requestBody:
        required: true
        content: 
          multipart/form-data: # Media type
            schema:
              type: object
              properties: # Request parts
                file:
                  type: string
                  format: binary
                options:
                  type: object
                  properties:
                    some_array:
                      type: array
                      items:
                        type: string
                    max_bar:
                      type: integer
                      default: 300
                    
      responses:
        '200':
          description: ok
          content:
            application/json:
              schema:
                type: object
                properties: 
                  foo:
                    type: string

Describe the bug you're encountering

The generated CURL looks like this:

curl -X POST "https://foo/v1/api/upload/" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "options={ "some_array": [ "string" ], "max_bar": 0 }" -F "myfile=@bla;type=application/zip"

Expected behavior

I think there are two issues here:

  1. The "type=" option tha specifies the content-type for the "options" part is missing - I feel it should be "application/json", since that is the default for "objects". The "type=" option is actually present for the "myfile" part, which is great.

  2. The quotes for the "options" part need to be single quotes, e.g. -F 'options={ "some_array": [ "string" ], "max_bar": 0 }', not -F "options={ "some_array": [ "string" ], "max_bar": 0 }", as having double-quotes everywhere will just cause the shell to strip them.

Additional context or thoughts

The full correct curl should be:

curl -X POST "https://foo/v1/api/upload/" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F 'options={ "some_array": [ "string" ], "max_bar": 0 };type=application/json' -F "myfile=@bla;type=application/zip"

Copied from original issue: swagger-api/swagger-editor#1871

@LouCastaldo
Copy link

Same issue here with both "multipart/mixed" and "multipart/form-data". Both generate incorrect cURL command. Any progress on correcting this ??

@char0n
Copy link
Member

char0n commented Jul 31, 2023

Hi everybody,

Related to second point from the Expected Behavior looks like that has already been fixed. Here is the POC generated from the definition of this issue description.

  1. The quotes for the "options" part need to be single quotes, e.g. -F 'options={ "some_array": [ "string" ], "max_bar": 0 }', not -F "options={ "some_array": [ "string" ], "max_bar": 0 }", as having double-quotes everywhere will just cause the shell to strip them.
curl -X 'POST' \
  'https://foo/v1/api/upload/' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'file=@file.pdf;type=application/pdf' \
  -F 'options={
  "some_array": [
    "string"
  ],
  "max_bar": 300
}'

Provided test case in #9096

@char0n
Copy link
Member

char0n commented Aug 1, 2023

Hi everybody,

Related to first point - missing mime type for individual boundaries:

The "type=" option tha specifies the content-type for the "options" part is missing - I feel it should be "application/json", since that is the default for "objects". The "type=" option is actually present for the "myfile" part, which is great.

This issue needs to be addressed both in SwaggerUI (here) and in SwaggerClient. OpenAPI 3.x.y specification allows to fully influence this by using encoding field of Media Type Object.


For SwaggerClient, PR has been issued to address this and respect the encoding field: swagger-api/swagger-js#3078.

Description

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.


For SwaggerUI this issue has been addressed in #9105

CURL command correctly generated by assigning additional type=:

image

char0n added a commit to swagger-api/swagger-js that referenced this issue Aug 1, 2023
This change affects building requests from OpenAPI 3.x.y definitions.

Refs swagger-api/swagger-ui#5356
Refs swagger-api/swagger-ui#4826
Refs #2954
char0n added a commit that referenced this issue Aug 1, 2023
This change fixes both:

1. making multipart/form-data requests with content-type
   header for every individual boundary
2. generating correct CURL command for multipart/form-data
   request, allowing specifying content-type header for every
   individual boundary

Refs #4826
Refs #5356
char0n added a commit that referenced this issue Aug 1, 2023
This change fixes both:

1. making multipart/form-data requests with content-type
   header for every individual boundary
2. generating correct CURL command for multipart/form-data
   request, allowing specifying content-type header for every
   individual boundary

Refs #4826
Refs #5356
@char0n
Copy link
Member

char0n commented Aug 1, 2023

Addressed by #9105

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants