Fix anaylze NullPointerException when AnalyzeTokenList tokens is null (#39332) (#39361)

This commit is contained in:
Mayya Sharipova 2019-02-25 12:49:18 -05:00 committed by GitHub
parent 3d49523726
commit bf058d6e4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 2 deletions

View File

@ -296,8 +296,10 @@ public class DetailAnalyzeResponse implements Streamable, ToXContentFragment {
XContentBuilder toXContentWithoutObject(XContentBuilder builder, Params params) throws IOException {
builder.field(Fields.NAME, this.name);
builder.startArray(AnalyzeResponse.Fields.TOKENS);
for (AnalyzeResponse.AnalyzeToken token : tokens) {
token.toXContent(builder, params);
if (tokens != null) {
for (AnalyzeResponse.AnalyzeToken token : tokens) {
token.toXContent(builder, params);
}
}
builder.endArray();
return builder;

View File

@ -19,7 +19,12 @@
package org.elasticsearch.action.admin.indices.analyze;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
import java.io.IOException;
@ -30,6 +35,8 @@ import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import static org.hamcrest.Matchers.equalTo;
public class AnalyzeResponseTests extends AbstractStreamableXContentTestCase<AnalyzeResponse> {
@Override
@ -112,4 +119,31 @@ public class AnalyzeResponseTests extends AbstractStreamableXContentTestCase<Ana
}
return new AnalyzeResponse.AnalyzeToken(token, position, startOffset, endOffset, posLength, type, extras);
}
public void testNullResponseToXContent() throws IOException {
DetailAnalyzeResponse.CharFilteredText[] charfilters = null;
String name = "test_tokens_null";
AnalyzeResponse.AnalyzeToken[] tokens = null;
DetailAnalyzeResponse.AnalyzeTokenList tokenizer = null;
DetailAnalyzeResponse.AnalyzeTokenList tokenfiltersItem = new DetailAnalyzeResponse.AnalyzeTokenList(name, tokens);
DetailAnalyzeResponse.AnalyzeTokenList[] tokenfilters = {tokenfiltersItem};
DetailAnalyzeResponse detail = new DetailAnalyzeResponse(charfilters, tokenizer, tokenfilters);
AnalyzeResponse response = new AnalyzeResponse(null, detail);
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
Map<String, Object> converted = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
List<Map<String, Object>> tokenfiltersValue = (List<Map<String, Object>>) ((Map<String, Object>)
converted.get("detail")).get("tokenfilters");
List<Map<String, Object>> nullTokens = (List<Map<String, Object>>) tokenfiltersValue.get(0).get("tokens");
String nameValue = (String) tokenfiltersValue.get(0).get("name");
assertThat(nullTokens.size(), equalTo(0));
assertThat(name, equalTo(nameValue));
}
}
}