I'm using Swagger 2.0 and I have a problem to send multiple post parameters. I have a swagger error Operation cannot have a body parameter and a formData parameter and I don't know how to fix it. In my definition I have a body parameter and this parameter need a JSON format but a side I have other parameter like files to upload and filename.
How can I do to send body and formData parameters both ? Or I have to change my web services ?
Here is the web service definition :
/updateDatas:
post:
summary: Upadate datas
description: |
Update datas
consumes:
- multipart/form-data
produces:
- application/json
parameters:
- name: firstFileName
in: formData
description: First file name.
required: true
type: string
- name: secondFileName
in: formData
description: Second file name.
required: true
type: string
- name: datas
in: body
description: Json object informations.
required: true
schema:
$ref: '#/definitions/Datas'
- name: firstFile
in: formData
description: First file .jpg
required: true
type: file
- name: clientFile
in: formData
description: Second file .jpg
required: true
type: file
tags:
- Application
responses:
'200':
description: Uploaded
schema:
$ref: '#/definitions/Upload'
'401':
description: Unauthorized Bad Token
Your spec is invalid, and the error message is right.
@webron can you help with this?
@Johngtrs - what you're trying to do is not fully supported by the spec right now. You can combine multiple formData parameters to upload files and other data, but in that case you cannot specify a model as a data type for the parameter (this is a limitation of the spec). The only real way you can do it right now is describe it as a string and not structure the content, which is a miss.
Body parameters are intended to be the case of a single payload to the request, which is not the case you have at hand.
@webron is there any way, where if the api support both json/multipart data to give it in the spec. The consume field is an array but the parameters are object.
@webron any plans to support the usecase mentioned by @Johngtrs in upcoming versions pls.
So it's not supported in 2.0. 3.0 added support for the use case, but it's not finalized. When we add support to it, you'll be able to do that.
Thank you so much for a super fast response @webron. For now I am going with array of array of strings, hoping that works.
@webron Could you please provide any references which we can follow to better understand the issue? My naive swagger config with formData and body parameters rendered in the Swagger-UI just fine, but it failed to send body (I haven't tried how Codegen clients handle it, and I am not sure how would my API server handle it as well; but since you mentioned that it is something that spec does not support, I would like to learn more about it).
Read the spec here - https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject - specifically the description of Form parameters.
Since form parameters are sent in the payload, they cannot be declared together with a body parameter for the same operation.
OK, I realized that the real issue I have is not the co-existance of body and formData, which obviously doesn't make sense, but the need of json format for formData parameters with string type. Does it make sense?
There is a nice JSON editor in Swagger UI, but it seems to work only with body parameters, and it is the true reason I went looking at in: body instead of considering something like
in: formData
type: string
format: json
schema: ...
It's got nothing to do with the UI. If you read the spec, you'll see that non-body parameters can only be primitives or array of primitives. You can't represent a JSON with formData parameters. This changes in 3.0.
Thank you, I am studying 3.0 now.