[PHP] array of objects as parameter not supported
Created by: thomasphansen
Description
PHP generator is not able to handle an array of objects as parameter to a path.
This is the code that handles the $httpBody, inside the {{operationId}}Request method, in /modules/openapi-generator/src/main/resources/php/api.mustache:
// for model (json/xml)
if (isset($_tempBody)) {
// $_tempBody is the method argument, if present
$httpBody = $_tempBody;
// \stdClass has no __toString(), so we should encode it manually
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
$httpBody = \GuzzleHttp\json_encode($httpBody);
}
} elseif (count($formParams) > 0) {
(...)
As you can see, this code will json_encode strClass objects, relay on the "toString()" method for normal object and simply add the parameter as the body otherwise. But if the parameter is an array of model objects, it will be given to the GuzzleHttp\Psr7\Request constructor, which will refuse it as invalid (the http body must be a string, of course).
Running json_encode on the array is useless, since all the properties inside the model object are protected.
I suggest using a recursive method to properly convert $httpBody to json. I'm creating a PR implementing this fix.
openapi-generator version
master (3.3.0)
OpenAPI declaration file content or url
This is a snippet from lines 3000-3013 of the yaml definition I'm working with. Please refer to the Gist link below, for checking the entire file.
"parameters": [
{
"in": "body",
"name": "body",
"description": "JSON representing a list of new object to be created. Should not have ID and version set.",
"required": false,
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Customer"
}
}
}
],
Command line used for generation
docker run \
--user $(id -u):$(id -g) \
--rm -v ${PWD}:/local openapitools/openapi-generator-cli:v3.0.0 generate \
-i https://tripletex.no/v2/swagger.json \
-g php \
-o /local \
-c /local/config.json
Steps to reproduce
try to use the method postList() from the generated CustomerApi
Related issues/PRs
Suggest a fix/enhancement
See PR #1037