Skip to content

Commit

Permalink
Fix anaylze NullPointerException when AnalyzeTokenList tokens is null
Browse files Browse the repository at this point in the history
  • Loading branch information
weizijun committed Feb 20, 2019
1 parent ae7b946 commit 0a46e99
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,10 @@ public static AnalyzeTokenList readAnalyzeTokenList(StreamInput in) throws IOExc
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +35,8 @@
import java.util.Map;
import java.util.function.Predicate;

import static org.hamcrest.Matchers.equalTo;

public class AnalyzeResponseTests extends AbstractStreamableXContentTestCase<AnalyzeResponse> {

@Override
Expand Down Expand Up @@ -112,4 +119,31 @@ private AnalyzeResponse.AnalyzeToken randomToken() {
}
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));
}

}
}

0 comments on commit 0a46e99

Please sign in to comment.