Skip to content

Commit 326e839

Browse files
authored
[ML] fix empty body on post issue for datafeed _preview (#73205) (#73209)
It is common practice to pass an empty body on a `POST` call. Consequently, a `POST _ml/datafeed/<ID>/_preview` could fail if the body is empty as we try to parse it while IGNORING the ID in the URL. This commit fixes that bug. closes #73206
1 parent 6cce026 commit 326e839

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedAction.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.action.ActionResponse;
1414
import org.elasticsearch.action.ActionType;
1515
import org.elasticsearch.client.ElasticsearchClient;
16+
import org.elasticsearch.common.Nullable;
1617
import org.elasticsearch.common.ParseField;
1718
import org.elasticsearch.common.Strings;
1819
import org.elasticsearch.common.bytes.BytesReference;
@@ -56,8 +57,13 @@ public static class Request extends ActionRequest implements ToXContentObject {
5657
PARSER.declareObject(Builder::setJobBuilder, Job.STRICT_PARSER, JOB_CONFIG);
5758
}
5859

59-
public static Request fromXContent(XContentParser parser) {
60-
return PARSER.apply(parser, null).build();
60+
public static Request fromXContent(XContentParser parser, @Nullable String datafeedId) {
61+
Builder builder = PARSER.apply(parser, null);
62+
// We don't need to check for "inconsistent ids" as we don't parse an ID from the body
63+
if (datafeedId != null) {
64+
builder.setDatafeedId(datafeedId);
65+
}
66+
return builder.build();
6167
}
6268

6369
private final String datafeedId;

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPreviewDatafeedAction.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public String getName() {
4343
@Override
4444
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
4545
PreviewDatafeedAction.Request request = restRequest.hasContentOrSourceParam() ?
46-
PreviewDatafeedAction.Request.fromXContent(restRequest.contentOrSourceParamParser()) :
46+
PreviewDatafeedAction.Request.fromXContent(
47+
restRequest.contentOrSourceParamParser(),
48+
restRequest.param(DatafeedConfig.ID.getPreferredName(), null)
49+
) :
4750
new PreviewDatafeedAction.Request(restRequest.param(DatafeedConfig.ID.getPreferredName()));
4851
return channel -> client.execute(PreviewDatafeedAction.INSTANCE, request, new RestToXContentListener<>(channel));
4952
}

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/ml/preview_datafeed.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,25 @@ setup:
109109
- match: { 3.airline: foo }
110110
- match: { 3.responsetime: 42.0 }
111111

112+
- do:
113+
ml.preview_datafeed:
114+
datafeed_id: preview-datafeed-feed
115+
body: >
116+
{}
117+
- length: { $body: 4 }
118+
- match: { 0.time: 1487376000000 }
119+
- match: { 0.airline: foo }
120+
- match: { 0.responsetime: 1.0 }
121+
- match: { 1.time: 1487377800000 }
122+
- match: { 1.airline: foo }
123+
- match: { 1.responsetime: 1.0 }
124+
- match: { 2.time: 1487379600000 }
125+
- match: { 2.airline: bar }
126+
- match: { 2.responsetime: 42.0 }
127+
- match: { 3.time: 1487379660000 }
128+
- match: { 3.airline: foo }
129+
- match: { 3.responsetime: 42.0 }
130+
112131
- do:
113132
ml.preview_datafeed:
114133
body: >

0 commit comments

Comments
 (0)