turn GetFieldMappingsResponse to ToXContentObject (#31544)

This commit is contained in:
Vladimir Dolzhenko 2018-06-25 10:12:31 +02:00 committed by Colin Goodheart-Smithe
parent e6dc01d2b7
commit 6be94dbecd
No known key found for this signature in database
GPG Key ID: F975E7BDD739B3C7
3 changed files with 4 additions and 7 deletions

View File

@ -28,6 +28,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
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;
@ -47,7 +48,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
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");
@ -111,6 +112,7 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
@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());
@ -126,6 +128,7 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
builder.endObject();
builder.endObject();
}
builder.endObject();
return builder;
}

View File

@ -81,9 +81,7 @@ public class RestGetFieldMappingAction extends BaseRestHandler {
if (mappingsByIndex.isEmpty() && fields.length > 0) {
status = NOT_FOUND;
}
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(status, builder);
}
});

View File

@ -149,9 +149,7 @@ public class SimpleGetFieldMappingsIT extends ESIntegTestCase {
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);
@ -163,9 +161,7 @@ public class SimpleGetFieldMappingsIT extends ESIntegTestCase {
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();