Skip to content

Commit eca4f24

Browse files
authored
[Test] Adding test for parsing SearchShardFailure leniently (#25144)
This change extends the tests and parsing of SearchShardFailure to make sure we can skip fields the parser doesn't know for forward compatibility reasons.
1 parent 34c3d1d commit eca4f24

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
import java.io.IOException;
3939

4040
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
41-
import static org.elasticsearch.common.xcontent.XContentParserUtils.throwUnknownField;
42-
import static org.elasticsearch.common.xcontent.XContentParserUtils.throwUnknownToken;
4341

4442
/**
4543
* Represents a failure to search on a specific shard.
@@ -200,16 +198,16 @@ public static ShardSearchFailure fromXContent(XContentParser parser) throws IOEx
200198
} else if (NODE_FIELD.equals(currentFieldName)) {
201199
nodeId = parser.text();
202200
} else {
203-
throwUnknownField(currentFieldName, parser.getTokenLocation());
201+
parser.skipChildren();
204202
}
205203
} else if (token == XContentParser.Token.START_OBJECT) {
206204
if (REASON_FIELD.equals(currentFieldName)) {
207205
exception = ElasticsearchException.fromXContent(parser);
208206
} else {
209-
throwUnknownField(currentFieldName, parser.getTokenLocation());
207+
parser.skipChildren();
210208
}
211209
} else {
212-
throwUnknownToken(token, parser.getTokenLocation());
210+
parser.skipChildren();
213211
}
214212
}
215213
return new ShardSearchFailure(exception,

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.io.IOException;
3434

3535
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
36+
import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
3637

3738
public class ShardSearchFailureTests extends ESTestCase {
3839

@@ -48,13 +49,31 @@ public static ShardSearchFailure createTestItem() {
4849
}
4950

5051
public void testFromXContent() throws IOException {
52+
doFromXContentTestWithRandomFields(false);
53+
}
54+
55+
/**
56+
* This test adds random fields and objects to the xContent rendered out to
57+
* ensure we can parse it back to be forward compatible with additions to
58+
* the xContent
59+
*/
60+
public void testFromXContentWithRandomFields() throws IOException {
61+
doFromXContentTestWithRandomFields(true);
62+
}
63+
64+
private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws IOException {
5165
ShardSearchFailure response = createTestItem();
5266
XContentType xContentType = randomFrom(XContentType.values());
5367
boolean humanReadable = randomBoolean();
5468
BytesReference originalBytes = toShuffledXContent(response, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
55-
69+
BytesReference mutated;
70+
if (addRandomFields) {
71+
mutated = insertRandomFields(xContentType, originalBytes, null, random());
72+
} else {
73+
mutated = originalBytes;
74+
}
5675
ShardSearchFailure parsed;
57-
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
76+
try (XContentParser parser = createParser(xContentType.xContent(), mutated)) {
5877
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
5978
parsed = ShardSearchFailure.fromXContent(parser);
6079
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
@@ -64,8 +83,11 @@ public void testFromXContent() throws IOException {
6483
assertEquals(response.shard().getNodeId(), parsed.shard().getNodeId());
6584
assertEquals(response.shardId(), parsed.shardId());
6685

67-
// we cannot compare the cause, because it will be wrapped in an outer ElasticSearchException
68-
// best effort: try to check that the original message appears somewhere in the rendered xContent
86+
/**
87+
* we cannot compare the cause, because it will be wrapped in an outer
88+
* ElasticSearchException best effort: try to check that the original
89+
* message appears somewhere in the rendered xContent
90+
*/
6991
String originalMsg = response.getCause().getMessage();
7092
assertEquals(parsed.getCause().getMessage(), "Elasticsearch exception [type=parsing_exception, reason=" + originalMsg + "]");
7193
String nestedMsg = response.getCause().getCause().getMessage();

0 commit comments

Comments
 (0)