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.
This commit is contained in:
parent
ad3658a8a3
commit
0b8da31ccf
|
@ -222,7 +222,7 @@ public class SearchResponse extends ActionResponse implements StatusToXContentOb
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SearchResponse fromXContent(XContentParser parser) throws IOException {
|
public static SearchResponse fromXContent(XContentParser parser) throws IOException {
|
||||||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
|
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
|
||||||
XContentParser.Token token;
|
XContentParser.Token token;
|
||||||
String currentFieldName = null;
|
String currentFieldName = null;
|
||||||
SearchHits hits = null;
|
SearchHits hits = null;
|
||||||
|
|
|
@ -49,7 +49,6 @@ import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static java.util.Collections.singletonMap;
|
import static java.util.Collections.singletonMap;
|
||||||
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
|
|
||||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
|
||||||
|
|
||||||
public class SearchResponseTests extends ESTestCase {
|
public class SearchResponseTests extends ESTestCase {
|
||||||
|
@ -104,7 +103,6 @@ public class SearchResponseTests extends ESTestCase {
|
||||||
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
|
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
|
||||||
BytesReference originalBytes = toShuffledXContent(response, xcontentType, params, humanReadable);
|
BytesReference originalBytes = toShuffledXContent(response, xcontentType, params, humanReadable);
|
||||||
try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) {
|
try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) {
|
||||||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
|
|
||||||
SearchResponse parsed = SearchResponse.fromXContent(parser);
|
SearchResponse parsed = SearchResponse.fromXContent(parser);
|
||||||
assertToXContentEquivalent(originalBytes, XContentHelper.toXContent(parsed, xcontentType, params, humanReadable), xcontentType);
|
assertToXContentEquivalent(originalBytes, XContentHelper.toXContent(parsed, xcontentType, params, humanReadable), xcontentType);
|
||||||
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
|
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
|
||||||
|
@ -129,7 +127,6 @@ public class SearchResponseTests extends ESTestCase {
|
||||||
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
|
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
|
||||||
BytesReference originalBytes = toShuffledXContent(response, xcontentType, params, randomBoolean());
|
BytesReference originalBytes = toShuffledXContent(response, xcontentType, params, randomBoolean());
|
||||||
try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) {
|
try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) {
|
||||||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
|
|
||||||
SearchResponse parsed = SearchResponse.fromXContent(parser);
|
SearchResponse parsed = SearchResponse.fromXContent(parser);
|
||||||
for (int i = 0; i < parsed.getShardFailures().length; i++) {
|
for (int i = 0; i < parsed.getShardFailures().length; i++) {
|
||||||
ShardSearchFailure parsedFailure = parsed.getShardFailures()[i];
|
ShardSearchFailure parsedFailure = parsed.getShardFailures()[i];
|
||||||
|
|
Loading…
Reference in New Issue