Skip to content

Commit cd78565

Browse files
authored
Update Delete Watch to allow unknown fields (#37435)
DeleteWatchResponse did not allow unknown fields. This commit fixes the test and ConstructingObjectParser such that it does now allow unknown fields. Relates #36938
1 parent 397f315 commit cd78565

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeleteWatchResponse.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@
2020

2121
import org.elasticsearch.common.ParseField;
2222
import org.elasticsearch.common.xcontent.ObjectParser;
23-
import org.elasticsearch.common.xcontent.ToXContentObject;
24-
import org.elasticsearch.common.xcontent.XContentBuilder;
2523
import org.elasticsearch.common.xcontent.XContentParser;
2624

2725
import java.io.IOException;
2826
import java.util.Objects;
2927

30-
public class DeleteWatchResponse implements ToXContentObject {
28+
public class DeleteWatchResponse {
3129

3230
private static final ObjectParser<DeleteWatchResponse, Void> PARSER
33-
= new ObjectParser<>("x_pack_delete_watch_response", DeleteWatchResponse::new);
31+
= new ObjectParser<>("x_pack_delete_watch_response", true, DeleteWatchResponse::new);
3432
static {
3533
PARSER.declareString(DeleteWatchResponse::setId, new ParseField("_id"));
3634
PARSER.declareLong(DeleteWatchResponse::setVersion, new ParseField("_version"));
@@ -89,15 +87,6 @@ public int hashCode() {
8987
return Objects.hash(id, version, found);
9088
}
9189

92-
@Override
93-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
94-
return builder.startObject()
95-
.field("_id", id)
96-
.field("_version", version)
97-
.field("found", found)
98-
.endObject();
99-
}
100-
10190
public static DeleteWatchResponse fromXContent(XContentParser parser) throws IOException {
10291
return PARSER.parse(parser, null);
10392
}

client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeleteWatchResponseTests.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,37 @@
1818
*/
1919
package org.elasticsearch.client.watcher;
2020

21-
import org.elasticsearch.common.xcontent.XContentParser;
22-
import org.elasticsearch.test.AbstractXContentTestCase;
21+
import org.elasticsearch.common.xcontent.XContentBuilder;
22+
import org.elasticsearch.test.ESTestCase;
2323

2424
import java.io.IOException;
2525

26-
public class DeleteWatchResponseTests extends AbstractXContentTestCase<DeleteWatchResponse> {
26+
import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
2727

28-
@Override
29-
protected DeleteWatchResponse createTestInstance() {
30-
String id = randomAlphaOfLength(10);
31-
long version = randomLongBetween(1, 10);
32-
boolean found = randomBoolean();
33-
return new DeleteWatchResponse(id, version, found);
28+
public class DeleteWatchResponseTests extends ESTestCase {
29+
30+
public void testFromXContent() throws IOException {
31+
xContentTester(this::createParser,
32+
DeleteWatchResponseTests::createTestInstance,
33+
DeleteWatchResponseTests::toXContent,
34+
DeleteWatchResponse::fromXContent)
35+
.supportsUnknownFields(true)
36+
.assertToXContentEquivalence(false)
37+
.test();
3438
}
3539

36-
@Override
37-
protected DeleteWatchResponse doParseInstance(XContentParser parser) throws IOException {
38-
return DeleteWatchResponse.fromXContent(parser);
40+
private static XContentBuilder toXContent(DeleteWatchResponse response, XContentBuilder builder) throws IOException {
41+
return builder.startObject()
42+
.field("_id", response.getId())
43+
.field("_version", response.getVersion())
44+
.field("found", response.isFound())
45+
.endObject();
3946
}
4047

41-
@Override
42-
protected boolean supportsUnknownFields() {
43-
return false;
48+
private static DeleteWatchResponse createTestInstance() {
49+
String id = randomAlphaOfLength(10);
50+
long version = randomLongBetween(1, 10);
51+
boolean found = randomBoolean();
52+
return new DeleteWatchResponse(id, version, found);
4453
}
4554
}

0 commit comments

Comments
 (0)