Skip to content

Commit

Permalink
turn GetFieldMappingsResponse to ToXContentObject (#31544)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8b698f0)
  • Loading branch information
Vladimir Dolzhenko committed Jun 25, 2018
1 parent b72d511 commit 57089e2
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
Expand All @@ -47,7 +48,7 @@
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;

/** Response object for {@link GetFieldMappingsRequest} API */
public class GetFieldMappingsResponse extends ActionResponse implements ToXContentFragment {
public class GetFieldMappingsResponse extends ActionResponse implements ToXContentObject {

private static final ParseField MAPPINGS = new ParseField("mappings");

Expand Down Expand Up @@ -111,6 +112,7 @@ public FieldMappingMetaData fieldMappings(String index, String type, String fiel

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
for (Map.Entry<String, Map<String, Map<String, FieldMappingMetaData>>> indexEntry : mappings.entrySet()) {
builder.startObject(indexEntry.getKey());
builder.startObject(MAPPINGS.getPreferredName());
Expand All @@ -126,6 +128,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.endObject();
builder.endObject();
}
builder.endObject();
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ public RestResponse buildResponse(GetFieldMappingsResponse response, XContentBui
if (mappingsByIndex.isEmpty() && fields.length > 0) {
status = NOT_FOUND;
}
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(status, builder);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ public void testSimpleGetFieldMappingsWithPretty() throws Exception {
params.put("pretty", "true");
GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings("index").setTypes("type").setFields("field1", "obj.subfield").get();
XContentBuilder responseBuilder = XContentFactory.jsonBuilder().prettyPrint();
responseBuilder.startObject();
response.toXContent(responseBuilder, new ToXContent.MapParams(params));
responseBuilder.endObject();
String responseStrings = Strings.toString(responseBuilder);


Expand All @@ -236,9 +234,7 @@ public void testSimpleGetFieldMappingsWithPretty() throws Exception {

response = client().admin().indices().prepareGetFieldMappings("index").setTypes("type").setFields("field1", "obj.subfield").get();
responseBuilder = XContentFactory.jsonBuilder().prettyPrint().lfAtEnd();
responseBuilder.startObject();
response.toXContent(responseBuilder, new ToXContent.MapParams(params));
responseBuilder.endObject();
responseStrings = Strings.toString(responseBuilder);

prettyJsonBuilder = XContentFactory.jsonBuilder().prettyPrint();
Expand Down

0 comments on commit 57089e2

Please sign in to comment.