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

Commit 69e1f47

Browse files
authored
Merge pull request #139 from nickbabcock/allow-trailing-comma
Add trailing comma feature
2 parents 3de7f5d + d509963 commit 69e1f47

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public enum Feature
6767
* @since 2.7
6868
*/
6969
IGNORE_TRAILING_UNMAPPABLE(false),
70+
71+
/**
72+
* Feature that allows there to be a trailing single extraneous data
73+
* column that is empty. When this feature is disabled, any extraneous
74+
* column, regardless of content will cause an exception to be thrown.
75+
* Disabling this feature is only useful when
76+
* IGNORE_TRAILING_UNMAPPABLE is also disabled.
77+
*/
78+
ALLOW_TRAILING_COMMA(true),
7079
;
7180

7281
final boolean _defaultState;
@@ -730,7 +739,7 @@ protected JsonToken _handleExtraColumn(String value) throws IOException
730739
// 14-Mar-2012, tatu: As per [dataformat-csv#1], let's allow one specific case
731740
// of extra: if we get just one all-whitespace entry, that can be just skipped
732741
_state = STATE_SKIP_EXTRA_COLUMNS;
733-
if (_columnIndex == _columnCount) {
742+
if (_columnIndex == _columnCount && Feature.ALLOW_TRAILING_COMMA.enabledIn(_formatFeatures)) {
734743
value = value.trim();
735744
if (value.isEmpty()) {
736745
// if so, need to verify we then get the end-of-record;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fasterxml.jackson.dataformat.csv.deser;
2+
3+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
4+
import com.fasterxml.jackson.databind.JsonMappingException;
5+
import com.fasterxml.jackson.databind.MappingIterator;
6+
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
7+
import com.fasterxml.jackson.dataformat.csv.CsvParser;
8+
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
9+
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;
10+
11+
public class TrailingCommaTest extends ModuleTestBase {
12+
final CsvMapper MAPPER = mapperForCsv();
13+
14+
@JsonPropertyOrder({ "a", "b" })
15+
static class StringPair {
16+
public String a, b;
17+
}
18+
19+
public void testDisallowTrailingComma() throws Exception
20+
{
21+
final String INPUT = "s,t\nd,e,\n";
22+
final CsvSchema schema = MAPPER.schemaFor(StringPair.class);
23+
24+
MappingIterator<StringPair> it = MAPPER.readerFor(StringPair.class)
25+
.with(schema)
26+
.without(CsvParser.Feature.ALLOW_TRAILING_COMMA)
27+
.readValues(INPUT);
28+
29+
it.nextValue();
30+
try {
31+
it.nextValue();
32+
fail("Should not have passed");
33+
} catch (JsonMappingException e) {
34+
verifyException(e, "Too many entries: expected at most 2 (value #2 (0 chars) \"\")");
35+
}
36+
37+
it.close();
38+
}
39+
}

0 commit comments

Comments
 (0)