swagger-api / swagger-api/swagger-codegen
PHP support for multipart/form-data POST request
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in the PHP api.mustache template, focusing on how form parameters set $multipart and how the request headers are selected. Reproduce the provided Swagger definition with the PHP generator, then verify that a multipart/form-data request remains multipart when no file parameter is supplied.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100