From 80bd69811d45383f9305550855b0024bc74dca0e Mon Sep 17 00:00:00 2001 From: Jun Ohtani Date: Sun, 7 Dec 2014 23:33:33 +0900 Subject: [PATCH] Mappings: Fix Get field mapping api with pretty flag Closes #6552 --- .../mapping/get/GetFieldMappingsResponse.java | 6 ++- .../get/RestGetFieldMappingAction.java | 3 +- .../mapping/SimpleGetFieldMappingsTests.java | 37 ++++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java b/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java index f59feec155c..b92e60de712 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetFieldMappingsResponse.java @@ -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; } } diff --git a/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/get/RestGetFieldMappingAction.java b/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/get/RestGetFieldMappingAction.java index 8234153d4e7..eb53129b5d0 100644 --- a/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/get/RestGetFieldMappingAction.java +++ b/src/main/java/org/elasticsearch/rest/action/admin/indices/mapping/get/RestGetFieldMappingAction.java @@ -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); } diff --git a/src/test/java/org/elasticsearch/indices/mapping/SimpleGetFieldMappingsTests.java b/src/test/java/org/elasticsearch/indices/mapping/SimpleGetFieldMappingsTests.java index da81d8438ec..70c451526f4 100644 --- a/src/test/java/org/elasticsearch/indices/mapping/SimpleGetFieldMappingsTests.java +++ b/src/test/java/org/elasticsearch/indices/mapping/SimpleGetFieldMappingsTests.java @@ -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 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()))); + + } }