Description
Description
trying to generate a PHP client library, using multipart/form-data parameters for POST request.
i see in the PHP api.mustache that $multipart is initially set to false and only if a file parameter has been received it it's value is changed to true:
// form params
if (${{paramName}} !== null) {
{{#isFile}}
$multipart = true;
$formParams['{{baseName}}'] = \GuzzleHttp\Psr7\try_fopen(ObjectSerializer::toFormValue(${{paramName}}), 'rb');
{{/isFile}}
{{^isFile}}
$formParams['{{baseName}}'] = ObjectSerializer::toFormValue(${{paramName}});
{{/isFile}}
}
Later in the same file we see that only if $multipart is set to true a multipart request is created:
if ($multipart) {
$headers = $this->headerSelector->selectHeadersForMultipart(
[{{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/produces}}]
);
} else {
$headers = $this->headerSelector->selectHeaders(
[{{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/produces}}],
[{{#consumes}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}]
);
}
this is my swagger json file:
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "",
"contact": {
"name": ""
},
"license": {
"name": "",
"url": "http://licenseUrl"
}
},
"host": "localhost:9000",
"basePath": "/",
"tags": [
{
"name": "Mail Controller"
},
{
"name": "Health Check Controller"
}
],
"paths": {
"/mail-service/submit": {
"post": {
"tags": [
"Mail Controller"
],
"summary": "Send Mail",
"description": "",
"operationId": "submit",
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "from",
"in": "formData",
"required": true,
"type": "string"
},
{
"name": "Content-Type",
"value" : "multipart/form-data",
"in": "header",
"type": "string"
},
{
"name": "subject",
"in": "formData",
"required": true,
"type": "string"
},
{
"name": "body",
"in": "formData",
"required": true,
"type": "string"
},
{
"name": "to",
"in": "formData",
"required": true,
"type": "string"
},
{
"name": "cc",
"in": "formData",
"required": false,
"type": "string"
},
{
"name": "bcc",
"in": "formData",
"required": false,
"type": "string"
},
{
"name": "replyTo",
"in": "formData",
"required": false,
"type": "string"
},
{
"name": "file",
"in": "formData",
"required": false,
"type": "file"
}
],
"responses": {
"200": {
"description": "success",
"schema": {
"$ref": "#/definitions/EmailJsonSuccessResponse"
}
},
"400": {
"description": "validation error",
"schema": {
"$ref": "#/definitions/JsonErrorResponse"
}
},
"500": {
"description": "server error",
"schema": {
"$ref": "#/definitions/JsonErrorResponse"
}
}
}
}
}
},
"definitions": {
"JsonErrorResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
},
"emailId": {
"type": "string"
}
}
},
"EmailJsonSuccessResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
},
"emailId": {
"type": "string"
}
}
},
"JsonResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
},
"emailId": {
"type": "string"
}
}
}
}
}
therefore, when sending a POST request with form parameters without a file parameter (not mandatory parameter), the generated client library does not send the request as a multipart/form-data parameters request.
Swagger-codegen version
version 2.3.1
Swagger declaration file content or url
Command line used for generation
java -DdebugOperations -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate
-i mail.json
-l php
-o ~/workspace/swagger-codegen/mail-service-php
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement
what might fix it is to set the multipart to true not only if a file was received as a parameter, but to check the content-type header parameter instead.