Allow parser to move on the START_OBJECT token when parsing search source

Currently we require parser to be right before the sources START_OBJECT
but if we are parsing embedded search sources this won't work since we potentially
moved already on to the START_OBJECT. This commit make this optional such that
both ways work.
This commit is contained in:
Simon Willnauer 2015-10-15 22:28:05 +02:00
parent 68d708a75f
commit cd0084915d
2 changed files with 8 additions and 3 deletions

View File

@ -697,9 +697,9 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ
public SearchSourceBuilder fromXContent(XContentParser parser, QueryParseContext context) throws IOException {
SearchSourceBuilder builder = new SearchSourceBuilder();
XContentParser.Token token;
XContentParser.Token token = parser.currentToken();
String currentFieldName = null;
if ((token = parser.nextToken()) != XContentParser.Token.START_OBJECT) {
if (token != XContentParser.Token.START_OBJECT && (token = parser.nextToken()) != XContentParser.Token.START_OBJECT) {
throw new ParsingException(parser.getTokenLocation(), "Expected [" + XContentParser.Token.START_OBJECT + "] but found [" + token + "]",
parser.getTokenLocation());
}

View File

@ -302,7 +302,12 @@ public class SearchSourceBuilderTests extends ESTestCase {
private void assertParseSearchSource(SearchSourceBuilder testBuilder, String builderAsString) throws IOException {
XContentParser parser = XContentFactory.xContent(builderAsString).createParser(builderAsString);
SearchSourceBuilder newBuilder = SearchSourceBuilder.parseSearchSource(parser, createParseContext(parser));
QueryParseContext parseContext = createParseContext(parser);
parseContext.reset(parser);
if (randomBoolean()) {
parser.nextToken(); // sometimes we move it on the START_OBJECT to test the embedded case
}
SearchSourceBuilder newBuilder = SearchSourceBuilder.parseSearchSource(parser, parseContext);
assertNotSame(testBuilder, newBuilder);
assertEquals(testBuilder, newBuilder);
assertEquals(testBuilder.hashCode(), newBuilder.hashCode());