4343import java .util .ArrayList ;
4444import java .util .Collections ;
4545import java .util .List ;
46+ import java .util .function .Supplier ;
4647
4748import static java .util .Collections .emptyList ;
4849import 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 );
0 commit comments