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 12006ea commit ac34b47
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
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
Expand Up @@ -19,17 +19,20 @@

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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Predicate;

import static org.hamcrest.Matchers.equalTo;

public class AnalyzeResponseTests extends AbstractStreamableXContentTestCase<AnalyzeResponse> {

@Override
Expand Down Expand Up @@ -112,4 +115,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 ac34b47

Please sign in to comment.