From f8309a0706d86020a6af38c7f155760c09c2bee1 Mon Sep 17 00:00:00 2001 From: David Kyle Date: Tue, 3 Jan 2017 14:59:01 +0000 Subject: [PATCH] Fix validating params for single bucket requests (elastic/elasticsearch#622) Original commit: elastic/x-pack-elasticsearch@42f8771bf862a889d0c8ac2dd5838f2893fe2642 --- .../rest/results/RestGetBucketsAction.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/results/RestGetBucketsAction.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/results/RestGetBucketsAction.java index 5cb936c135f..326a1bed8f3 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/results/RestGetBucketsAction.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/prelert/rest/results/RestGetBucketsAction.java @@ -65,17 +65,31 @@ public class RestGetBucketsAction extends BaseRestHandler { || restRequest.hasParam(GetBucketsAction.Request.MAX_NORMALIZED_PROBABILITY.getPreferredName()) || timestamp == null) { - // Multiple buckets - request.setStart(restRequest.param(GetBucketsAction.Request.START.getPreferredName())); - request.setEnd(restRequest.param(GetBucketsAction.Request.END.getPreferredName())); - request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), - restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); - request.setAnomalyScore( - Double.parseDouble(restRequest.param(GetBucketsAction.Request.ANOMALY_SCORE.getPreferredName(), "0.0"))); - request.setMaxNormalizedProbability( - Double.parseDouble(restRequest.param( - GetBucketsAction.Request.MAX_NORMALIZED_PROBABILITY.getPreferredName(), "0.0"))); - request.setPartitionValue(restRequest.param(GetBucketsAction.Request.PARTITION_VALUE.getPreferredName())); + // Multiple buckets, check if the param is set first so mutually exclusive + // options will only cause an error if set + if (restRequest.hasParam(GetBucketsAction.Request.START.getPreferredName())) { + request.setStart(restRequest.param(GetBucketsAction.Request.START.getPreferredName())); + } + if (restRequest.hasParam(GetBucketsAction.Request.END.getPreferredName())) { + request.setEnd(restRequest.param(GetBucketsAction.Request.END.getPreferredName())); + } + if (restRequest.hasParam(PageParams.FROM.getPreferredName()) || restRequest.hasParam(PageParams.SIZE.getPreferredName())) { + request.setPageParams( + new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM), + restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE))); + } + if (restRequest.hasParam(GetBucketsAction.Request.ANOMALY_SCORE.getPreferredName())) { + request.setAnomalyScore( + Double.parseDouble(restRequest.param(GetBucketsAction.Request.ANOMALY_SCORE.getPreferredName(), "0.0"))); + } + if (restRequest.hasParam(GetBucketsAction.Request.MAX_NORMALIZED_PROBABILITY.getPreferredName())) { + request.setMaxNormalizedProbability( + Double.parseDouble(restRequest.param( + GetBucketsAction.Request.MAX_NORMALIZED_PROBABILITY.getPreferredName(), "0.0"))); + } + if (restRequest.hasParam(GetBucketsAction.Request.PARTITION_VALUE.getPreferredName())) { + request.setPartitionValue(restRequest.param(GetBucketsAction.Request.PARTITION_VALUE.getPreferredName())); + } } // Common options @@ -85,4 +99,5 @@ public class RestGetBucketsAction extends BaseRestHandler { return channel -> transportAction.execute(request, new RestStatusToXContentListener<>(channel)); } + }