Skip to content

Commit

Permalink
Don't fail on duplicated content field filters
Browse files Browse the repository at this point in the history
Set.of() throws a runtime exception if it encounters duplicated elements.
  • Loading branch information
arteam committed Mar 28, 2022
1 parent e8498dc commit 852bcfc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,7 +38,7 @@

public class XContentFieldFilterTests extends AbstractFilteringTestCase {
@Override
protected void testFilter(Builder expected, Builder actual, Set<String> includes, Set<String> excludes) throws IOException {
protected void testFilter(Builder expected, Builder actual, Collection<String> includes, Collection<String> excludes) throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
final boolean humanReadable = randomBoolean();
String[] sourceIncludes;
Expand All @@ -56,7 +58,7 @@ protected void testFilter(Builder expected, Builder actual, Set<String> includes
assertMap(XContentHelper.convertToMap(ref, true, xContentType).v2(), matchesMap(toMap(expected, xContentType, humanReadable)));
}

private void testFilter(String expectedJson, String actualJson, Set<String> includes, Set<String> excludes) throws IOException {
private void testFilter(String expectedJson, String actualJson, Collection<String> includes, Collection<String> excludes) throws IOException {
CheckedFunction<String, Builder, IOException> toBuilder = json -> {
XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, new BytesArray(json), XContentType.JSON);
if ((parser.currentToken() == null) && (parser.nextToken() == null)) {
Expand All @@ -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 = """
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,7 +44,8 @@
public class XContentMapValuesTests extends AbstractFilteringTestCase {

@Override
protected void testFilter(Builder expected, Builder actual, Set<String> includes, Set<String> excludes) throws IOException {
protected void testFilter(Builder expected, Builder actual, Collection<String> includes, Collection<String> excludes)
throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
final boolean humanReadable = randomBoolean();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,7 +42,7 @@ public abstract class AbstractFilteringTestCase extends ESTestCase {
@FunctionalInterface
protected interface Builder extends CheckedFunction<XContentBuilder, XContentBuilder, IOException> {}

protected abstract void testFilter(Builder expected, Builder actual, Set<String> includes, Set<String> excludes) throws IOException;
protected abstract void testFilter(Builder expected, Builder actual, Collection<String> includes, Collection<String> excludes) throws IOException;

/** Sample test case */
protected static final Builder SAMPLE = builderFor("sample.json");
Expand Down

0 comments on commit 852bcfc

Please sign in to comment.