Forráskód Böngészése

Fix IngestDocument.deepCopy to support sets (#63067)

Alexander Reelsen 5 éve
szülő
commit
4043b18462

+ 8 - 0
server/src/main/java/org/elasticsearch/ingest/IngestDocument.java

@@ -37,6 +37,7 @@ import java.util.Collections;
 import java.util.Date;
 import java.util.EnumMap;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
@@ -712,6 +713,13 @@ public final class IngestDocument {
                 copy.add(deepCopy(itemValue));
             }
             return copy;
+        } else if (value instanceof Set) {
+            Set<?> setValue = (Set<?>) value;
+            Set<Object> copy = new HashSet<>(setValue.size());
+            for (Object itemValue : setValue) {
+                copy.add(deepCopy(itemValue));
+            }
+            return copy;
         } else if (value instanceof byte[]) {
             byte[] bytes = (byte[]) value;
             return Arrays.copyOf(bytes, bytes.length);

+ 18 - 1
server/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java

@@ -27,21 +27,24 @@ import org.elasticsearch.common.xcontent.XContentFactory;
 import org.elasticsearch.common.xcontent.XContentHelper;
 import org.elasticsearch.common.xcontent.XContentParser;
 import org.elasticsearch.common.xcontent.XContentType;
-import org.elasticsearch.ingest.RandomDocumentPicks;
 import org.elasticsearch.ingest.IngestDocument;
+import org.elasticsearch.ingest.RandomDocumentPicks;
 import org.elasticsearch.test.AbstractXContentTestCase;
 import org.elasticsearch.test.RandomObjects;
 
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 import java.util.StringJoiner;
 import java.util.function.Predicate;
 
 import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS;
 import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument;
 import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.instanceOf;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.not;
 
@@ -156,6 +159,20 @@ public class WriteableIngestDocumentTests extends AbstractXContentTestCase<Write
         assertThat(serializedIngestDocument, equalTo(serializedIngestDocument));
     }
 
+    public void testXContentHashSetSerialization() throws Exception {
+        IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of("key", Set.of("value")));
+        final WriteableIngestDocument writeableIngestDocument = new WriteableIngestDocument(ingestDocument);
+        try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
+            builder.startObject();
+            writeableIngestDocument.toXContent(builder, EMPTY_PARAMS);
+            builder.endObject();
+            Map<String, Object> map = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
+            assertThat(map.get("doc"), is(instanceOf(Map.class)));
+            Map<String, Object> source = (Map<String, Object>) ((Map) map.get("doc")).get("_source");
+            assertThat(source.get("key"), is(Arrays.asList("value")));
+        }
+    }
+
     static IngestDocument createRandomIngestDoc() {
         XContentType xContentType = randomFrom(XContentType.values());
         BytesReference sourceBytes = RandomObjects.randomSource(random(), xContentType);