Throw exception when trying to write map with null keys

Closes #14346
This commit is contained in:
javanna 2015-12-16 14:40:16 +01:00 committed by Luca Cavanna
parent 89c960b00a
commit 99f9bd7cfe
2 changed files with 36 additions and 26 deletions

View File

@ -53,7 +53,7 @@ import java.util.Map;
*/ */
public final class XContentBuilder implements BytesStream, Releasable { public final class XContentBuilder implements BytesStream, Releasable {
public static enum FieldCaseConversion { public enum FieldCaseConversion {
/** /**
* No conversion will occur. * No conversion will occur.
*/ */
@ -251,14 +251,7 @@ public final class XContentBuilder implements BytesStream, Releasable {
} }
public XContentBuilder field(XContentBuilderString name) throws IOException { public XContentBuilder field(XContentBuilderString name) throws IOException {
if (fieldCaseConversion == FieldCaseConversion.UNDERSCORE) { return field(name, fieldCaseConversion);
generator.writeFieldName(name.underscore());
} else if (fieldCaseConversion == FieldCaseConversion.CAMELCASE) {
generator.writeFieldName(name.camelCase());
} else {
generator.writeFieldName(name.underscore());
}
return this;
} }
public XContentBuilder field(XContentBuilderString name, FieldCaseConversion conversion) throws IOException { 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 { public XContentBuilder field(String name) throws IOException {
if (fieldCaseConversion == FieldCaseConversion.UNDERSCORE) { return field(name, fieldCaseConversion);
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;
} }
public XContentBuilder field(String name, FieldCaseConversion conversion) throws IOException { 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 (conversion == FieldCaseConversion.UNDERSCORE) {
if (cachedStringBuilder == null) { if (cachedStringBuilder == null) {
cachedStringBuilder = new StringBuilder(); cachedStringBuilder = new StringBuilder();

View File

@ -22,7 +22,6 @@ package org.elasticsearch.common.xcontent.builder;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.io.FastCharArrayWriter;
import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -39,6 +38,7 @@ import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.HashMap; 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.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConversion.UNDERSCORE;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
/**
*
*/
public class XContentBuilderTests extends ESTestCase { public class XContentBuilderTests extends ESTestCase {
public void testPrettyWithLfAtEnd() throws Exception { public void testPrettyWithLfAtEnd() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
@ -350,4 +347,33 @@ public class XContentBuilderTests extends ESTestCase {
"}", string.trim()); "}", 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"));
}
}
} }