This repository was archived by the owner on Jan 22, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +49
-1
lines changed
main/java/com/fasterxml/jackson/dataformat/csv
test/java/com/fasterxml/jackson/dataformat/csv/deser Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -67,6 +67,15 @@ public enum Feature
67
67
* @since 2.7
68
68
*/
69
69
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 ),
70
79
;
71
80
72
81
final boolean _defaultState ;
@@ -730,7 +739,7 @@ protected JsonToken _handleExtraColumn(String value) throws IOException
730
739
// 14-Mar-2012, tatu: As per [dataformat-csv#1], let's allow one specific case
731
740
// of extra: if we get just one all-whitespace entry, that can be just skipped
732
741
_state = STATE_SKIP_EXTRA_COLUMNS ;
733
- if (_columnIndex == _columnCount ) {
742
+ if (_columnIndex == _columnCount && Feature . ALLOW_TRAILING_COMMA . enabledIn ( _formatFeatures ) ) {
734
743
value = value .trim ();
735
744
if (value .isEmpty ()) {
736
745
// if so, need to verify we then get the end-of-record;
Original file line number Diff line number Diff line change
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\n d,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
+ }
You can’t perform that action at this time.
0 commit comments