[BUG] PHP client is not sending multipart request unless a file is part of the schema
Bug Report Checklist
- [X] Have you provided a full/minimal spec to reproduce the issue?
- [X] Have you validated the input using an OpenAPI validator?
- [X] What's the version of OpenAPI Generator used?
- [X] Have you search for related issues/PRs?
- [X] What's the actual output vs expected output?
- [ ] [Optional] Bounty to sponsor the fix
Description
I noticed that even if the OpenAPI3 definition says it consumes multipart/form-data the generated client is not sending multipart request. It only sends multipart requests if a property of the type string and format binary is defined and used in the request (definition only is not changing anything). In my point of view the client should send what the server api has defined to be consumed.
openapi-generator version
4.2.3, 4.3.0 and 5.0.0
OpenAPI declaration file content or url
https://gist.github.com/reeperbahnause/07fc91a8c276e7479c270b8353eb45d9#file-openapi-generator-php-client-multipart-json
Command line used for generation
java -jar openapi-generator-cli-4.2.3.jar generate -i apitestminimal.json -g php -o apitestdir
Steps to reproduce
Generate the php client api send a request and check the request on the server side. It wont be a multipart/form-data request.
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/pull/3750
Suggest a fix
If the parameter is not a file, it might still be a multipart-form-data message, but it's not set to true? I think, this is the spot to look at: https://github.com/OpenAPITools/openapi-generator/blob/3588990a4f9f7e7d3e3ae8e2dda59206f57bf676/modules/openapi-generator/src/main/resources/php/api.mustache#L533-L541
I'm running into a similar issue using v7.6.0. The generated code contains $multipart = false;, even though the OpenAPI spec specifies the media type as multipart/form-data.
(Sidenote: The operation does not contain any files or binary data.)
As a result, Guzzle sends requests without a boundary value: Content-Type: multipart/form-data.
According to https://stackoverflow.com/questions/43932900/multipart-form-data-without-a-boundary, this is invalid.
Consequently, the server responds with Multipart form parse error - Invalid boundary in multipart: None.
Manually setting the value of $multipart to true fixes the issue.
I've started looking into this.
@reeperbahnause correctly pointed out the right location in the template (updated for the latest commit on master).
$multipart is only ever set to true if any parameter is a file.
https://github.com/OpenAPITools/openapi-generator/blob/640ef9d9448a4a008af90eca9cc84c8a78ec87ec/modules/openapi-generator/src/main/resources/php/api.mustache#L670-L674
This in turn causes HeaderSelector::selectHeaders() to set the Content-Type header to multipart/form-data if the YAML spec specifies this media type (line 51):
https://github.com/OpenAPITools/openapi-generator/blob/640ef9d9448a4a008af90eca9cc84c8a78ec87ec/modules/openapi-generator/src/main/resources/php/HeaderSelector.mustache#L46-L52
But Guzzle doesn't like that and requires you to omit the Content-Type header (https://github.com/guzzle/guzzle/issues/1885), otherwise the request is sent without the required boundary value.