Skip to content

Commit fbce9c5

Browse files
authored
Fix test failures for long YAML xContent (#122097)
Some search response and aggregations tests can create very large xContent objects over 3Mb in size. Parsing these works for JSON and other xContent types, but snakeyaml limits the input size to 3Mb since version 1.32 which can lead to test failures for very large inputs. This change adds a length check in case we test YAML xContent and creates new test instances if the length is to large. Closes #121666
1 parent cfc9369 commit fbce9c5

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

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

+21-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.ArrayList;
4444
import java.util.Collections;
4545
import java.util.List;
46+
import java.util.function.Supplier;
4647

4748
import static java.util.Collections.emptyList;
4849
import static java.util.Collections.singletonMap;
@@ -144,7 +145,7 @@ static SearchResponse.Clusters randomClusters() {
144145
* compare xContent, so we omit it here
145146
*/
146147
public void testFromXContent() throws IOException {
147-
doFromXContentTestWithRandomFields(createTestItem(), false);
148+
doFromXContentTestWithRandomFields(this::createTestItem, false);
148149
}
149150

150151
/**
@@ -154,11 +155,16 @@ public void testFromXContent() throws IOException {
154155
* fields to SearchHits, Aggregations etc... is tested in their own tests
155156
*/
156157
public void testFromXContentWithRandomFields() throws IOException {
157-
doFromXContentTestWithRandomFields(createMinimalTestItem(), true);
158+
doFromXContentTestWithRandomFields(this::createMinimalTestItem, true);
158159
}
159160

160-
private void doFromXContentTestWithRandomFields(SearchResponse response, boolean addRandomFields) throws IOException {
161+
private void doFromXContentTestWithRandomFields(Supplier<SearchResponse> responseSupplier, boolean addRandomFields) throws IOException {
162+
SearchResponse response = responseSupplier.get();
161163
XContentType xcontentType = randomFrom(XContentType.values());
164+
if (xcontentType.equals(XContentType.YAML) && isLargeResponse(response)) {
165+
// restrict YAML xContent response to < 3MB because of limit in snakeyaml input
166+
response = randomValueOtherThanMany(SearchResponseTests::isLargeResponse, responseSupplier);
167+
}
162168
boolean humanReadable = randomBoolean();
163169
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
164170
BytesReference originalBytes = toShuffledXContent(response, xcontentType, params, humanReadable);
@@ -190,6 +196,10 @@ public void testFromXContentWithFailures() throws IOException {
190196
}
191197
SearchResponse response = createTestItem(failures);
192198
XContentType xcontentType = randomFrom(XContentType.values());
199+
if (xcontentType.equals(XContentType.YAML) && isLargeResponse(response)) {
200+
// restrict YAML xContent response to < 3MB because of limit in snakeyaml input
201+
response = randomValueOtherThanMany(SearchResponseTests::isLargeResponse, () -> createTestItem(failures));
202+
}
193203
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
194204
BytesReference originalBytes = toShuffledXContent(response, xcontentType, params, randomBoolean());
195205
try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) {
@@ -216,6 +226,14 @@ public void testFromXContentWithFailures() throws IOException {
216226
}
217227
}
218228

229+
/**
230+
* returns true if the response object string is larger than 3 MB since this might create issues with YAML
231+
* xContent parsing
232+
*/
233+
private static boolean isLargeResponse(SearchResponse response) {
234+
return Strings.toString(response).length() > 3 * 1024 * 1024;
235+
}
236+
219237
public void testToXContent() {
220238
SearchHit hit = new SearchHit(1, "id1", new Text("type"), Collections.emptyMap(), Collections.emptyMap());
221239
hit.score(2.0f);

server/src/test/java/org/elasticsearch/search/aggregations/AggregationsTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package org.elasticsearch.search.aggregations;
1010

1111
import org.elasticsearch.common.ParsingException;
12+
import org.elasticsearch.common.Strings;
1213
import org.elasticsearch.common.bytes.BytesReference;
1314
import org.elasticsearch.common.xcontent.XContentHelper;
1415
import org.elasticsearch.rest.action.search.RestSearchAction;
@@ -207,6 +208,11 @@ private void parseAndAssert(boolean addRandomFields) throws IOException {
207208
XContentType xContentType = randomFrom(XContentType.values());
208209
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
209210
Aggregations aggregations = createTestInstance();
211+
if (xContentType.equals(XContentType.YAML) && isLargeAgg(aggregations)) {
212+
// restrict YAML xContent response to < 3MB because of limit in snakeyaml input
213+
aggregations = randomValueOtherThanMany(AggregationsTests::isLargeAgg, this::createTestInstance);
214+
}
215+
210216
BytesReference originalBytes = toShuffledXContent(aggregations, xContentType, params, randomBoolean());
211217
BytesReference mutated;
212218
if (addRandomFields) {
@@ -252,6 +258,14 @@ private void parseAndAssert(boolean addRandomFields) throws IOException {
252258
}
253259
}
254260

261+
/**
262+
* returns true if the aggregation object string is larger than 3 MB since this might create issues with YAML
263+
* xContent parsing
264+
*/
265+
private static boolean isLargeAgg(Aggregations agg) {
266+
return Strings.toString(agg).length() > 3 * 1024 * 1024;
267+
}
268+
255269
public void testParsingExceptionOnUnknownAggregation() throws IOException {
256270
XContentBuilder builder = XContentFactory.jsonBuilder();
257271
builder.startObject();

0 commit comments

Comments
 (0)