diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentFieldFilter.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentFieldFilter.java index 56a5dfa2faf25..3c5e1d07c345e 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentFieldFilter.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentFieldFilter.java @@ -24,8 +24,8 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.Map; -import java.util.Set; import java.util.function.Function; /** @@ -73,8 +73,8 @@ static XContentFieldFilter newFieldFilter(String[] includes, String[] excludes) }; } else { final XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withFiltering( - Set.of(includes), - Set.of(excludes), + new HashSet<>(Arrays.asList(includes)), + new HashSet<>(Arrays.asList(excludes)), true ); return (originalSource, contentType) -> { diff --git a/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentFieldFilterTests.java b/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentFieldFilterTests.java index 47ea35b15b8e6..2deeb974b00ed 100644 --- a/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentFieldFilterTests.java +++ b/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentFieldFilterTests.java @@ -23,7 +23,9 @@ import org.elasticsearch.xcontent.XContentType; import java.io.IOException; +import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.Set; @@ -36,7 +38,7 @@ public class XContentFieldFilterTests extends AbstractFilteringTestCase { @Override - protected void testFilter(Builder expected, Builder actual, Set includes, Set excludes) throws IOException { + protected void testFilter(Builder expected, Builder actual, Collection includes, Collection excludes) throws IOException { final XContentType xContentType = randomFrom(XContentType.values()); final boolean humanReadable = randomBoolean(); String[] sourceIncludes; @@ -56,7 +58,7 @@ protected void testFilter(Builder expected, Builder actual, Set includes assertMap(XContentHelper.convertToMap(ref, true, xContentType).v2(), matchesMap(toMap(expected, xContentType, humanReadable))); } - private void testFilter(String expectedJson, String actualJson, Set includes, Set excludes) throws IOException { + private void testFilter(String expectedJson, String actualJson, Collection includes, Collection excludes) throws IOException { CheckedFunction toBuilder = json -> { XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, new BytesArray(json), XContentType.JSON); if ((parser.currentToken() == null) && (parser.nextToken() == null)) { @@ -82,6 +84,36 @@ public void testPrefixedNamesFilteringTest() throws IOException { testFilter(expected, actual, singleton("obj_name"), emptySet()); } + public void testDuplicatedIncludes() throws IOException { + String actual = """ + { + "obj": "value", + "obj_name": "value_name" + } + """; + String expected = """ + { + "obj_name": "value_name" + } + """; + testFilter(expected, actual, List.of("obj_name", "obj_name"), emptySet()); + } + + public void testDuplicatedExcludes() throws IOException { + String actual = """ + { + "obj": "value", + "obj_name": "value_name" + } + """; + String expected = """ + { + "obj": "value" + } + """; + testFilter(expected, actual, emptySet(), List.of("obj_name", "obj_name")); + } + public void testNestedFiltering() throws IOException { String actual = """ { diff --git a/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java b/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java index faf24fe13c444..c4fbe9b2d340f 100644 --- a/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java +++ b/server/src/test/java/org/elasticsearch/common/xcontent/support/XContentMapValuesTests.java @@ -22,11 +22,11 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; import static org.elasticsearch.common.xcontent.XContentHelper.convertToMap; import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; @@ -44,7 +44,8 @@ public class XContentMapValuesTests extends AbstractFilteringTestCase { @Override - protected void testFilter(Builder expected, Builder actual, Set includes, Set excludes) throws IOException { + protected void testFilter(Builder expected, Builder actual, Collection includes, Collection excludes) + throws IOException { final XContentType xContentType = randomFrom(XContentType.values()); final boolean humanReadable = randomBoolean(); diff --git a/test/framework/src/main/java/org/elasticsearch/common/xcontent/support/AbstractFilteringTestCase.java b/test/framework/src/main/java/org/elasticsearch/common/xcontent/support/AbstractFilteringTestCase.java index 4a3b3d27c75ab..e62e670c57ea1 100644 --- a/test/framework/src/main/java/org/elasticsearch/common/xcontent/support/AbstractFilteringTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/common/xcontent/support/AbstractFilteringTestCase.java @@ -26,6 +26,7 @@ import java.io.InputStreamReader; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; +import java.util.Collection; import java.util.Set; import static java.util.Collections.emptySet; @@ -41,7 +42,7 @@ public abstract class AbstractFilteringTestCase extends ESTestCase { @FunctionalInterface protected interface Builder extends CheckedFunction {} - protected abstract void testFilter(Builder expected, Builder actual, Set includes, Set excludes) throws IOException; + protected abstract void testFilter(Builder expected, Builder actual, Collection includes, Collection excludes) throws IOException; /** Sample test case */ protected static final Builder SAMPLE = builderFor("sample.json");