[7.x][ML] Fix node serialization on GET df-nalytics stats without id (#54808) (#54812)

Previously, the id of the `GetDataFrameAnalyticsStatsAction.Request`
could be `null` which caused NPE on serialization as `writeString`
is used (it doesn't accept null values).

This commit ensures the id is never null.

Closes #54807

Backport of #54808
This commit is contained in:
Dimitris Athanasiou 2020-04-06 18:13:16 +03:00 committed by GitHub
parent 2fdbed7797
commit 0049e9467b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View File

@ -54,7 +54,7 @@ public class GetDataFrameAnalyticsStatsAction extends ActionType<GetDataFrameAna
public static final ParseField ALLOW_NO_MATCH = new ParseField("allow_no_match"); public static final ParseField ALLOW_NO_MATCH = new ParseField("allow_no_match");
private String id; private String id = "_all";
private boolean allowNoMatch = true; private boolean allowNoMatch = true;
private PageParams pageParams = PageParams.defaultParams(); private PageParams pageParams = PageParams.defaultParams();
@ -94,7 +94,7 @@ public class GetDataFrameAnalyticsStatsAction extends ActionType<GetDataFrameAna
} }
public void setId(String id) { public void setId(String id) {
this.id = id; this.id = ExceptionsHelper.requireNonNull(id, DataFrameAnalyticsConfig.ID.getPreferredName());
} }
public String getId() { public String getId() {

View File

@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.core.ml.action;
import org.elasticsearch.test.ESTestCase;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
public class GetDataFrameAnalyticsStatsActionRequestTests extends ESTestCase {
public void testRequest_GivenNoId() {
GetDataFrameAnalyticsStatsAction.Request request = new GetDataFrameAnalyticsStatsAction.Request();
assertThat(request.getId(), notNullValue());
assertThat(request.getId(), equalTo("_all"));
}
public void testSetId_GivenNull() {
GetDataFrameAnalyticsStatsAction.Request request = new GetDataFrameAnalyticsStatsAction.Request();
expectThrows(IllegalArgumentException.class, () -> request.setId(null));
}
public void testSetId_GivenString() {
GetDataFrameAnalyticsStatsAction.Request request = new GetDataFrameAnalyticsStatsAction.Request();
request.setId("foo");
assertThat(request.getId(), equalTo("foo"));
}
}