Skip to content

Commit

Permalink
Mappings: Fix Get field mapping api with pretty flag
Browse files Browse the repository at this point in the history
Closes #6552
  • Loading branch information
johtani committed Dec 11, 2014
1 parent f7f7e52 commit 2a00f97
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Expand Up @@ -115,7 +115,11 @@ public boolean isNull() {
@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;
}
}
Expand Down
Expand Up @@ -28,7 +28,6 @@
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;
Expand Down Expand Up @@ -81,7 +80,7 @@ public RestResponse buildResponse(GetFieldMappingsResponse response, XContentBui
status = NOT_FOUND;
}
builder.startObject();
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(status, builder);
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -146,4 +147,38 @@ public void simpleGetFieldMappingsWithDefaults() throws Exception {


}

//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())));

}
}

0 comments on commit 2a00f97

Please sign in to comment.