diff --git a/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java b/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java index 792a6890554..d5ac7a816e8 100644 --- a/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java +++ b/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java @@ -53,15 +53,24 @@ public class XContentMapValues { if (index == pathElements.length) { return; } - String currentPath = pathElements[index]; - Object currentValue = part.get(currentPath); + + String key = pathElements[index]; + Object currentValue = part.get(key); + int nextIndex = index + 1; + while (currentValue == null && nextIndex != pathElements.length) { + key += "." + pathElements[nextIndex]; + currentValue = part.get(key); + nextIndex++; + } + if (currentValue == null) { return; } + if (currentValue instanceof Map) { - extractRawValues(values, (Map) currentValue, pathElements, index + 1); + extractRawValues(values, (Map) currentValue, pathElements, nextIndex); } else if (currentValue instanceof List) { - extractRawValues(values, (List) currentValue, pathElements, index + 1); + extractRawValues(values, (List) currentValue, pathElements, nextIndex); } else { values.add(currentValue); } @@ -101,7 +110,15 @@ public class XContentMapValues { } if (currentValue instanceof Map) { Map map = (Map) currentValue; - return extractValue(pathElements, index + 1, map.get(pathElements[index])); + String key = pathElements[index]; + Object mapValue = map.get(key); + int nextIndex = index + 1; + while (mapValue == null && nextIndex != pathElements.length) { + key += "." + pathElements[nextIndex]; + mapValue = map.get(key); + nextIndex++; + } + return extractValue(pathElements, nextIndex, mapValue); } if (currentValue instanceof List) { List valueList = (List) currentValue; diff --git a/src/test/java/org/elasticsearch/test/unit/common/xcontent/support/XContentMapValuesTests.java b/src/test/java/org/elasticsearch/test/unit/common/xcontent/support/XContentMapValuesTests.java index 83c73e13707..077f0213153 100644 --- a/src/test/java/org/elasticsearch/test/unit/common/xcontent/support/XContentMapValuesTests.java +++ b/src/test/java/org/elasticsearch/test/unit/common/xcontent/support/XContentMapValuesTests.java @@ -148,5 +148,51 @@ public class XContentMapValuesTests { assertThat(extListValue.size(), equalTo(2)); assertThat(extListValue.get(0).toString(), equalTo("value1")); assertThat(extListValue.get(1).toString(), equalTo("value2")); + + // fields with . in them + builder = XContentFactory.jsonBuilder().startObject() + .field("xxx.yyy", "value") + .endObject(); + map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + assertThat(XContentMapValues.extractValue("xxx.yyy", map).toString(), equalTo("value")); + + builder = XContentFactory.jsonBuilder().startObject() + .startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject() + .endObject(); + + map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + assertThat(XContentMapValues.extractValue("path1.xxx.path2.yyy.test", map).toString(), equalTo("value")); + } + + @SuppressWarnings({"unchecked"}) + @Test + public void testExtractRawValue() throws Exception { + XContentBuilder builder = XContentFactory.jsonBuilder().startObject() + .field("test", "value") + .endObject(); + + Map map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + assertThat(XContentMapValues.extractRawValues("test", map).get(0).toString(), equalTo("value")); + + builder = XContentFactory.jsonBuilder().startObject() + .field("test.me", "value") + .endObject(); + + map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + assertThat(XContentMapValues.extractRawValues("test.me", map).get(0).toString(), equalTo("value")); + + builder = XContentFactory.jsonBuilder().startObject() + .startObject("path1").startObject("path2").field("test", "value").endObject().endObject() + .endObject(); + + map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + assertThat(XContentMapValues.extractRawValues("path1.path2.test", map).get(0).toString(), equalTo("value")); + + builder = XContentFactory.jsonBuilder().startObject() + .startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject() + .endObject(); + + map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose(); + assertThat(XContentMapValues.extractRawValues("path1.xxx.path2.yyy.test", map).get(0).toString(), equalTo("value")); } }