Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit 92a070b

Browse files
committed
Fix #90
1 parent 73879b8 commit 92a070b

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

release-notes/CREDITS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ sothmann@github)
4545

4646
* Reported #83: Serializing List with null values leads to corrupt CSV
4747
(2.6.0)
48+
49+
Jonathan Cheseaux (cheseaux@github)
50+
51+
* Reported #90: Unexpected output with arrays starting with a null/empty element
52+
(2.6.4)

release-notes/VERSION

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Project: jackson-dataformat-csv
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
2.6.4 (not yet released)
8+
9+
#90: Unexpected output with arrays starting with a null/empty element
10+
(reported by Jonathan C)
11+
712
2.6.3 (12-Oct-2015)
813

914
#91: Ensure that "too many columns" is recoverable

src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,19 @@ private Feature(boolean defaultState) {
158158
*/
159159
protected int _arraySeparator = -1;
160160

161+
/**
162+
* Accumulated contents of an array cell, if any
163+
*/
161164
protected StringBuilder _arrayContents;
165+
166+
/**
167+
* Additional counter that indicates number of value entries in the
168+
* array. Needed because `null` entries do not add content, but need
169+
* to be separated by array cell separator
170+
*
171+
* @since 2.7
172+
*/
173+
protected int _arrayElements;
162174

163175
/*
164176
/**********************************************************
@@ -424,6 +436,7 @@ public final void writeStartArray() throws IOException
424436
} else {
425437
_arrayContents.setLength(0);
426438
}
439+
_arrayElements = 0;
427440
}
428441
} else if (_arraySeparator >= 0) {
429442
// also: no nested arrays, yet
@@ -863,16 +876,18 @@ protected void _handleFirstLine() throws IOException
863876
}
864877

865878
protected void _addToArray(String value) {
866-
if (_arrayContents.length() > 0) {
879+
if (_arrayElements > 0) {
867880
_arrayContents.append((char) _arraySeparator);
868881
}
882+
++_arrayElements;
869883
_arrayContents.append(value);
870884
}
871885

872886
protected void _addToArray(char[] value) {
873-
if (_arrayContents.length() > 0) {
887+
if (_arrayElements > 0) {
874888
_arrayContents.append((char) _arraySeparator);
875889
}
890+
++_arrayElements;
876891
_arrayContents.append(value);
877892
}
878893
}

src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestParserWorkarounds.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public void testIgnoringOptionalTrailing() throws Exception
2727
.addColumn("second")
2828
.build();
2929

30-
@SuppressWarnings("resource")
3130
MappingIterator<Map<?,?>> it = mapper.reader(schema).forType(Map.class).readValues(
3231
"a,b\nc,d,\ne,f, \nfoo,bar,x\n");
3332
assertTrue(it.hasNext());
@@ -59,6 +58,7 @@ public void testIgnoringOptionalTrailing() throws Exception
5958
} catch (JsonParseException e) {
6059
verifyException(e, "Too many entries");
6160
}
61+
it.close();
6262
}
6363

6464
// also ensure [databind-csv#1] also works appropriately for failing case
@@ -70,7 +70,6 @@ public void testOptionalTrailFailing() throws Exception
7070
.addColumn("second")
7171
.build();
7272

73-
@SuppressWarnings("resource")
7473
MappingIterator<Map<?,?>> it = mapper.reader(schema).forType(Map.class).readValues(
7574
"a,b,\nc,d,,,\n");
7675
assertTrue(it.hasNext());
@@ -88,5 +87,6 @@ public void testOptionalTrailFailing() throws Exception
8887
} catch (JsonProcessingException e) {
8988
verifyException(e, "Too many entries: expected at most 2");
9089
}
90+
it.close();
9191
}
9292
}

src/test/java/com/fasterxml/jackson/dataformat/csv/ser/ArrayWriteTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import com.fasterxml.jackson.dataformat.csv.*;
66

7-
// for [dataformat-csv#57]
7+
// Tests for verifying that it is possible to write "simple" arrays
8+
// (ones with scalar serializations) as CSV
89
public class ArrayWriteTest extends ModuleTestBase
910
{
1011
@JsonPropertyOrder({"id", "values", "extra"})
@@ -19,6 +20,16 @@ public ValueEntry(String id, String extra, int... v) {
1920
}
2021
}
2122

23+
@JsonPropertyOrder({"a", "b", "c"})
24+
static class Pojo90 {
25+
26+
public String[] a = new String[]{"", "foo"};
27+
28+
public String[] b = new String[]{null, "bar"};
29+
30+
public String[] c = new String[]{"baz",null};
31+
}
32+
2233
/*
2334
/**********************************************************************
2435
/* Test methods
@@ -49,4 +60,17 @@ public void testSeparatorOverride() throws Exception
4960
// gets quoted due to white space
5061
assertEquals("foo,\"1 2 3\",stuff", csv);
5162
}
63+
64+
public void testArraysWithNulls() throws Exception
65+
{
66+
Pojo90 value = new Pojo90();
67+
CsvMapper mapper = mapperForCsv();
68+
String csvContent = mapper.writer(mapper.schemaFor(Pojo90.class)
69+
.withHeader())
70+
.writeValueAsString(value);
71+
String[] lines = csvContent.split("\\n");
72+
assertEquals(2, lines.length);
73+
assertEquals("a,b,c", lines[0]);
74+
assertEquals(";foo,;bar,baz;", lines[1]);
75+
}
5276
}

0 commit comments

Comments
 (0)