Skip to content

Commit 945ad05

Browse files
authored
Update verify repository to allow unknown fields (#37619)
The subparser in verify repository allows for unknown fields. This commit sets the value to true for the parser and modifies the test such that it accurately tests it. Relates #36938
1 parent be78816 commit 945ad05

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.watcher;
21+
22+
import org.elasticsearch.action.admin.cluster.repositories.verify.VerifyRepositoryResponse;
23+
import org.elasticsearch.common.xcontent.ToXContent;
24+
import org.elasticsearch.common.xcontent.XContentBuilder;
25+
import org.elasticsearch.test.ESTestCase;
26+
27+
import java.io.IOException;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
31+
import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
32+
33+
public class VerifyRepositoryResponseTests extends ESTestCase {
34+
35+
public void testFromXContent() throws IOException {
36+
xContentTester(this::createParser,
37+
VerifyRepositoryResponseTests::createTestInstance,
38+
VerifyRepositoryResponseTests::toXContent,
39+
VerifyRepositoryResponse::fromXContent)
40+
.supportsUnknownFields(true)
41+
.shuffleFieldsExceptions(new String[] {"nodes"}) // do not mix up the order of nodes, it will cause the tests to fail
42+
.randomFieldsExcludeFilter((f) -> f.equals("nodes")) // everything in nodes needs to be a particular parseable object
43+
.assertToXContentEquivalence(false)
44+
.test();
45+
}
46+
47+
private static VerifyRepositoryResponse createTestInstance() {
48+
List<VerifyRepositoryResponse.NodeView> nodes = new ArrayList<>();
49+
for (int i = 0; i < randomIntBetween(0, 2); i++) {
50+
nodes.add(new VerifyRepositoryResponse.NodeView(randomAlphaOfLength(5), randomAlphaOfLength(5)));
51+
}
52+
53+
return new VerifyRepositoryResponse(nodes);
54+
}
55+
56+
private static XContentBuilder toXContent(VerifyRepositoryResponse response, XContentBuilder builder) throws IOException {
57+
return response.toXContent(builder, ToXContent.EMPTY_PARAMS);
58+
}
59+
}

server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/verify/VerifyRepositoryResponse.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class VerifyRepositoryResponse extends ActionResponse implements ToXConte
4848
public static class NodeView implements Writeable, ToXContentObject {
4949
private static final ObjectParser.NamedObjectParser<NodeView, Void> PARSER;
5050
static {
51-
ObjectParser<NodeView, Void> internalParser = new ObjectParser<>(NODES);
51+
ObjectParser<NodeView, Void> internalParser = new ObjectParser<>(NODES, true, null);
5252
internalParser.declareString(NodeView::setName, new ParseField(NAME));
5353
PARSER = (p, v, name) -> internalParser.parse(p, new NodeView(name), null);
5454
}
@@ -110,7 +110,7 @@ public int hashCode() {
110110
private List<NodeView> nodes;
111111

112112
private static final ObjectParser<VerifyRepositoryResponse, Void> PARSER =
113-
new ObjectParser<>(VerifyRepositoryResponse.class.getName(), VerifyRepositoryResponse::new);
113+
new ObjectParser<>(VerifyRepositoryResponse.class.getName(), true, VerifyRepositoryResponse::new);
114114
static {
115115
PARSER.declareNamedObjects(VerifyRepositoryResponse::setNodes, NodeView.PARSER, new ParseField("nodes"));
116116
}
@@ -122,6 +122,10 @@ public VerifyRepositoryResponse(DiscoveryNode[] nodes) {
122122
this.nodes = Arrays.stream(nodes).map(dn -> new NodeView(dn.getId(), dn.getName())).collect(Collectors.toList());
123123
}
124124

125+
public VerifyRepositoryResponse(List<NodeView> nodes) {
126+
this.nodes = nodes;
127+
}
128+
125129
@Override
126130
public void readFrom(StreamInput in) throws IOException {
127131
super.readFrom(in);
@@ -168,19 +172,15 @@ public String toString() {
168172
}
169173

170174
@Override
171-
public boolean equals(Object obj) {
172-
if (obj == null) {
173-
return false;
174-
}
175-
if (getClass() != obj.getClass()) {
176-
return false;
177-
}
178-
VerifyRepositoryResponse other = (VerifyRepositoryResponse) obj;
179-
return nodes.equals(other.nodes);
175+
public boolean equals(Object o) {
176+
if (this == o) return true;
177+
if (o == null || getClass() != o.getClass()) return false;
178+
VerifyRepositoryResponse that = (VerifyRepositoryResponse) o;
179+
return Objects.equals(nodes, that.nodes);
180180
}
181181

182182
@Override
183183
public int hashCode() {
184-
return nodes.hashCode();
184+
return Objects.hash(nodes);
185185
}
186186
}

0 commit comments

Comments
 (0)