Skip to content

Commit

Permalink
Fixed #204
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 11, 2020
1 parent efdf20e commit 1726341
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,6 @@ protected void _readHeaderLine() throws IOException {
while ((name = _reader.nextString()) != null) {
// one more thing: always trim names, regardless of config settings
name = name.trim();

// See if "old" schema defined type; if so, use that type...
CsvSchema.Column prev = _schema.column(name);
if (prev != null) {
Expand All @@ -776,6 +775,11 @@ protected void _readHeaderLine() throws IOException {
}
}

// [dataformats-text#204]: Drop trailing empty name if so instructed
if (CsvParser.Feature.ALLOW_TRAILING_COMMA.enabledIn(_formatFeatures)) {
builder.dropLastColumnIfEmpty();
}

// Ok: did we get any columns?
CsvSchema newSchema = builder.build();
int size = newSchema.size();
Expand Down Expand Up @@ -1002,6 +1006,7 @@ protected JsonToken _handleExtraColumn(String value) throws IOException
if (next == null) { // should end of record or input
return _handleObjectRowEnd();
}
System.err.println("... yet we did NOT skip");
}
}
// 21-May-2015, tatu: Need to enter recovery mode, to skip remainder of the line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,22 @@ public Builder renameColumn(int index, String newName) {
return this;
}

/**
* Helper method called to drop the last collected column name if
* it is empty: called if {link CsvParser.Feature#ALLOW_TRAILING_COMMA}
* enabled to remove the last entry after being added initially.
*
* @since 2.11.2
*/
public void dropLastColumnIfEmpty() {
final int ix = _columns.size() - 1;
if (ix >= 0) {
if (_columns.get(ix).getName().isEmpty()) {
_columns.remove(ix);
}
}
}

public Builder setColumnType(int index, ColumnType type) {
_checkIndex(index);
_columns.set(index, _columns.get(index).withType(type));
Expand Down Expand Up @@ -795,7 +811,7 @@ public Builder setNullValue(char[] nvl) {
_nullValue = nvl;
return this;
}

public CsvSchema build()
{
Column[] cols = _columns.toArray(new Column[_columns.size()]);
Expand All @@ -817,7 +833,7 @@ protected void _checkIndex(int index) {
/* Configuration, construction
/**********************************************************************
*/

/**
* Column definitions, needed for optional header and/or mapping
* of field names to column positions.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.fasterxml.jackson.dataformat.csv.deser;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.MappingIterator;
Expand All @@ -8,14 +10,20 @@
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;

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

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

static class Person {
public String name;
public int age;
}

private final CsvMapper MAPPER = mapperForCsv();

public void testDisallowTrailingComma() throws Exception
{
final String INPUT = "s,t\nd,e,\n";
Expand All @@ -36,4 +44,32 @@ public void testDisallowTrailingComma() throws Exception

it.close();
}

// [dataformats-text#204]: should also work for header line

public void testWithTrailingHeaderComma() throws Exception
{
final String INPUT = "name,age,\n" +
"Roger,27,\n" +
"Chris,53,\n";
final CsvSchema schema = CsvSchema.emptySchema().withHeader();

MappingIterator<Person> persons = MAPPER
.enable(CsvParser.Feature.ALLOW_TRAILING_COMMA)
.readerFor(Person.class)
.with(schema)
.<Person> readValues(INPUT);
assertTrue(persons.hasNextValue());
Person p = persons.nextValue();
assertNotNull(p);
assertEquals("Roger", p.name);

assertTrue(persons.hasNextValue());
p = persons.nextValue();
assertNotNull(p);
assertEquals(53, p.age);

assertFalse(persons.hasNextValue());
persons.close();
}
}
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,7 @@ Francesco Tumanischvili (frantuma@github)
* Contibuted fix for #201: (yaml) Improve `MINIMIZE_QUOTES` handling to avoid quoting
for some uses of `#` and `:`
(2.11.1)

Björn Michael (bjmi@github)
* Reported #204: `CsvParser.Feature.ALLOW_TRAILING_COMMA` doesn't work with header columns
(2.11.2)
5 changes: 5 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Project: jackson-datatypes-text
=== Releases ===
------------------------------------------------------------------------

2.11.2 (not yet released)

#204: `CsvParser.Feature.ALLOW_TRAILING_COMMA` doesn't work with header columns
(reported by Björn M)
2.11.1 (25-Jun-2020)
#51: (yaml) `YAMLParser._locationFor()` does not use index available from `Mark`
Expand Down

0 comments on commit 1726341

Please sign in to comment.