Skip to content

Commit 00e5d08

Browse files
committed
Fixes elastic#4047 - Empty objects are not stored in _source when an include/exclude list is present
1 parent a9fdcad commit 00e5d08

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private static void filter(Map<String, Object> map, Map<String, Object> into, St
205205
}
206206

207207

208-
if (entry.getValue() instanceof Map) {
208+
if (entry.getValue() instanceof Map && !((Map<String, Object>) entry.getValue()).isEmpty()) {
209209
Map<String, Object> innerInto = Maps.newHashMap();
210210
// if we had an exact match, we want give deeper excludes their chance
211211
filter((Map<String, Object>) entry.getValue(), innerInto, exactIncludeMatch ? Strings.EMPTY_ARRAY : includes, excludes, sb);

src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,122 @@ public void filterWithEmptyIncludesExcludes() {
368368
assertThat(filteredMap.get("field").toString(), equalTo("value"));
369369

370370
}
371+
372+
@Test
373+
public void testThatFilteringWithEmptyObjectAndExclusionWorks() throws Exception {
374+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
375+
.startObject("obj")
376+
.endObject()
377+
.endObject();
378+
379+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true);
380+
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[]{"nonExistingField"});
381+
382+
assertThat(mapTuple.v2(), equalTo(filteredSource));
383+
}
384+
385+
@Test
386+
public void testThatFilterIncludesEmptyObjectsWithoutExcludedProperties() throws Exception {
387+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
388+
.startObject("obj")
389+
.endObject()
390+
.endObject();
391+
392+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true);
393+
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[]{"obj.*"});
394+
395+
assertThat(filteredSource.size(), equalTo(1));
396+
assertThat(filteredSource, hasKey("obj"));
397+
assertThat(((Map) filteredSource.get("obj")).size(), equalTo(0));
398+
}
399+
400+
@Test
401+
public void testThatFilterOmitsObjectsWithExcludedProperties() throws Exception {
402+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
403+
.startObject("obj")
404+
.field("f1", "v1")
405+
.endObject()
406+
.endObject();
407+
408+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true);
409+
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[]{"obj.f1"});
410+
411+
assertThat(filteredSource.size(), equalTo(0));
412+
}
413+
414+
@Test
415+
public void testThatFilterIncludesObjectsWithSomeExcludedProperties() throws Exception {
416+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
417+
.startObject("obj")
418+
.field("f1", "v1")
419+
.field("f2", "v2")
420+
.endObject()
421+
.endObject();
422+
423+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true);
424+
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[]{"obj.f1"});
425+
426+
assertThat(filteredSource.size(), equalTo(1));
427+
assertThat(filteredSource, hasKey("obj"));
428+
assertThat(((Map) filteredSource.get("obj")).size(), equalTo(1));
429+
assertThat(((Map<String, Object>) filteredSource.get("obj")), hasKey("f2"));
430+
}
431+
432+
@Test
433+
public void testThatFilteringWithEmptyObjectAndInclusionWorks() throws Exception {
434+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
435+
.startObject("obj")
436+
.endObject()
437+
.endObject();
438+
439+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true);
440+
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[]{"obj"}, Strings.EMPTY_ARRAY);
441+
442+
assertThat(mapTuple.v2(), equalTo(filteredSource));
443+
}
444+
445+
@Test
446+
public void testThatFilterOmitsEmptyObjectsWithoutIncludedProperties() throws Exception {
447+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
448+
.startObject("obj")
449+
.endObject()
450+
.endObject();
451+
452+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true);
453+
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[]{"obj.*"}, Strings.EMPTY_ARRAY);
454+
455+
assertThat(filteredSource.size(), equalTo(0));
456+
}
457+
458+
@Test
459+
public void testThatFilterOmitsObjectsWithoutIncludedProperties() throws Exception {
460+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
461+
.startObject("obj")
462+
.field("f1", "v1")
463+
.endObject()
464+
.endObject();
465+
466+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true);
467+
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[]{"obj.f2"}, Strings.EMPTY_ARRAY);
468+
469+
assertThat(filteredSource.size(), equalTo(0));
470+
}
471+
472+
@Test
473+
public void testThatFilterIncludesObjectsWithSomeIncludedProperties() throws Exception {
474+
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
475+
.startObject("obj")
476+
.field("f1", "v1")
477+
.field("f2", "v2")
478+
.endObject()
479+
.endObject();
480+
481+
Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true);
482+
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[]{"obj.f2"}, Strings.EMPTY_ARRAY);
483+
484+
assertThat(filteredSource.size(), equalTo(1));
485+
assertThat(filteredSource, hasKey("obj"));
486+
assertThat(((Map) filteredSource.get("obj")).size(), equalTo(1));
487+
assertThat(((Map<String, Object>) filteredSource.get("obj")), hasKey("f2"));
488+
}
371489
}

0 commit comments

Comments
 (0)