Skip to content

Commit 1f44591

Browse files
committed
Update Rollup Caps to allow unknown fields
This commit ensures that the parts of rollup caps that can allow unknown fields will allow them. It also modifies the test such that we can use the features we need for disallowing fields in spots where they would not be allowed. Relates elastic#36938 Backport of elastic#38339
1 parent 80ae5da commit 1f44591

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/RollableIndexCaps.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class RollableIndexCaps implements ToXContentFragment {
4444
public static final Function<String, ConstructingObjectParser<RollableIndexCaps, Void>> PARSER = indexName -> {
4545
@SuppressWarnings("unchecked")
4646
ConstructingObjectParser<RollableIndexCaps, Void> p
47-
= new ConstructingObjectParser<>(indexName,
47+
= new ConstructingObjectParser<>(indexName, true,
4848
a -> new RollableIndexCaps(indexName, (List<RollupJobCaps>) a[0]));
4949

5050
p.declareObjectArray(ConstructingObjectParser.constructorArg(), RollupJobCaps.PARSER::apply,

client/rest-high-level/src/main/java/org/elasticsearch/client/rollup/RollupJobCaps.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import java.util.List;
3434
import java.util.Map;
3535
import java.util.Objects;
36-
import java.util.function.Function;
36+
import java.util.stream.Collectors;
3737

3838
/**
3939
* Represents the Rollup capabilities for a specific job on a single rollup index
@@ -45,15 +45,12 @@ public class RollupJobCaps implements ToXContentObject {
4545
private static final ParseField FIELDS = new ParseField("fields");
4646
private static final String NAME = "rollup_job_caps";
4747

48-
public static final ConstructingObjectParser<RollupJobCaps, Void> PARSER = new ConstructingObjectParser<>(NAME,
48+
public static final ConstructingObjectParser<RollupJobCaps, Void> PARSER = new ConstructingObjectParser<>(NAME, true,
4949
a -> {
5050
@SuppressWarnings("unchecked")
5151
List<Tuple<String, RollupFieldCaps>> caps = (List<Tuple<String, RollupFieldCaps>>) a[3];
52-
if (caps.isEmpty()) {
53-
return new RollupJobCaps((String) a[0], (String) a[1], (String) a[2], Collections.emptyMap());
54-
}
55-
Map<String, RollupFieldCaps> mapCaps = new HashMap<>(caps.size());
56-
caps.forEach(c -> mapCaps.put(c.v1(), c.v2()));
52+
Map<String, RollupFieldCaps> mapCaps =
53+
new HashMap<>(caps.stream().collect(Collectors.toMap(Tuple::v1, Tuple::v2)));
5754
return new RollupJobCaps((String) a[0], (String) a[1], (String) a[2], mapCaps);
5855
});
5956

@@ -140,16 +137,6 @@ public static class RollupFieldCaps implements ToXContentFragment {
140137
private static final String NAME = "rollup_field_caps";
141138
private final List<Map<String, Object>> aggs;
142139

143-
public static final Function<String, ConstructingObjectParser<RollupFieldCaps, Void>> PARSER = fieldName -> {
144-
@SuppressWarnings("unchecked")
145-
ConstructingObjectParser<RollupFieldCaps, Void> parser
146-
= new ConstructingObjectParser<>(NAME, a -> new RollupFieldCaps((List<Map<String, Object>>) a[0]));
147-
148-
parser.declareObjectArray(ConstructingObjectParser.constructorArg(),
149-
(p, c) -> p.map(), new ParseField(fieldName));
150-
return parser;
151-
};
152-
153140
RollupFieldCaps(final List<Map<String, Object>> aggs) {
154141
this.aggs = Collections.unmodifiableList(Objects.requireNonNull(aggs));
155142
}
@@ -170,13 +157,12 @@ public static RollupFieldCaps fromXContent(XContentParser parser) throws IOExcep
170157
List<Map<String, Object>> aggs = new ArrayList<>();
171158
if (parser.nextToken().equals(XContentParser.Token.START_ARRAY)) {
172159
while (parser.nextToken().equals(XContentParser.Token.START_OBJECT)) {
173-
aggs.add(Collections.unmodifiableMap(parser.map()));
160+
aggs.add(parser.map());
174161
}
175162
}
176-
return new RollupFieldCaps(Collections.unmodifiableList(aggs));
163+
return new RollupFieldCaps(aggs);
177164
}
178165

179-
180166
@Override
181167
public boolean equals(Object other) {
182168
if (this == other) {

client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupCapsResponseTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.IOException;
2525
import java.util.Map;
26+
import java.util.function.Predicate;
2627

2728
public class GetRollupCapsResponseTests extends RollupCapsResponseTestCase<GetRollupCapsResponse> {
2829

@@ -40,6 +41,15 @@ protected void toXContent(GetRollupCapsResponse response, XContentBuilder builde
4041
builder.endObject();
4142
}
4243

44+
@Override
45+
protected Predicate<String> randomFieldsExcludeFilter() {
46+
return (field) ->
47+
// base cannot have extra things in it
48+
"".equals(field)
49+
// the field list expects to be a nested object of a certain type
50+
|| field.contains("fields");
51+
}
52+
4353
@Override
4454
protected GetRollupCapsResponse fromXContent(XContentParser parser) throws IOException {
4555
return GetRollupCapsResponse.fromXContent(parser);

client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsResponseTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.IOException;
2525
import java.util.Map;
26+
import java.util.function.Predicate;
2627

2728
public class GetRollupIndexCapsResponseTests extends RollupCapsResponseTestCase<GetRollupIndexCapsResponse> {
2829

@@ -40,6 +41,15 @@ protected void toXContent(GetRollupIndexCapsResponse response, XContentBuilder b
4041
builder.endObject();
4142
}
4243

44+
@Override
45+
protected Predicate<String> randomFieldsExcludeFilter() {
46+
return (field) ->
47+
// base cannot have extra things in it
48+
"".equals(field)
49+
// the field list expects to be a nested object of a certain type
50+
|| field.contains("fields");
51+
}
52+
4353
@Override
4454
protected GetRollupIndexCapsResponse fromXContent(XContentParser parser) throws IOException {
4555
return GetRollupIndexCapsResponse.fromXContent(parser);

client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/RollupCapsResponseTestCase.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.client.rollup.job.config.RollupJobConfig;
2626
import org.elasticsearch.client.rollup.job.config.RollupJobConfigTests;
2727
import org.elasticsearch.client.rollup.job.config.TermsGroupConfig;
28+
import org.elasticsearch.common.Strings;
2829
import org.elasticsearch.common.xcontent.XContentBuilder;
2930
import org.elasticsearch.common.xcontent.XContentParser;
3031
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
@@ -40,6 +41,7 @@
4041
import java.util.HashMap;
4142
import java.util.List;
4243
import java.util.Map;
44+
import java.util.function.Predicate;
4345
import java.util.stream.Collectors;
4446

4547
import static java.util.Collections.singletonMap;
@@ -55,15 +57,23 @@ abstract class RollupCapsResponseTestCase<T> extends ESTestCase {
5557

5658
protected abstract T fromXContent(XContentParser parser) throws IOException;
5759

60+
protected Predicate<String> randomFieldsExcludeFilter() {
61+
return field -> false;
62+
}
63+
64+
protected String[] shuffleFieldsExceptions() {
65+
return Strings.EMPTY_ARRAY;
66+
}
67+
5868
public void testFromXContent() throws IOException {
5969
xContentTester(
6070
this::createParser,
6171
this::createTestInstance,
6272
this::toXContent,
6373
this::fromXContent)
64-
.supportsUnknownFields(false)
65-
.randomFieldsExcludeFilter(field ->
66-
field.endsWith("job_id"))
74+
.supportsUnknownFields(true)
75+
.randomFieldsExcludeFilter(randomFieldsExcludeFilter())
76+
.shuffleFieldsExceptions(shuffleFieldsExceptions())
6777
.test();
6878
}
6979

0 commit comments

Comments
 (0)