Fix GET bucket params validation (elastic/elasticsearch#661)
Original commit: elastic/x-pack-elasticsearch@66f522588b
This commit is contained in:
parent
45ef535b38
commit
fd4d412433
|
@ -51,47 +51,41 @@ public class RestGetBucketsAction extends BaseRestHandler {
|
|||
request = GetBucketsAction.Request.parseRequest(jobId, parser, () -> parseFieldMatcher);
|
||||
} else {
|
||||
request = new GetBucketsAction.Request(jobId);
|
||||
String timestamp = restRequest.param(GetBucketsAction.Request.TIMESTAMP.getPreferredName());
|
||||
|
||||
// Single bucket
|
||||
if (timestamp != null && !timestamp.isEmpty()) {
|
||||
request.setTimestamp(timestamp);
|
||||
// Check if the REST param is set first so mutually exclusive
|
||||
// options will only cause an error if set
|
||||
if (restRequest.hasParam(GetBucketsAction.Request.TIMESTAMP.getPreferredName())) {
|
||||
String timestamp = restRequest.param(GetBucketsAction.Request.TIMESTAMP.getPreferredName());
|
||||
if (timestamp != null && !timestamp.isEmpty()) {
|
||||
request.setTimestamp(timestamp);
|
||||
}
|
||||
}
|
||||
if (restRequest.hasParam(PageParams.FROM.getPreferredName())
|
||||
|| restRequest.hasParam(PageParams.SIZE.getPreferredName())
|
||||
|| restRequest.hasParam(GetBucketsAction.Request.START.getPreferredName())
|
||||
|| restRequest.hasParam(GetBucketsAction.Request.END.getPreferredName())
|
||||
|| restRequest.hasParam(GetBucketsAction.Request.ANOMALY_SCORE.getPreferredName())
|
||||
|| restRequest.hasParam(GetBucketsAction.Request.MAX_NORMALIZED_PROBABILITY.getPreferredName())
|
||||
|| timestamp == null) {
|
||||
|
||||
// multiple bucket options
|
||||
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)));
|
||||
|
||||
// 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(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()));
|
||||
}
|
||||
}
|
||||
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(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
|
||||
// single and multiple bucket options
|
||||
request.setExpand(restRequest.paramAsBoolean(GetBucketsAction.Request.EXPAND.getPreferredName(), false));
|
||||
request.setIncludeInterim(restRequest.paramAsBoolean(GetBucketsAction.Request.INCLUDE_INTERIM.getPreferredName(), false));
|
||||
}
|
||||
|
|
|
@ -99,6 +99,13 @@ setup:
|
|||
timestamp: "2016-06-01T00:00:00Z"
|
||||
end: "2016-05-01T00:00:00Z"
|
||||
|
||||
- do:
|
||||
catch: request
|
||||
xpack.ml.get_buckets:
|
||||
job_id: "farequote"
|
||||
timestamp: "2016-06-01T00:00:00Z"
|
||||
anomaly_score: "80.0"
|
||||
|
||||
---
|
||||
"Test mutually-exclusive params (via body)":
|
||||
- do:
|
||||
|
@ -132,3 +139,11 @@ setup:
|
|||
body:
|
||||
timestamp: "2016-06-01T00:00:00Z"
|
||||
end: "2016-05-01T00:00:00Z"
|
||||
|
||||
- do:
|
||||
catch: request
|
||||
xpack.ml.get_buckets:
|
||||
job_id: "farequote"
|
||||
body:
|
||||
timestamp: "2016-06-01T00:00:00Z"
|
||||
anomaly_score: "80.0"
|
||||
|
|
Loading…
Reference in New Issue