-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Fix error when giving an array as parameter to an endpoint body request #1037
Fix error when giving an array as parameter to an endpoint body request #1037
Conversation
if($headers['Content-Type'] !== 'application/json') { | ||
$httpBody = $_tempBody; | ||
} else { | ||
$httpBody = $this->formatBody($_tempBody); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thomasphansen thanks for the PR. What about using ObjectSerializer::sanitizeForSerialization
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, makes sense! :D
(BTW, I just found this very nice article on how to contribute to openapi-generator: https://medium.com/ringcentral-developers/openapi-generator-for-go-contribution-quickstart-8cc72bf37b53 |
So: Travis is failing because the ObjectSerializer::sanitizeForSerialization() method deals poorly with \stdClass objects. I can see some possible solutions here:
What do you think @wing328 ? :) |
Pushed a commit implementing 1.b option... :) |
18ad57d
to
87ffe6f
Compare
Well, I'm not being able to get travis-ci finishing the tests: I'm getting the message " |
@thomasphansen I've restarted the job and all tests pass. cc @jebentier @dkarlovi @mandrean @jfastnacht @ackintosh @ybelenko |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR!
&& !in_array($value, $openAPIType::getAllowableEnumValues(), true)) { | ||
$imploded = implode("', '", $openAPIType::getAllowableEnumValues()); | ||
throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'"); | ||
if($data instanceof ModelInterface) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: please put a space after if
.
// \stdClass has no __toString(), so we should encode it manually | ||
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') { | ||
$httpBody = \GuzzleHttp\json_encode($httpBody); | ||
if($headers['Content-Type'] !== 'application/json') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: please put a space after if
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about invert the if
condition like below:
if ($headers['Content-Type'] !== 'application/json') {
$httpBody = $_tempBody;
} else {
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
}
↓↓↓
if ($headers['Content-Type'] === 'application/json') {
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
} else {
$httpBody = $_tempBody;
}
The ===
in if
statement may be Intuitive and readable. 💡
I've restarted the travis-ci build as it was resulted in the error.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update. Looks good to me. ✨
OpenAPITools#1037) * PR: Fix error when giving an array as parameter to an endpoint body request * PR OpenAPITools#1037 - Fix issue with array as parameter to operation * update samples * PHP - ObjectSerializer::sanitizeForSerialization(): manage \stdClass objects properly * update samples * bug fix: missing "use" clause * update samples * Changes after @ackintosh review * update samples
@thomasphansen thanks again for the PR, which is included in the v3.3.0 minor release: https://twitter.com/oas_generator/status/1046941449609068544 |
OpenAPITools#1037) * PR: Fix error when giving an array as parameter to an endpoint body request * PR OpenAPITools#1037 - Fix issue with array as parameter to operation * update samples * PHP - ObjectSerializer::sanitizeForSerialization(): manage \stdClass objects properly * update samples * bug fix: missing "use" clause * update samples * Changes after @ackintosh review * update samples
…equest
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
and./bin/security/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
.master
,3.3.x
,4.0.x
. Default:master
.Description of the PR
This PR targets Issue #1036 : as of today, it is not possible to provide an array of model objects as parameter for an operation, since it will not be properly json-encoded. This PR fixes this, by adding a recursive method that is able to properly json-encode arrays of model objects.