Skip to content

[BUG] PHP client is not sending multipart request unless a file is part of the schema #5536

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

Open
5 of 6 tasks
reeperbahnause opened this issue Mar 6, 2020 · 2 comments
Open
5 of 6 tasks

Comments

@reeperbahnause
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • 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

#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:

if (${{paramName}} !== null) {
{{#isFile}}
$multipart = true;
$formParams['{{baseName}}'] = \GuzzleHttp\Psr7\try_fopen(ObjectSerializer::toFormValue(${{paramName}}), 'rb');
{{/isFile}}
{{^isFile}}
$formParams['{{baseName}}'] = ObjectSerializer::toFormValue(${{paramName}});
{{/isFile}}
}

@simonhammes
Copy link
Contributor

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.

@simonhammes
Copy link
Contributor

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.

{{#formParams}}
// form params
if (${{paramName}} !== null) {
{{#isFile}}
$multipart = true;

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):

if (!$isMultipart) {
if($contentType === '') {
$contentType = 'application/json';
}
$headers['Content-Type'] = $contentType;
}

But Guzzle doesn't like that and requires you to omit the Content-Type header (guzzle/guzzle#1885), otherwise the request is sent without the required boundary value.

wing328 pushed a commit that referenced this issue Dec 15, 2024
…5536) (#18991)

* [php] Set $multipart to true if content-type is multipart/form-data (#5536)

Related: #5536

* [php] Use isMultipart variable

---------

Co-authored-by: Simon Hammes <[email protected]>
timon-sbr pushed a commit to timon-sbr/openapi-generator that referenced this issue Mar 13, 2025
…penAPITools#5536) (OpenAPITools#18991)

* [php] Set $multipart to true if content-type is multipart/form-data (OpenAPITools#5536)

Related: OpenAPITools#5536

* [php] Use isMultipart variable

---------

Co-authored-by: Simon Hammes <[email protected]>
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