Skip to content

Commit

Permalink
Convert FieldCapabilitiesResponse to a ToXContentObject. (#30182)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtibshirani authored Apr 27, 2018
1 parent dd66659 commit d633130
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParserUtils;
Expand All @@ -41,7 +41,7 @@
/**
* Response for {@link FieldCapabilitiesRequest} requests.
*/
public class FieldCapabilitiesResponse extends ActionResponse implements ToXContentFragment {
public class FieldCapabilitiesResponse extends ActionResponse implements ToXContentObject {
private static final ParseField FIELDS_FIELD = new ParseField("fields");

private Map<String, Map<String, FieldCapabilities>> responseMap;
Expand Down Expand Up @@ -123,8 +123,9 @@ private static void writeField(StreamOutput out,

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(FIELDS_FIELD.getPreferredName(), responseMap);
return builder;
return builder.startObject()
.field(FIELDS_FIELD.getPreferredName(), responseMap)
.endObject();
}

public static FieldCapabilitiesResponse fromXContent(XContentParser parser) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ public RestChannelConsumer prepareRequest(final RestRequest request,
fieldRequest.indicesOptions(
IndicesOptions.fromRequest(request, fieldRequest.indicesOptions())
);
return channel -> client.fieldCaps(fieldRequest,
new RestBuilderListener<FieldCapabilitiesResponse>(channel) {
@Override
public RestResponse buildResponse(FieldCapabilitiesResponse response,
XContentBuilder builder) throws Exception {
RestStatus status = OK;
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(status, builder);
}
});
return channel -> client.fieldCaps(fieldRequest, new RestToXContentListener<>(channel));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,8 @@ protected Predicate<String> getRandomFieldsExcludeFilter() {
public void testToXContent() throws IOException {
FieldCapabilitiesResponse response = createSimpleResponse();

XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)
.startObject();
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();

String generatedResponse = BytesReference.bytes(builder).utf8ToString();
assertEquals((
Expand Down

0 comments on commit d633130

Please sign in to comment.