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

Add CsvParser.Feature.ALLOW_TRAILING_COMMA to allow enforcing strict handling #139

Merged
merged 1 commit into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ public enum Feature
* @since 2.7
*/
IGNORE_TRAILING_UNMAPPABLE(false),

/**
* Feature that allows there to be a trailing single extraneous data
* column that is empty. When this feature is disabled, any extraneous
* column, regardless of content will cause an exception to be thrown.
* Disabling this feature is only useful when
* IGNORE_TRAILING_UNMAPPABLE is also disabled.
*/
ALLOW_TRAILING_COMMA(true),
;

final boolean _defaultState;
Expand Down Expand Up @@ -730,7 +739,7 @@ protected JsonToken _handleExtraColumn(String value) throws IOException
// 14-Mar-2012, tatu: As per [dataformat-csv#1], let's allow one specific case
// of extra: if we get just one all-whitespace entry, that can be just skipped
_state = STATE_SKIP_EXTRA_COLUMNS;
if (_columnIndex == _columnCount) {
if (_columnIndex == _columnCount && Feature.ALLOW_TRAILING_COMMA.enabledIn(_formatFeatures)) {
value = value.trim();
if (value.isEmpty()) {
// if so, need to verify we then get the end-of-record;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.fasterxml.jackson.dataformat.csv.deser;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvParser;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;

public class TrailingCommaTest extends ModuleTestBase {
final CsvMapper MAPPER = mapperForCsv();

@JsonPropertyOrder({ "a", "b" })
static class StringPair {
public String a, b;
}

public void testDisallowTrailingComma() throws Exception
{
final String INPUT = "s,t\nd,e,\n";
final CsvSchema schema = MAPPER.schemaFor(StringPair.class);

MappingIterator<StringPair> it = MAPPER.readerFor(StringPair.class)
.with(schema)
.without(CsvParser.Feature.ALLOW_TRAILING_COMMA)
.readValues(INPUT);

it.nextValue();
try {
it.nextValue();
fail("Should not have passed");
} catch (JsonMappingException e) {
verifyException(e, "Too many entries: expected at most 2 (value #2 (0 chars) \"\")");
}

it.close();
}
}