-
Notifications
You must be signed in to change notification settings - Fork 212
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
Change normalizeIncludeTotals() in GenericResource to have sane defaults #347
Change normalizeIncludeTotals() in GenericResource to have sane defaults #347
Conversation
@kler - I appreciate the contribution here but I think that this is all working as expected. The first https://github.com/auth0/auth0-PHP/blob/master/src/API/Helpers/RequestBuilder.php#L199 Do you have a use case where this is failing or was this just a review of the code that was there? If we've got a bug, I'm happy to take a closer look. Thank you! |
I got a case where it's failing, it's the one I posted above in the PR. On submitting
In fact I just realised that the second I'll walk through the code.
Let's put it like this: Since the API expects the |
There are two possible fixes
|
@kler - Can you send me the complete code you're using to cause this error? I see what you're saying here but the final processing of the URL converts I just wrote a test to try this out and I get results back just fine: $api = new Management('API_TOKEN', 'auth0-php.auth0.com');
$response = $api->roles->getAll( [ 'include_filters' => null ] );
echo '<pre>' . print_r( $response, TRUE ) . '</pre>'; die(); I'm more worried about the URL param convertor not doing it's job (which it looks, to me, like it is). |
Here's a simple curl which shows that an empty Will WORK since include_totals is omitted
Will FAIL since include_totals is interpreted as NULL
The fix needs to be one of:
1+2 are both "lazy" and "silent" - i.e. users of the library may use it wrong and don't understand it. Personally I think I would have resorted to 2, which is also exactly this PR :) |
@kler - You're correct, a direct call to that API with Do you have a code sample using the SDK that's failing? |
|
@kler - Thank you for that, I can reproduce the issue now. The example I posted above uses a method that does not use So, it looks like the problem is not that The question is ... how best to guide people here? I don't think converting everything to a boolean is the right way to do this: ❯ php -r 'echo (bool) "FALSE" ? "I am TRUE" : "I am FALSE";'
I am TRUE ... and I want to keep the API default if nothing is passed in. I think the best way to handle this is to check whether the key exists and check if it's boolean, then Does that sound like a good approach? Thank you for sticking with this! |
Use filter_var() with FILTER_VALIDATE_BOOLEAN instead. That will provide sane boolean conversions. (On cell, so didn't bother to format commen or provide links). |
@kler - TIL, I didn't know As long as |
Both If anything, it should be checked that the user doesn't provide NULL as a param, because that's a complete misuse of the lib and only indicates the user doesn't understand it. But I won't make that change though - but I strongly believe the param default should be something that the API accepts - or do you have any good argument why the default should be something that the API doesn't accept? |
Quick change - I've changed the default to NULL, and unsetting the key if it's NULL in change |
// Make sure we have a boolean. | ||
$params['include_totals'] = filter_var( $params['include_totals'], FILTER_VALIDATE_BOOLEAN ); | ||
// Unset if NULL, or else make sure we have a boolean. | ||
if (is_null($params['include_totals'])) { |
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.
You can remove this check. I'll submit a PR to exclude any parameters set to null
in the parameter builder. That way we can avoid these checks in other "normalize params" methods we might add in the future. If you add FILTER_NULL_ON_FAILURE
as a third param, then we can handle all irrational arguments the same way.
f6b2720
to
e72afb4
Compare
Like this then @joshcanhelp ? |
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.
This will keep null
as the default (which we'll handle in the param builder) and convert anything that's not filterable to null
as well.
Appreciate you sticking with this ... apologies if my feedback has not been clear.
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 once again for the PR and working through it with me 🎉
Closing and reopening to trigger CI |
…lts using filter_var()
…lts using filter_var(), unset key if it's NULL.
…lts using filter_var(), unset key if it's NULL.
48f0308
to
37d0adc
Compare
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.
Sorry for all the notifications 😄
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Changes
include_totals
to befalse
instead ofnull
The endpoint
GET /api/v2/users/{id}/roles
accepts a param calledinclude_totals
, which is expected to be a boolean. However, the default inGenericResource::normalizeIncludeTotals()
isnull
.I argue that false should be the default, or that the default should be removed all along (
normalizeIncludeTotals()
unconditionally adds the param, which I believe is wrong, however in this PR I only changed the default though).On submitting
include_totals=null
this is what the API will respond:On top of this the function
normalizeIncludeTotals()
had an unnecessary check:The key
include_totals
in$params
will always be set, because either the user provides it, or if not it's added in the statement above. I removed the check whether it's set or not.Checklist
[x] I have read the Auth0 general contribution guidelines.
[x] I have read the Auth0 Code of Conduct.
[ ] All existing and new tests complete without errors.
=> No, since the testing tenant needs to be precisely tailored for the test.
Getting for instance:
And test doesn't contain necessary backoff algorithm, resulting in
too_many_requests
[x] The correct base branch is being used.