Skip to content

Commit 663bffa

Browse files
weizijunmayya-sharipova
authored andcommitted
Fix anaylze NullPointerException when AnalyzeTokenList tokens is null (#39332)
1 parent 222dc3e commit 663bffa

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/analyze/DetailAnalyzeResponse.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,10 @@ public static AnalyzeTokenList readAnalyzeTokenList(StreamInput in) throws IOExc
296296
XContentBuilder toXContentWithoutObject(XContentBuilder builder, Params params) throws IOException {
297297
builder.field(Fields.NAME, this.name);
298298
builder.startArray(AnalyzeResponse.Fields.TOKENS);
299-
for (AnalyzeResponse.AnalyzeToken token : tokens) {
300-
token.toXContent(builder, params);
299+
if (tokens != null) {
300+
for (AnalyzeResponse.AnalyzeToken token : tokens) {
301+
token.toXContent(builder, params);
302+
}
301303
}
302304
builder.endArray();
303305
return builder;

server/src/test/java/org/elasticsearch/action/admin/indices/analyze/AnalyzeResponseTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919

2020
package org.elasticsearch.action.admin.indices.analyze;
2121

22+
import org.elasticsearch.common.bytes.BytesReference;
23+
import org.elasticsearch.common.xcontent.ToXContent;
24+
import org.elasticsearch.common.xcontent.XContentBuilder;
25+
import org.elasticsearch.common.xcontent.XContentHelper;
2226
import org.elasticsearch.common.xcontent.XContentParser;
27+
import org.elasticsearch.common.xcontent.json.JsonXContent;
2328
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
2429

2530
import java.io.IOException;
@@ -30,6 +35,8 @@
3035
import java.util.Map;
3136
import java.util.function.Predicate;
3237

38+
import static org.hamcrest.Matchers.equalTo;
39+
3340
public class AnalyzeResponseTests extends AbstractStreamableXContentTestCase<AnalyzeResponse> {
3441

3542
@Override
@@ -112,4 +119,31 @@ private AnalyzeResponse.AnalyzeToken randomToken() {
112119
}
113120
return new AnalyzeResponse.AnalyzeToken(token, position, startOffset, endOffset, posLength, type, extras);
114121
}
122+
123+
public void testNullResponseToXContent() throws IOException {
124+
DetailAnalyzeResponse.CharFilteredText[] charfilters = null;
125+
126+
String name = "test_tokens_null";
127+
AnalyzeResponse.AnalyzeToken[] tokens = null;
128+
DetailAnalyzeResponse.AnalyzeTokenList tokenizer = null;
129+
130+
131+
DetailAnalyzeResponse.AnalyzeTokenList tokenfiltersItem = new DetailAnalyzeResponse.AnalyzeTokenList(name, tokens);
132+
DetailAnalyzeResponse.AnalyzeTokenList[] tokenfilters = {tokenfiltersItem};
133+
134+
DetailAnalyzeResponse detail = new DetailAnalyzeResponse(charfilters, tokenizer, tokenfilters);
135+
136+
AnalyzeResponse response = new AnalyzeResponse(null, detail);
137+
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
138+
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
139+
Map<String, Object> converted = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
140+
List<Map<String, Object>> tokenfiltersValue = (List<Map<String, Object>>) ((Map<String, Object>)
141+
converted.get("detail")).get("tokenfilters");
142+
List<Map<String, Object>> nullTokens = (List<Map<String, Object>>) tokenfiltersValue.get(0).get("tokens");
143+
String nameValue = (String) tokenfiltersValue.get(0).get("name");
144+
assertThat(nullTokens.size(), equalTo(0));
145+
assertThat(name, equalTo(nameValue));
146+
}
147+
148+
}
115149
}

0 commit comments

Comments
 (0)