|
@@ -13,12 +13,9 @@ import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
import org.elasticsearch.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.xcontent.XContentParser;
|
|
|
-import org.elasticsearch.xcontent.XContentType;
|
|
|
import org.elasticsearch.xcontent.json.JsonXContent;
|
|
|
-import org.hamcrest.Matchers;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.Arrays;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -29,7 +26,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
final ContentPath contentPath = new ContentPath();
|
|
|
try (
|
|
|
XContentParser inputParser = createParser(JsonXContent.jsonXContent, withDots);
|
|
|
- XContentParser expandedParser = DotExpandingXContentParser.expandDots(inputParser, contentPath, null)
|
|
|
+ XContentParser expandedParser = DotExpandingXContentParser.expandDots(inputParser, contentPath)
|
|
|
) {
|
|
|
expandedParser.allowDuplicateKeys(true);
|
|
|
|
|
@@ -40,7 +37,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
expectedParser.allowDuplicateKeys(true);
|
|
|
try (
|
|
|
var p = createParser(JsonXContent.jsonXContent, withDots);
|
|
|
- XContentParser actualParser = DotExpandingXContentParser.expandDots(p, contentPath, null)
|
|
|
+ XContentParser actualParser = DotExpandingXContentParser.expandDots(p, contentPath)
|
|
|
) {
|
|
|
XContentParser.Token currentToken;
|
|
|
while ((currentToken = actualParser.nextToken()) != null) {
|
|
@@ -130,7 +127,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
public void testDotsCollapsingFlatPaths() throws IOException {
|
|
|
ContentPath contentPath = new ContentPath();
|
|
|
XContentParser parser = DotExpandingXContentParser.expandDots(createParser(JsonXContent.jsonXContent, """
|
|
|
- {"metrics.service.time": 10, "metrics.service.time.max": 500, "metrics.foo": "value"}"""), contentPath, null);
|
|
|
+ {"metrics.service.time": 10, "metrics.service.time.max": 500, "metrics.foo": "value"}"""), contentPath);
|
|
|
parser.nextToken();
|
|
|
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
|
|
|
assertEquals("metrics", parser.currentName());
|
|
@@ -200,7 +197,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
},
|
|
|
"foo" : "value"
|
|
|
}
|
|
|
- }"""), contentPath, null);
|
|
|
+ }"""), contentPath);
|
|
|
parser.nextToken();
|
|
|
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
|
|
|
assertEquals("metrics", parser.currentName());
|
|
@@ -238,7 +235,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
|
|
|
public void testSkipChildren() throws IOException {
|
|
|
XContentParser parser = DotExpandingXContentParser.expandDots(createParser(JsonXContent.jsonXContent, """
|
|
|
- { "test.with.dots" : "value", "nodots" : "value2" }"""), new ContentPath(), null);
|
|
|
+ { "test.with.dots" : "value", "nodots" : "value2" }"""), new ContentPath());
|
|
|
parser.nextToken(); // start object
|
|
|
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
|
|
|
assertEquals("test", parser.currentName());
|
|
@@ -261,7 +258,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
|
|
|
public void testSkipChildrenWithinInnerObject() throws IOException {
|
|
|
XContentParser parser = DotExpandingXContentParser.expandDots(createParser(JsonXContent.jsonXContent, """
|
|
|
- { "test.with.dots" : {"obj" : {"field":"value"}}, "nodots" : "value2" }"""), new ContentPath(), null);
|
|
|
+ { "test.with.dots" : {"obj" : {"field":"value"}}, "nodots" : "value2" }"""), new ContentPath());
|
|
|
|
|
|
parser.nextToken(); // start object
|
|
|
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
|
|
@@ -309,8 +306,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
XContentParser expectedParser = createParser(JsonXContent.jsonXContent, jsonInput);
|
|
|
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
|
|
|
createParser(JsonXContent.jsonXContent, jsonInput),
|
|
|
- new ContentPath(),
|
|
|
- null
|
|
|
+ new ContentPath()
|
|
|
);
|
|
|
|
|
|
assertEquals(expectedParser.getTokenLocation(), dotExpandedParser.getTokenLocation());
|
|
@@ -368,8 +364,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
public void testParseMapUOE() throws Exception {
|
|
|
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
|
|
|
createParser(JsonXContent.jsonXContent, ""),
|
|
|
- new ContentPath(),
|
|
|
- null
|
|
|
+ new ContentPath()
|
|
|
);
|
|
|
expectThrows(UnsupportedOperationException.class, dotExpandedParser::map);
|
|
|
}
|
|
@@ -377,8 +372,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
public void testParseMapOrderedUOE() throws Exception {
|
|
|
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
|
|
|
createParser(JsonXContent.jsonXContent, ""),
|
|
|
- new ContentPath(),
|
|
|
- null
|
|
|
+ new ContentPath()
|
|
|
);
|
|
|
expectThrows(UnsupportedOperationException.class, dotExpandedParser::mapOrdered);
|
|
|
}
|
|
@@ -386,8 +380,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
public void testParseMapStringsUOE() throws Exception {
|
|
|
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
|
|
|
createParser(JsonXContent.jsonXContent, ""),
|
|
|
- new ContentPath(),
|
|
|
- null
|
|
|
+ new ContentPath()
|
|
|
);
|
|
|
expectThrows(UnsupportedOperationException.class, dotExpandedParser::mapStrings);
|
|
|
}
|
|
@@ -395,8 +388,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
public void testParseMapSupplierUOE() throws Exception {
|
|
|
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
|
|
|
createParser(JsonXContent.jsonXContent, ""),
|
|
|
- new ContentPath(),
|
|
|
- null
|
|
|
+ new ContentPath()
|
|
|
);
|
|
|
expectThrows(UnsupportedOperationException.class, () -> dotExpandedParser.map(HashMap::new, XContentParser::text));
|
|
|
}
|
|
@@ -411,8 +403,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
contentPath.setWithinLeafObject(true);
|
|
|
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
|
|
|
createParser(JsonXContent.jsonXContent, jsonInput),
|
|
|
- contentPath,
|
|
|
- null
|
|
|
+ contentPath
|
|
|
);
|
|
|
assertEquals(XContentParser.Token.START_OBJECT, dotExpandedParser.nextToken());
|
|
|
assertEquals(XContentParser.Token.FIELD_NAME, dotExpandedParser.nextToken());
|
|
@@ -427,8 +418,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
public void testParseListUOE() throws Exception {
|
|
|
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
|
|
|
createParser(JsonXContent.jsonXContent, ""),
|
|
|
- new ContentPath(),
|
|
|
- null
|
|
|
+ new ContentPath()
|
|
|
);
|
|
|
expectThrows(UnsupportedOperationException.class, dotExpandedParser::list);
|
|
|
}
|
|
@@ -436,8 +426,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
public void testParseListOrderedUOE() throws Exception {
|
|
|
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
|
|
|
createParser(JsonXContent.jsonXContent, ""),
|
|
|
- new ContentPath(),
|
|
|
- null
|
|
|
+ new ContentPath()
|
|
|
);
|
|
|
expectThrows(UnsupportedOperationException.class, dotExpandedParser::listOrderedMap);
|
|
|
}
|
|
@@ -451,8 +440,7 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
contentPath.setWithinLeafObject(true);
|
|
|
XContentParser dotExpandedParser = DotExpandingXContentParser.expandDots(
|
|
|
createParser(JsonXContent.jsonXContent, jsonInput),
|
|
|
- contentPath,
|
|
|
- null
|
|
|
+ contentPath
|
|
|
);
|
|
|
assertEquals(XContentParser.Token.START_OBJECT, dotExpandedParser.nextToken());
|
|
|
assertEquals(XContentParser.Token.FIELD_NAME, dotExpandedParser.nextToken());
|
|
@@ -462,104 +450,4 @@ public class DotExpandingXContentParserTests extends ESTestCase {
|
|
|
assertEquals("one", list.get(0));
|
|
|
assertEquals("two", list.get(1));
|
|
|
}
|
|
|
-
|
|
|
- private static DocumentParserContext createContext(XContentBuilder builder) throws IOException {
|
|
|
- var documentMapper = new MapperServiceTestCase() {
|
|
|
- }.createDocumentMapper(builder);
|
|
|
- return new TestDocumentParserContext(documentMapper.mappers(), null);
|
|
|
- }
|
|
|
-
|
|
|
- private static List<String> getSubPaths(XContentBuilder builder, String... path) throws IOException {
|
|
|
- DocumentParserContext context = createContext(builder);
|
|
|
- return DotExpandingXContentParser.maybeFlattenPaths(Arrays.stream(path).toList(), context, new ContentPath());
|
|
|
- }
|
|
|
-
|
|
|
- private static List<String> getSubPaths(XContentBuilder builder, List<String> contentPath, List<String> path) throws IOException {
|
|
|
- DocumentParserContext context = createContext(builder);
|
|
|
- ContentPath content = new ContentPath();
|
|
|
- for (String c : contentPath) {
|
|
|
- content.add(c);
|
|
|
- }
|
|
|
- return DotExpandingXContentParser.maybeFlattenPaths(path, context, content);
|
|
|
- }
|
|
|
-
|
|
|
- public void testAutoFlattening() throws Exception {
|
|
|
- var b = XContentBuilder.builder(XContentType.JSON.xContent());
|
|
|
- b.startObject().startObject("_doc");
|
|
|
- {
|
|
|
- b.field("subobjects", "auto");
|
|
|
- b.startObject("properties");
|
|
|
- {
|
|
|
- b.startObject("path").startObject("properties");
|
|
|
- {
|
|
|
- b.startObject("to").startObject("properties");
|
|
|
- {
|
|
|
- b.startObject("field").field("type", "integer").endObject();
|
|
|
- }
|
|
|
- b.endObject().endObject();
|
|
|
- }
|
|
|
- b.endObject().endObject();
|
|
|
- b.startObject("path.auto").field("subobjects", "auto").startObject("properties");
|
|
|
- {
|
|
|
- b.startObject("to").startObject("properties");
|
|
|
- {
|
|
|
- b.startObject("some.field").field("type", "integer").endObject();
|
|
|
- }
|
|
|
- b.endObject().endObject();
|
|
|
- b.startObject("inner.enabled").field("dynamic", "false").startObject("properties");
|
|
|
- {
|
|
|
- b.startObject("field").field("type", "integer").endObject();
|
|
|
- }
|
|
|
- b.endObject().endObject();
|
|
|
- }
|
|
|
- b.endObject().endObject();
|
|
|
- b.startObject("path.disabled").field("subobjects", "false").startObject("properties");
|
|
|
- {
|
|
|
- b.startObject("to").startObject("properties");
|
|
|
- {
|
|
|
- b.startObject("some.field").field("type", "integer").endObject();
|
|
|
- }
|
|
|
- b.endObject().endObject();
|
|
|
- }
|
|
|
- b.endObject().endObject();
|
|
|
- }
|
|
|
- b.endObject();
|
|
|
- }
|
|
|
- b.endObject().endObject();
|
|
|
-
|
|
|
- // inner [subobjects:enabled] gets flattened
|
|
|
- assertThat(getSubPaths(b, "field"), Matchers.contains("field"));
|
|
|
- assertThat(getSubPaths(b, "path", "field"), Matchers.contains("path.field"));
|
|
|
- assertThat(getSubPaths(b, "path", "to", "field"), Matchers.contains("path.to.field"));
|
|
|
- assertThat(getSubPaths(b, "path", "to", "any"), Matchers.contains("path.to.any"));
|
|
|
-
|
|
|
- // inner [subobjects:auto] does not get flattened
|
|
|
- assertThat(getSubPaths(b, "path", "auto", "field"), Matchers.contains("path.auto", "field"));
|
|
|
- assertThat(getSubPaths(b, "path", "auto", "some", "field"), Matchers.contains("path.auto", "some.field"));
|
|
|
- assertThat(getSubPaths(b, "path", "auto", "to", "some", "field"), Matchers.contains("path.auto", "to.some.field"));
|
|
|
- assertThat(getSubPaths(b, "path", "auto", "to", "some", "other"), Matchers.contains("path.auto", "to.some.other"));
|
|
|
- assertThat(getSubPaths(b, "path", "auto", "inner", "enabled", "field"), Matchers.contains("path.auto", "inner.enabled", "field"));
|
|
|
- assertThat(
|
|
|
- getSubPaths(b, "path", "auto", "inner", "enabled", "to", "some", "field"),
|
|
|
- Matchers.contains("path.auto", "inner.enabled", "to", "some", "field")
|
|
|
- );
|
|
|
-
|
|
|
- // inner [subobjects:disabled] gets flattened
|
|
|
- assertThat(getSubPaths(b, "path", "disabled", "field"), Matchers.contains("path.disabled.field"));
|
|
|
- assertThat(getSubPaths(b, "path", "disabled", "some", "field"), Matchers.contains("path.disabled.some.field"));
|
|
|
- assertThat(getSubPaths(b, "path", "disabled", "to", "some", "field"), Matchers.contains("path.disabled.to.some.field"));
|
|
|
- assertThat(getSubPaths(b, "path", "disabled", "to", "some", "other"), Matchers.contains("path.disabled.to.some.other"));
|
|
|
-
|
|
|
- // Non-empty content path.
|
|
|
- assertThat(getSubPaths(b, List.of("path"), List.of("field")), Matchers.contains("field"));
|
|
|
- assertThat(getSubPaths(b, List.of("path"), List.of("to", "field")), Matchers.contains("to", "field"));
|
|
|
- assertThat(getSubPaths(b, List.of("path", "to"), List.of("field")), Matchers.contains("field"));
|
|
|
- assertThat(getSubPaths(b, List.of("path"), List.of("auto", "field")), Matchers.contains("auto", "field"));
|
|
|
- assertThat(getSubPaths(b, List.of("path", "auto"), List.of("to", "some", "field")), Matchers.contains("to.some.field"));
|
|
|
- assertThat(
|
|
|
- getSubPaths(b, List.of("path", "auto"), List.of("inner", "enabled", "to", "some", "field")),
|
|
|
- Matchers.contains("inner.enabled", "to", "some", "field")
|
|
|
- );
|
|
|
- assertThat(getSubPaths(b, List.of("path", "disabled"), List.of("to", "some", "field")), Matchers.contains("to", "some", "field"));
|
|
|
- }
|
|
|
}
|