Skip to content

Commit

Permalink
Do not throw errors on unknown types in SearchAfterBuilder
Browse files Browse the repository at this point in the history
The support for BigInteger and BigDecimal was added for XContent in
elastic#32888. However the SearchAfterBuilder
xcontent parser doesn't expect them to be present so it throws an AssertionError.
This change fixes this discrepancy by changing the AssertionError into an
IllegalArgumentException that will not cause the node to die when thrown.

Closes elastic#48074
  • Loading branch information
jimczi committed Oct 16, 2019
1 parent f9511f4 commit 3312df0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public static SearchAfterBuilder fromXContent(XContentParser parser) throws IOEx
break;

default:
throw new AssertionError("Unknown number type []" + parser.numberType());
throw new IllegalArgumentException("Unknown number type: [" + parser.numberType() + "]");
}
} else if (token == XContentParser.Token.VALUE_STRING) {
values.add(parser.text());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
Expand All @@ -38,10 +39,13 @@
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;

import static org.elasticsearch.search.searchafter.SearchAfterBuilder.extractSortType;
import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public class SearchAfterBuilderTests extends ESTestCase {
Expand Down Expand Up @@ -187,6 +191,36 @@ public void testFromXContent() throws Exception {
}
}

public void testFromXContentIllegalType() throws Exception {
XContentBuilder xContent = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
xContent.startObject()
.startArray("search_after")
.value(new BigInteger("9223372036854776000"))
.endArray()
.endObject();
try (XContentParser parser = createParser(shuffleXContent(xContent))) {
parser.nextToken();
parser.nextToken();
parser.nextToken();
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> SearchAfterBuilder.fromXContent(parser));
assertThat(exc.getMessage(), containsString("BIG_INTEGER"));
}

xContent = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
xContent.startObject()
.startArray("search_after")
.value(new BigDecimal("9223372036854776000.35"))
.endArray()
.endObject();
try (XContentParser parser = createParser(shuffleXContent(xContent))) {
parser.nextToken();
parser.nextToken();
parser.nextToken();
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> SearchAfterBuilder.fromXContent(parser));
assertThat(exc.getMessage(), containsString("BIG_DECIMAL"));
}
}

public void testWithNullArray() throws Exception {
SearchAfterBuilder builder = new SearchAfterBuilder();
try {
Expand Down

0 comments on commit 3312df0

Please sign in to comment.