Skip to content

Commit b0c5660

Browse files
authored
Update verify repository to allow unknown fields (#38124)
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 Relates #37619
1 parent fcd68cb commit b0c5660

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
@@ -52,7 +52,7 @@ public class VerifyRepositoryResponse extends ActionResponse implements ToXConte
5252
public static class NodeView implements Writeable, ToXContentObject {
5353
private static final ObjectParser.NamedObjectParser<NodeView, Void> PARSER;
5454
static {
55-
ObjectParser<NodeView, Void> internalParser = new ObjectParser<>(NODES);
55+
ObjectParser<NodeView, Void> internalParser = new ObjectParser<>(NODES, true, null);
5656
internalParser.declareString(NodeView::setName, new ParseField(NAME));
5757
PARSER = (p, v, name) -> internalParser.parse(p, new NodeView(name), null);
5858
}
@@ -131,7 +131,7 @@ public int hashCode() {
131131

132132

133133
private static final ObjectParser<VerifyRepositoryResponse, Void> PARSER =
134-
new ObjectParser<>(VerifyRepositoryResponse.class.getName(), VerifyRepositoryResponse::new);
134+
new ObjectParser<>(VerifyRepositoryResponse.class.getName(), true, VerifyRepositoryResponse::new);
135135
static {
136136
PARSER.declareNamedObjects(VerifyRepositoryResponse::setNodes, NodeView.PARSER, new ParseField("nodes"));
137137
}
@@ -144,6 +144,10 @@ public VerifyRepositoryResponse(ClusterName clusterName, DiscoveryNode[] nodes)
144144
this.nodes = Arrays.asList(nodes);
145145
}
146146

147+
public VerifyRepositoryResponse(List<NodeView> nodes) {
148+
setNodes(nodes);
149+
}
150+
147151
@Override
148152
public void readFrom(StreamInput in) throws IOException {
149153
super.readFrom(in);
@@ -208,19 +212,15 @@ public String toString() {
208212
}
209213

210214
@Override
211-
public boolean equals(Object obj) {
212-
if (obj == null) {
213-
return false;
214-
}
215-
if (getClass() != obj.getClass()) {
216-
return false;
217-
}
218-
VerifyRepositoryResponse other = (VerifyRepositoryResponse) obj;
219-
return nodes.equals(other.nodes);
215+
public boolean equals(Object o) {
216+
if (this == o) return true;
217+
if (o == null || getClass() != o.getClass()) return false;
218+
VerifyRepositoryResponse that = (VerifyRepositoryResponse) o;
219+
return Objects.equals(nodes, that.nodes);
220220
}
221221

222222
@Override
223223
public int hashCode() {
224-
return nodes.hashCode();
224+
return Objects.hash(nodes);
225225
}
226226
}

0 commit comments

Comments
 (0)