Skip to content
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

Check for negative "from" values in search request body #54953

Merged
merged 8 commits into from
May 28, 2020

Conversation

cbuescher
Copy link
Member

Today we already disallow negative values for the "from" parameter in the search
API when it is set as a request parameter and setting it on the
SearchSourceBuilder, but it is still parsed without complaint from a search
body, leading to different, confusing exceptions later on. This PR changes
this behavior to be the same regardless of setting the value directly, as url
parameter or in the search body. Values of -1 are still allowed as the current
default, meaning an unset value, in order not to break existing searches and
templates using this.

Closes #54897

Today we already disallow negative values for the "from" parameter in the search
API when it is set as a request parameter and setting it on the
SearchSourceBuilder, but it is still parsed without complaint from a search
body, leading to differing exceptions later. This PR changes this behavior to be
the same regardless of setting the value directly, as url parameter or in the
search body. Values of -1 are still allowed as the current default, meaning an
unset value, in order not to break existing searches and templates using this.

Closes elastic#54897
@cbuescher cbuescher added >bug :Search/Search Search-related issues that do not fall into other categories v8.0.0 v7.8.0 labels Apr 8, 2020
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (:Search/Search)

@cbuescher cbuescher requested a review from javanna April 8, 2020 13:07
@cbuescher
Copy link
Member Author

@elasticmachine update branch

@cbuescher
Copy link
Member Author

@elasticmachine run elasticsearch-ci/2

Copy link
Contributor

@jimczi jimczi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left one comment.

search:
rest_total_hits_as_int: true
index: test_1
# value of -1 should be accepted as unset value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we try to address this ? For instance we could set the default value directly rather than delaying this to the creation of the search context ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be open to forbid users setting from to "-1" entirely on 8.0 but since we currently accept it in the request body (and treat it as 0 later) I think we should allow it at least on 7.x to not break existing queries there. Every other negative value should alread throw a different exception, so thats fine I think. We can log a warning on 7.x when using "-1" to prepare for the change in 8.0 if thats okay?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest spec states that the default value for from is 0 and size is 10 so I don't think users rely on us accepting -1 as a valid value. As you said we never output the value if it's unset so we imo we should forbid in 7.x.

*/
public SearchSourceBuilder from(int from) {
if (from < 0) {
throw new IllegalArgumentException("[from] parameter cannot be negative");
// accept -1 as the default unset value without error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set the default early so that we can refuse any negative values ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for SearchSourceBuilder I would prefer to keep the notion of an "unset" value and interpret that in any way necessary later. I think this class should represent the user request body, e.g. in innerToXContent we only output values that are explicitely set by the user. Maybe @javanna has some preferences there as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd agree if "unset" had different semantics depending on the context but we always set it to the default value.
We could forbid setting negative value but keep the initial value to -1 ?

@cbuescher
Copy link
Member Author

@jimczi I left some answers to the questions you raised in your review. I'm leaning towards forbidding users setting from to "-1" entirely on 8.0, I think in 7.x we cannot do that since it would be breaking but log a warning instead. I currently would prefer to keep the distinction between an "unset" value for the reasons stated above but am open to discussion, maybe also involving input from @javanna

@rjernst rjernst added the Team:Search Meta label for search team label May 4, 2020
@cbuescher
Copy link
Member Author

@jimczi @javanna just found this PR sitting around, let me know if there anything I can do to move it forward.

@cbuescher cbuescher removed the v7.9.0 label May 27, 2020
@cbuescher
Copy link
Member Author

@jimczi thanks for the review, I updated the PR to reject from values of -1 as url and search body parameter, however I still think this should be considered a breaking change then so I added a note to the migration docs and would only merge to master at this point.

@cbuescher
Copy link
Member Author

@elasticmachine update branch

Copy link
Contributor

@jimczi jimczi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cbuescher cbuescher merged commit 3d4f9fe into elastic:master May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Specifying a negative from value in a Search returns different hard-to-interpret error messages
7 participants