|
13 | 13 | import org.elasticsearch.test.ESTestCase;
|
14 | 14 | import org.elasticsearch.xcontent.XContentType;
|
15 | 15 |
|
| 16 | +import java.util.Arrays; |
16 | 17 | import java.util.List;
|
17 | 18 | import java.util.Map;
|
18 | 19 |
|
@@ -111,4 +112,23 @@ public Source filter(SourceFilter sourceFilter) {
|
111 | 112 |
|
112 | 113 | }
|
113 | 114 |
|
| 115 | + // Verification for issue #109668 |
| 116 | + public void testIncludeParentAndExcludeChildEmptyArray() { |
| 117 | + Source fromMap = Source.fromMap(Map.of("myArray", List.of()), XContentType.JSON); |
| 118 | + Source filteredMap = fromMap.filter(new SourceFilter(new String[] {"myArray"}, new String[]{"myArray.myField"})); |
| 119 | + assertEquals(filteredMap.source(), Map.of("myArray", List.of())); |
| 120 | + Source fromBytes = Source.fromBytes(new BytesArray("{\"myArray\": []}"), XContentType.JSON); |
| 121 | + Source filteredBytes = fromBytes.filter(new SourceFilter(new String[] {"myArray"}, new String[]{"myArray.myField"})); |
| 122 | + assertEquals(filteredBytes.source(), Map.of("myArray", List.of())); |
| 123 | + } |
| 124 | + |
| 125 | + public void testIncludeParentAndExcludeChildSubFields() { |
| 126 | + Source fromMap = Source.fromMap(Map.of("myArray", List.of(Map.<String, Object>of("myField", "myValue", "other", "otherValue"))), XContentType.JSON); |
| 127 | + Source filteredMap = fromMap.filter(new SourceFilter(new String[] {"myArray"}, new String[]{"myArray.myField"})); |
| 128 | + assertEquals(filteredMap.source(), Map.of("myArray", List.of(Map.of("other", "otherValue")))); |
| 129 | + Source fromBytes = Source.fromBytes(new BytesArray(""" |
| 130 | + { "myArray": [ { "myField": "myValue", "other": "otherValue" } ] }"""), XContentType.JSON); |
| 131 | + Source filteredBytes = fromBytes.filter(new SourceFilter(new String[] {"myArray"}, new String[]{"myArray.myField"})); |
| 132 | + assertEquals(filteredBytes.source(), Map.of("myArray", List.of(Map.of("other", "otherValue")))); |
| 133 | + } |
114 | 134 | }
|
0 commit comments