Skip to content

Commit 4043b18

Browse files
authored
Fix IngestDocument.deepCopy to support sets (elastic#63067)
1 parent b827686 commit 4043b18

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

server/src/main/java/org/elasticsearch/ingest/IngestDocument.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Date;
3838
import java.util.EnumMap;
3939
import java.util.HashMap;
40+
import java.util.HashSet;
4041
import java.util.LinkedHashSet;
4142
import java.util.List;
4243
import java.util.Map;
@@ -712,6 +713,13 @@ private static Object deepCopy(Object value) {
712713
copy.add(deepCopy(itemValue));
713714
}
714715
return copy;
716+
} else if (value instanceof Set) {
717+
Set<?> setValue = (Set<?>) value;
718+
Set<Object> copy = new HashSet<>(setValue.size());
719+
for (Object itemValue : setValue) {
720+
copy.add(deepCopy(itemValue));
721+
}
722+
return copy;
715723
} else if (value instanceof byte[]) {
716724
byte[] bytes = (byte[]) value;
717725
return Arrays.copyOf(bytes, bytes.length);

server/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,24 @@
2727
import org.elasticsearch.common.xcontent.XContentHelper;
2828
import org.elasticsearch.common.xcontent.XContentParser;
2929
import org.elasticsearch.common.xcontent.XContentType;
30-
import org.elasticsearch.ingest.RandomDocumentPicks;
3130
import org.elasticsearch.ingest.IngestDocument;
31+
import org.elasticsearch.ingest.RandomDocumentPicks;
3232
import org.elasticsearch.test.AbstractXContentTestCase;
3333
import org.elasticsearch.test.RandomObjects;
3434

3535
import java.io.IOException;
36+
import java.util.Arrays;
3637
import java.util.Collections;
3738
import java.util.HashMap;
3839
import java.util.Map;
40+
import java.util.Set;
3941
import java.util.StringJoiner;
4042
import java.util.function.Predicate;
4143

4244
import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS;
4345
import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument;
4446
import static org.hamcrest.Matchers.equalTo;
47+
import static org.hamcrest.Matchers.instanceOf;
4548
import static org.hamcrest.Matchers.is;
4649
import static org.hamcrest.Matchers.not;
4750

@@ -156,6 +159,20 @@ public void testToXContent() throws IOException {
156159
assertThat(serializedIngestDocument, equalTo(serializedIngestDocument));
157160
}
158161

162+
public void testXContentHashSetSerialization() throws Exception {
163+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Map.of("key", Set.of("value")));
164+
final WriteableIngestDocument writeableIngestDocument = new WriteableIngestDocument(ingestDocument);
165+
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
166+
builder.startObject();
167+
writeableIngestDocument.toXContent(builder, EMPTY_PARAMS);
168+
builder.endObject();
169+
Map<String, Object> map = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
170+
assertThat(map.get("doc"), is(instanceOf(Map.class)));
171+
Map<String, Object> source = (Map<String, Object>) ((Map) map.get("doc")).get("_source");
172+
assertThat(source.get("key"), is(Arrays.asList("value")));
173+
}
174+
}
175+
159176
static IngestDocument createRandomIngestDoc() {
160177
XContentType xContentType = randomFrom(XContentType.values());
161178
BytesReference sourceBytes = RandomObjects.randomSource(random(), xContentType);

0 commit comments

Comments
 (0)