Browse Source

Fix failure in InnerHitBuilderTests around 'fields' option. (#62341)

The case InnerHitBuilderTests#testEqualsAndHashcode creates a copy of the object
by serializing + deserializing it, then applies a modification. If the 'fields'
list is empty, then deserializing it results in Collections.emptyList. Because
this is immutable, then modifying it can throw an UnsupportedOperationException.

This PR takes the same approach as for docvalue_fields, where we create a new
list instead of trying to add to an empty one.
Julie Tibshirani 5 years ago
parent
commit
c1d9037647

+ 1 - 1
server/src/main/java/org/elasticsearch/index/query/InnerHitBuilder.java

@@ -395,7 +395,7 @@ public final class InnerHitBuilder implements Writeable, ToXContentObject {
      * @param format an optional format string used when formatting values, for example a date format.
      */
     public InnerHitBuilder addFetchField(String name, @Nullable String format) {
-        if (fetchFields == null) {
+        if (fetchFields == null || fetchFields.isEmpty()) {
             fetchFields = new ArrayList<>();
         }
         fetchFields.add(new FieldAndFormat(name, format));