From eba4da7086dc8bb7cbc3fb1c074f366e8d268ffa Mon Sep 17 00:00:00 2001 From: David Pilato Date: Fri, 22 Feb 2013 09:17:03 +0100 Subject: [PATCH] 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.(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 #23 --- .../mapper/attachment/AttachmentMapper.java | 5 +++++ .../test/SimpleAttachmentIntegrationTests.java | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java b/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java index 16b3bcf01e6..8d9a7cac468 100644 --- a/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java @@ -295,6 +295,11 @@ public class AttachmentMapper implements Mapper { } } + // Throw clean exception when no content is provided Fix #23 + if (content == null) { + throw new MapperParsingException("No content is provided."); + } + Metadata metadata = new Metadata(); if (contentType != null) { metadata.add(Metadata.CONTENT_TYPE, contentType); diff --git a/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java b/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java index 66cf76c21db..5fa39c59e49 100644 --- a/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java +++ b/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java @@ -25,6 +25,7 @@ import org.elasticsearch.action.count.CountResponse; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.network.NetworkUtils; +import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.node.Node; import org.testng.annotations.*; @@ -133,4 +134,19 @@ public class SimpleAttachmentIntegrationTests { countResponse = node.client().count(countRequest("test").query(fieldQuery("file", "End"))).actionGet(); assertThat(countResponse.count(), equalTo(1l)); } -} \ No newline at end of file + + /** + * Test case for issue https://github.com/elasticsearch/elasticsearch-mapper-attachments/issues/23 + *
We throw a nicer exception when no content is provided + * @throws Exception + */ + @Test(expectedExceptions = MapperParsingException.class) + public void testNoContent() throws Exception { + String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/test-mapping.json"); + + node.client().admin().indices().putMapping(putMappingRequest("test").type("person").source(mapping)).actionGet(); + + node.client().index(indexRequest("test").type("person") + .source(jsonBuilder().startObject().field("file").startObject().endObject())).actionGet(); + } +}