-
Notifications
You must be signed in to change notification settings - Fork 153
Return empty array on empty collection response #260
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
Conversation
(the added assertions were already passing before the fix)
|
(I have added 2 extra commits I think make the code better, but they're optional, feel free to discard them) |
|
Thank you @guilliamxavier . I'm going to run this tomorrow. I'll be bumping the version to 2.0.0 as it is possible that people have taken a dependency on the old behavior. |
|
Thank you for responding. As concerns a hypothetical dependency on the buggy behavior... given for example $query = http_build_query(['$filter' => $filter, '$orderby' => $orderby]);
$userGrabber = $graph->createCollectionRequest('GET', "/users?$query")
->setReturnType(Model\User::class)
->setPageSize(100);
$users = $userGrabber->getPage();I admit that (although very unlikely) there is indeed a slight possibility that someone might have written some "fragile" code like // this code assumes that $users cannot be empty:
if (is_array($users)) {
/* code that must be run if the real collection was not empty (use $users) */
} else {
/* code that must be run if the real collection was empty (don't use $users) */
}and would need to change it (actually, to fix it), but there's a much higher probability that people have written reasonable code like // this code trusts that $users is an array:
if (count($users) > 0) {
/* code that must be run if the real collection was not empty (use $users) */
} else {
/* code that must be run if the real collection was empty (don't use $users) */
}then if got bitten by the bug (e.g. "expected array, got object"), adapted it like this: // this code checks to be sure that $users is an array:
if (is_array($users) && count($users) > 0) {
/* code that WILL be run if the real collection was not empty (use $users) */
} else {
/* code that WILL be run if the real collection was empty (don't use $users) */
}or (that's me) like that: // Workaround for bug https://github.com/microsoftgraph/msgraph-sdk-php/issues/46
if (!is_array($users)) {
$users = [];
}
// this code can now be confident that $users is an array:
if (count($users) > 0) {
/* code that WILL be run if the real collection was not empty (use $users) */
} else {
/* code that WILL be run if the real collection was empty (don't use $users) */
}For me it would be a bit sad if people who are using version |
MIchaelMainer
left a comment
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.
Thank you @guilliamxavier for your time on this. And FYI, I'm going to be updating PHP to 7.4 as well as updating the dependencies.
|
I see that this was released in 1.13.0 (with a bump to PHP 7.1) 🎉 Thanks |
Fixes #46
Closes #105