Fix NPE in Rest Layer when GET missing watch.

The REST GET API was trying to render a	null watch on GET which	was causing an NPE. Don't render the watch if it's not found and add a test for this case.

Fixes: elastic/elasticsearch#202

Original commit: elastic/x-pack-elasticsearch@0c8afa63ba
This commit is contained in:
Brian Murphy 2015-05-07 16:46:08 -04:00
parent a90d5a581e
commit f5c50c44de
2 changed files with 19 additions and 3 deletions

View File

@ -0,0 +1,14 @@
---
"Test get watch api with missing watch":
- do:
cluster.health:
wait_for_status: green
- do:
watcher.get_watch:
id: "missing_watch"
ignore: 404
- match: { found : false}
- match: { _id: "missing_watch" }

View File

@ -38,9 +38,11 @@ public class RestGetWatchAction extends WatcherRestHandler {
builder.startObject()
.field("found", response.isFound())
.field("_id", response.getId())
.field("_version", response.getVersion())
.field("watch", response.getSource(), ToXContent.EMPTY_PARAMS)
.endObject();
.field("_version", response.getVersion());
if (response.isFound()) {
builder.field("watch", response.getSource(), ToXContent.EMPTY_PARAMS);
}
builder.endObject();
RestStatus status = response.isFound() ? OK : NOT_FOUND;
return new BytesRestResponse(status, builder);