Mappings: Fix Get field mapping api with pretty flag

Closes #6552
This commit is contained in:
Jun Ohtani 2014-12-07 23:33:33 +09:00
parent 084d25cdbd
commit 80bd69811d
3 changed files with 42 additions and 4 deletions

View File

@ -115,7 +115,11 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("full_name", fullName);
XContentHelper.writeRawField("mapping", source, builder, params);
if (params.paramAsBoolean("pretty", false)) {
builder.field("mapping", sourceAsMap());
} else {
builder.rawField("mapping", source);
}
return builder;
}
}

View File

@ -28,7 +28,6 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestBuilderListener;
@ -81,7 +80,7 @@ public class RestGetFieldMappingAction extends BaseRestHandler {
status = NOT_FOUND;
}
builder.startObject();
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(status, builder);
}

View File

@ -19,8 +19,9 @@
package org.elasticsearch.indices.mapping;
import com.google.common.collect.Maps;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.*;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.hamcrest.Matchers;
import org.junit.Test;
@ -146,4 +147,38 @@ public class SimpleGetFieldMappingsTests extends ElasticsearchIntegrationTest {
}
//fix #6552
@Test
public void simpleGetFieldMappingsWithPretty() throws Exception {
assertAcked(prepareCreate("index").addMapping("type", getMappingForType("type")));
Map<String, String> params = Maps.newHashMap();
params.put("pretty", "true");
ensureYellow();
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 = responseBuilder.string();
XContentBuilder prettyJsonBuilder = XContentFactory.jsonBuilder().prettyPrint();
prettyJsonBuilder.copyCurrentStructure(XContentFactory.xContent(responseStrings).createParser(responseStrings));
assertThat(responseStrings, equalTo(prettyJsonBuilder.string()));
params.put("pretty", "false");
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 = responseBuilder.string();
prettyJsonBuilder = XContentFactory.jsonBuilder().prettyPrint();
prettyJsonBuilder.copyCurrentStructure(XContentFactory.xContent(responseStrings).createParser(responseStrings));
assertThat(responseStrings, not(equalTo(prettyJsonBuilder.string())));
}
}