fix toXContent() for mapper attachments field

We must use simpleName() instead of name() because otherwise when the mapping
is generated as a string the field name will be the full path with dots
and that is illegal from es 2.0 on.

closes https://github.com/elastic/elasticsearch-mapper-attachments/issues/169
This commit is contained in:
Britta Weber 2015-11-30 14:58:00 +01:00
parent c4a2298194
commit d8a1a4bd43
2 changed files with 33 additions and 1 deletions

View File

@ -624,7 +624,7 @@ public class AttachmentMapper extends FieldMapper {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(name());
builder.startObject(simpleName());
builder.field("type", CONTENT_TYPE);
if (indexCreatedBefore2x) {
builder.field("path", pathType.name().toLowerCase(Locale.ROOT));

View File

@ -22,10 +22,14 @@ package org.elasticsearch.mapper.attachments;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.ParseContext;
import org.junit.Test;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.StreamsUtils.copyToBytesFromClasspath;
@ -107,4 +111,32 @@ public class SimpleAttachmentMapperTests extends AttachmentUnitTestCase {
assertThat(doc.get(docMapper.mappers().getMapper("file.content").fieldType().names().indexName()), containsString("This document tests the ability of Apache Tika to extract content"));
}
/**
* See issue https://github.com/elastic/elasticsearch-mapper-attachments/issues/169
* Mapping should not contain field names with dot.
*/
public void testMapperErrorWithDotTwoLevels169() throws Exception {
XContentBuilder mappingBuilder = jsonBuilder();
mappingBuilder.startObject()
.startObject("mail")
.startObject("properties")
.startObject("attachments")
.startObject("properties")
.startObject("innerfield")
.field("type", "attachment")
.endObject()
.endObject()
.endObject()
.endObject()
.endObject();
byte[] mapping = mappingBuilder.bytes().toBytes();
MapperService mapperService = MapperTestUtils.newMapperService(createTempDir(), Settings.EMPTY);
DocumentMapper docMapper = mapperService.parse("mail", new CompressedXContent(mapping), true);
// this should not throw an exception
mapperService.parse("mail", new CompressedXContent(docMapper.mapping().toString()), true);
// the mapping may not contain a field name with a dot
assertFalse(docMapper.mapping().toString().contains("."));
}
}