43
43
import java .util .ArrayList ;
44
44
import java .util .Collections ;
45
45
import java .util .List ;
46
+ import java .util .function .Supplier ;
46
47
47
48
import static java .util .Collections .emptyList ;
48
49
import static java .util .Collections .singletonMap ;
@@ -144,7 +145,7 @@ static SearchResponse.Clusters randomClusters() {
144
145
* compare xContent, so we omit it here
145
146
*/
146
147
public void testFromXContent () throws IOException {
147
- doFromXContentTestWithRandomFields (createTestItem () , false );
148
+ doFromXContentTestWithRandomFields (this :: createTestItem , false );
148
149
}
149
150
150
151
/**
@@ -154,11 +155,16 @@ public void testFromXContent() throws IOException {
154
155
* fields to SearchHits, Aggregations etc... is tested in their own tests
155
156
*/
156
157
public void testFromXContentWithRandomFields () throws IOException {
157
- doFromXContentTestWithRandomFields (createMinimalTestItem () , true );
158
+ doFromXContentTestWithRandomFields (this :: createMinimalTestItem , true );
158
159
}
159
160
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 ();
161
163
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
+ }
162
168
boolean humanReadable = randomBoolean ();
163
169
final ToXContent .Params params = new ToXContent .MapParams (singletonMap (RestSearchAction .TYPED_KEYS_PARAM , "true" ));
164
170
BytesReference originalBytes = toShuffledXContent (response , xcontentType , params , humanReadable );
@@ -190,6 +196,10 @@ public void testFromXContentWithFailures() throws IOException {
190
196
}
191
197
SearchResponse response = createTestItem (failures );
192
198
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
+ }
193
203
final ToXContent .Params params = new ToXContent .MapParams (singletonMap (RestSearchAction .TYPED_KEYS_PARAM , "true" ));
194
204
BytesReference originalBytes = toShuffledXContent (response , xcontentType , params , randomBoolean ());
195
205
try (XContentParser parser = createParser (xcontentType .xContent (), originalBytes )) {
@@ -216,6 +226,14 @@ public void testFromXContentWithFailures() throws IOException {
216
226
}
217
227
}
218
228
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
+
219
237
public void testToXContent () {
220
238
SearchHit hit = new SearchHit (1 , "id1" , new Text ("type" ), Collections .emptyMap (), Collections .emptyMap ());
221
239
hit .score (2.0f );
0 commit comments