parent
89c960b00a
commit
99f9bd7cfe
|
@ -53,7 +53,7 @@ import java.util.Map;
|
|||
*/
|
||||
public final class XContentBuilder implements BytesStream, Releasable {
|
||||
|
||||
public static enum FieldCaseConversion {
|
||||
public enum FieldCaseConversion {
|
||||
/**
|
||||
* No conversion will occur.
|
||||
*/
|
||||
|
@ -251,14 +251,7 @@ public final class XContentBuilder implements BytesStream, Releasable {
|
|||
}
|
||||
|
||||
public XContentBuilder field(XContentBuilderString name) throws IOException {
|
||||
if (fieldCaseConversion == FieldCaseConversion.UNDERSCORE) {
|
||||
generator.writeFieldName(name.underscore());
|
||||
} else if (fieldCaseConversion == FieldCaseConversion.CAMELCASE) {
|
||||
generator.writeFieldName(name.camelCase());
|
||||
} else {
|
||||
generator.writeFieldName(name.underscore());
|
||||
}
|
||||
return this;
|
||||
return field(name, fieldCaseConversion);
|
||||
}
|
||||
|
||||
public XContentBuilder field(XContentBuilderString name, FieldCaseConversion conversion) throws IOException {
|
||||
|
@ -273,22 +266,13 @@ public final class XContentBuilder implements BytesStream, Releasable {
|
|||
}
|
||||
|
||||
public XContentBuilder field(String name) throws IOException {
|
||||
if (fieldCaseConversion == FieldCaseConversion.UNDERSCORE) {
|
||||
if (cachedStringBuilder == null) {
|
||||
cachedStringBuilder = new StringBuilder();
|
||||
}
|
||||
name = Strings.toUnderscoreCase(name, cachedStringBuilder);
|
||||
} else if (fieldCaseConversion == FieldCaseConversion.CAMELCASE) {
|
||||
if (cachedStringBuilder == null) {
|
||||
cachedStringBuilder = new StringBuilder();
|
||||
}
|
||||
name = Strings.toCamelCase(name, cachedStringBuilder);
|
||||
}
|
||||
generator.writeFieldName(name);
|
||||
return this;
|
||||
return field(name, fieldCaseConversion);
|
||||
}
|
||||
|
||||
public XContentBuilder field(String name, FieldCaseConversion conversion) throws IOException {
|
||||
if (name == null) {
|
||||
throw new IllegalArgumentException("field name cannot be null");
|
||||
}
|
||||
if (conversion == FieldCaseConversion.UNDERSCORE) {
|
||||
if (cachedStringBuilder == null) {
|
||||
cachedStringBuilder = new StringBuilder();
|
||||
|
|
|
@ -22,7 +22,6 @@ package org.elasticsearch.common.xcontent.builder;
|
|||
import org.apache.lucene.util.BytesRef;
|
||||
import org.elasticsearch.common.bytes.BytesArray;
|
||||
import org.elasticsearch.common.geo.GeoPoint;
|
||||
import org.elasticsearch.common.io.FastCharArrayWriter;
|
||||
import org.elasticsearch.common.io.PathUtils;
|
||||
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
|
@ -39,6 +38,7 @@ import java.nio.file.Path;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.HashMap;
|
||||
|
@ -51,9 +51,6 @@ import static org.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConvers
|
|||
import static org.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConversion.UNDERSCORE;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class XContentBuilderTests extends ESTestCase {
|
||||
public void testPrettyWithLfAtEnd() throws Exception {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
|
@ -350,4 +347,33 @@ public class XContentBuilderTests extends ESTestCase {
|
|||
"}", string.trim());
|
||||
}
|
||||
|
||||
public void testWriteMapWithNullKeys() throws IOException {
|
||||
XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
|
||||
try {
|
||||
builder.map(Collections.singletonMap(null, "test"));
|
||||
fail("write map should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("field name cannot be null"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testWriteMapValueWithNullKeys() throws IOException {
|
||||
XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
|
||||
try {
|
||||
builder.value(Collections.singletonMap(null, "test"));
|
||||
fail("write map should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("field name cannot be null"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testWriteFieldMapWithNullKeys() throws IOException {
|
||||
XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
|
||||
try {
|
||||
builder.field("map", Collections.singletonMap(null, "test"));
|
||||
fail("write map should have failed");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("field name cannot be null"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue