Skip to content

Commit 0b8da31

Browse files
authored
SearchResponse#fromXContent to not require START_OBJECT as current token (#24794)
The method should rather advance one token and only then require a START_OBJECT as the current token. This allows to parse given a parser that's at the beginning of the response, where the initial/current token is null.
1 parent ad3658a commit 0b8da31

File tree

2 files changed

+1
-4
lines changed

2 files changed

+1
-4
lines changed

core/src/main/java/org/elasticsearch/action/search/SearchResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
222222
}
223223

224224
public static SearchResponse fromXContent(XContentParser parser) throws IOException {
225-
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
225+
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
226226
XContentParser.Token token;
227227
String currentFieldName = null;
228228
SearchHits hits = null;

core/src/test/java/org/elasticsearch/action/search/SearchResponseTests.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import java.util.List;
5050

5151
import static java.util.Collections.singletonMap;
52-
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
5352
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
5453

5554
public class SearchResponseTests extends ESTestCase {
@@ -104,7 +103,6 @@ public void testFromXContent() throws IOException {
104103
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
105104
BytesReference originalBytes = toShuffledXContent(response, xcontentType, params, humanReadable);
106105
try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) {
107-
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
108106
SearchResponse parsed = SearchResponse.fromXContent(parser);
109107
assertToXContentEquivalent(originalBytes, XContentHelper.toXContent(parsed, xcontentType, params, humanReadable), xcontentType);
110108
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
@@ -129,7 +127,6 @@ public void testFromXContentWithFailures() throws IOException {
129127
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
130128
BytesReference originalBytes = toShuffledXContent(response, xcontentType, params, randomBoolean());
131129
try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) {
132-
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
133130
SearchResponse parsed = SearchResponse.fromXContent(parser);
134131
for (int i = 0; i < parsed.getShardFailures().length; i++) {
135132
ShardSearchFailure parsedFailure = parsed.getShardFailures()[i];

0 commit comments

Comments
 (0)