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
This commit is contained in:
Michael Basnight 2019-01-14 19:50:20 -06:00 committed by GitHub
parent 397f315f56
commit cd78565acf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 28 deletions

View File

@ -20,17 +20,15 @@ package org.elasticsearch.client.watcher;
import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
public class DeleteWatchResponse implements ToXContentObject { public class DeleteWatchResponse {
private static final ObjectParser<DeleteWatchResponse, Void> PARSER private static final ObjectParser<DeleteWatchResponse, Void> PARSER
= new ObjectParser<>("x_pack_delete_watch_response", DeleteWatchResponse::new); = new ObjectParser<>("x_pack_delete_watch_response", true, DeleteWatchResponse::new);
static { static {
PARSER.declareString(DeleteWatchResponse::setId, new ParseField("_id")); PARSER.declareString(DeleteWatchResponse::setId, new ParseField("_id"));
PARSER.declareLong(DeleteWatchResponse::setVersion, new ParseField("_version")); PARSER.declareLong(DeleteWatchResponse::setVersion, new ParseField("_version"));
@ -89,15 +87,6 @@ public class DeleteWatchResponse implements ToXContentObject {
return Objects.hash(id, version, found); return Objects.hash(id, version, found);
} }
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject()
.field("_id", id)
.field("_version", version)
.field("found", found)
.endObject();
}
public static DeleteWatchResponse fromXContent(XContentParser parser) throws IOException { public static DeleteWatchResponse fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null); return PARSER.parse(parser, null);
} }

View File

@ -18,28 +18,37 @@
*/ */
package org.elasticsearch.client.watcher; package org.elasticsearch.client.watcher;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.test.ESTestCase;
import java.io.IOException; import java.io.IOException;
public class DeleteWatchResponseTests extends AbstractXContentTestCase<DeleteWatchResponse> { import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
@Override public class DeleteWatchResponseTests extends ESTestCase {
protected DeleteWatchResponse createTestInstance() {
public void testFromXContent() throws IOException {
xContentTester(this::createParser,
DeleteWatchResponseTests::createTestInstance,
DeleteWatchResponseTests::toXContent,
DeleteWatchResponse::fromXContent)
.supportsUnknownFields(true)
.assertToXContentEquivalence(false)
.test();
}
private static XContentBuilder toXContent(DeleteWatchResponse response, XContentBuilder builder) throws IOException {
return builder.startObject()
.field("_id", response.getId())
.field("_version", response.getVersion())
.field("found", response.isFound())
.endObject();
}
private static DeleteWatchResponse createTestInstance() {
String id = randomAlphaOfLength(10); String id = randomAlphaOfLength(10);
long version = randomLongBetween(1, 10); long version = randomLongBetween(1, 10);
boolean found = randomBoolean(); boolean found = randomBoolean();
return new DeleteWatchResponse(id, version, found); return new DeleteWatchResponse(id, version, found);
} }
@Override
protected DeleteWatchResponse doParseInstance(XContentParser parser) throws IOException {
return DeleteWatchResponse.fromXContent(parser);
}
@Override
protected boolean supportsUnknownFields() {
return false;
}
} }