NPE when closing XContentBuilder and using 'pretty' query parameter

fixes #4100
This commit is contained in:
Shay Banon 2013-11-06 01:10:58 +01:00
parent 71f5b16137
commit ebc8975efd
2 changed files with 22 additions and 1 deletions

View File

@ -327,6 +327,9 @@ public class JsonXContentGenerator implements XContentGenerator {
@Override
public void close() throws IOException {
if (generator.isClosed()) {
return;
}
if (writeLineFeedAtEnd) {
flush();
generator.writeRaw(LF);

View File

@ -33,7 +33,6 @@ import java.util.*;
import static org.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConversion.CAMELCASE;
import static org.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConversion.UNDERSCORE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
/**
@ -41,6 +40,25 @@ import static org.hamcrest.Matchers.equalTo;
*/
public class XContentBuilderTests extends ElasticsearchTestCase {
@Test
public void testPrettyWithLfAtEnd() throws Exception {
FastCharArrayWriter writer = new FastCharArrayWriter();
XContentGenerator generator = XContentFactory.xContent(XContentType.JSON).createGenerator(writer);
generator.usePrettyPrint();
generator.usePrintLineFeedAtEnd();
generator.writeStartObject();
generator.writeStringField("test", "value");
generator.writeEndObject();
generator.flush();
generator.close();
// double close, and check there is no error...
generator.close();
assertThat(writer.unsafeCharArray()[writer.size() - 1], equalTo('\n'));
}
@Test
public void verifyReuseJsonGenerator() throws Exception {
FastCharArrayWriter writer = new FastCharArrayWriter();