Skip to content

Commit eba4da7

Browse files
committed
NPE if "content" is missing in mapper-attachment plugin
Curl recreation: curl -X DELETE "localhost:9200/test" curl -X PUT "localhost:9200/test" -d '{ "settings" : { "index" : { "number_of_shards" : 1, "number_of_replicas" : 0 }} }' curl -X GET "localhost:9200/_cluster/health?wait_for_status=green&pretty=1&timeout=5s" curl -X PUT "localhost:9200/test/attachment/_mapping" -d '{ "attachment" : { "properties" : { "file" : { "type" : "attachment" } } } }' curl -X PUT "localhost:9200/test/attachment/1" -d '{ "file" : { "_content_type" : "application/pdf", "_name" : "resource/name/of/my.pdf" } } ' Produces a: {"error":"NullPointerException[null]","status":500} And in ES logs: [2013-02-20 12:49:04,445][DEBUG][action.index ] [Drake, Frank] [test][0], node[LI6crwNKQmu1ue1u7mlqGA], [P], s[STARTED]: Failed to execute [index {[test][attachment][1], source[{ "file" : { "_content_type" : "application/pdf", "_name" : "resource/name/of/my.pdf" } } ]}] java.lang.NullPointerException at org.elasticsearch.common.io.FastByteArrayInputStream.<init>(FastByteArrayInputStream.java:90) at org.elasticsearch.index.mapper.attachment.AttachmentMapper.parse(AttachmentMapper.java:309) at org.elasticsearch.index.mapper.object.ObjectMapper.serializeObject(ObjectMapper.java:507) at org.elasticsearch.index.mapper.object.ObjectMapper.parse(ObjectMapper.java:449) at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:486) at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:430) at org.elasticsearch.index.shard.service.InternalIndexShard.prepareIndex(InternalIndexShard.java:318) at org.elasticsearch.action.index.TransportIndexAction.shardOperationOnPrimary(TransportIndexAction.java:203) at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:531) at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:429) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:680) Closes elastic#23
1 parent 69f8bde commit eba4da7

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java

+5
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ public void parse(ParseContext context) throws IOException {
295295
}
296296
}
297297

298+
// Throw clean exception when no content is provided Fix #23
299+
if (content == null) {
300+
throw new MapperParsingException("No content is provided.");
301+
}
302+
298303
Metadata metadata = new Metadata();
299304
if (contentType != null) {
300305
metadata.add(Metadata.CONTENT_TYPE, contentType);

src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.common.logging.ESLogger;
2626
import org.elasticsearch.common.logging.Loggers;
2727
import org.elasticsearch.common.network.NetworkUtils;
28+
import org.elasticsearch.index.mapper.MapperParsingException;
2829
import org.elasticsearch.node.Node;
2930
import org.testng.annotations.*;
3031

@@ -133,4 +134,19 @@ public void testSimpleAttachmentNoContentLengthLimit() throws Exception {
133134
countResponse = node.client().count(countRequest("test").query(fieldQuery("file", "End"))).actionGet();
134135
assertThat(countResponse.count(), equalTo(1l));
135136
}
136-
}
137+
138+
/**
139+
* Test case for issue https://github.com/elasticsearch/elasticsearch-mapper-attachments/issues/23
140+
* <br/>We throw a nicer exception when no content is provided
141+
* @throws Exception
142+
*/
143+
@Test(expectedExceptions = MapperParsingException.class)
144+
public void testNoContent() throws Exception {
145+
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/test-mapping.json");
146+
147+
node.client().admin().indices().putMapping(putMappingRequest("test").type("person").source(mapping)).actionGet();
148+
149+
node.client().index(indexRequest("test").type("person")
150+
.source(jsonBuilder().startObject().field("file").startObject().endObject())).actionGet();
151+
}
152+
}

0 commit comments

Comments
 (0)