Skip to content

Commit 15eeacf

Browse files
committed
Update Put Watch to allow unknown fields (#37494)
PutWatchResponse did not allow unknown fields. This commit fixes the test and ConstructingObjectParser such that it does now allow unknown fields. Relates #36938
1 parent c349636 commit 15eeacf

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/PutWatchResponse.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 PutWatchResponse implements ToXContentObject {
28+
public class PutWatchResponse {
3129

3230
private static final ObjectParser<PutWatchResponse, Void> PARSER
33-
= new ObjectParser<>("x_pack_put_watch_response", PutWatchResponse::new);
31+
= new ObjectParser<>("x_pack_put_watch_response", true, PutWatchResponse::new);
3432

3533
static {
3634
PARSER.declareString(PutWatchResponse::setId, new ParseField("_id"));
@@ -90,15 +88,6 @@ public int hashCode() {
9088
return Objects.hash(id, version, created);
9189
}
9290

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

client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/PutWatchResponseTests.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 PutWatchResponseTests extends AbstractXContentTestCase<PutWatchResponse> {
26+
import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
2727

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

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

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

0 commit comments

Comments
 (0)