This commit is contained in:
parent
3d49523726
commit
bf058d6e4d
|
@ -296,8 +296,10 @@ public class DetailAnalyzeResponse implements Streamable, ToXContentFragment {
|
||||||
XContentBuilder toXContentWithoutObject(XContentBuilder builder, Params params) throws IOException {
|
XContentBuilder toXContentWithoutObject(XContentBuilder builder, Params params) throws IOException {
|
||||||
builder.field(Fields.NAME, this.name);
|
builder.field(Fields.NAME, this.name);
|
||||||
builder.startArray(AnalyzeResponse.Fields.TOKENS);
|
builder.startArray(AnalyzeResponse.Fields.TOKENS);
|
||||||
for (AnalyzeResponse.AnalyzeToken token : tokens) {
|
if (tokens != null) {
|
||||||
token.toXContent(builder, params);
|
for (AnalyzeResponse.AnalyzeToken token : tokens) {
|
||||||
|
token.toXContent(builder, params);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
builder.endArray();
|
builder.endArray();
|
||||||
return builder;
|
return builder;
|
||||||
|
|
|
@ -19,7 +19,12 @@
|
||||||
|
|
||||||
package org.elasticsearch.action.admin.indices.analyze;
|
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.XContentParser;
|
||||||
|
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
||||||
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
|
import org.elasticsearch.test.AbstractStreamableXContentTestCase;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -30,6 +35,8 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
public class AnalyzeResponseTests extends AbstractStreamableXContentTestCase<AnalyzeResponse> {
|
public class AnalyzeResponseTests extends AbstractStreamableXContentTestCase<AnalyzeResponse> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -112,4 +119,31 @@ public class AnalyzeResponseTests extends AbstractStreamableXContentTestCase<Ana
|
||||||
}
|
}
|
||||||
return new AnalyzeResponse.AnalyzeToken(token, position, startOffset, endOffset, posLength, type, extras);
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue