Skip to content

Commit

Permalink
Merge branch '2.19'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 5, 2025
2 parents 51f169b + 2d6d4b0 commit 040b27d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
7 changes: 5 additions & 2 deletions csv/src/main/java/tools/jackson/dataformat/csv/CsvParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,13 @@ protected void _readHeaderLine() throws JacksonException {
CsvSchema.Builder builder = _schema.rebuild().clearColumns();
int count = 0;

final boolean trimHeaderNames = CsvReadFeature.TRIM_HEADER_SPACES.enabledIn(_formatFeatures);
while ((name = _reader.nextString()) != null) {
// one more thing: always trim names, regardless of config settings
// TODO!!! [dataformats-text#31]: Allow disabling of trimming
name = name.trim();
// [dataformats-text#31]: Allow disabling of trimming
if (trimHeaderNames) {
name = name.trim();
}
// See if "old" schema defined type; if so, use that type...
CsvSchema.Column prev = _schema.column(name);
if (prev != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ public enum CsvReadFeature
*/
TRIM_SPACES(false),

/**
* Feature determines whether spaces around separator characters
* (commas) in header line entries (header names) are to be automatically
* trimmed before being reported or not.
* Note that this does NOT force trimming of possible white space from
* within double-quoted values, but only those surrounding unquoted
* values (white space outside of double-quotes is never included regardless
* of trimming).
*<p>
* Default value is {@code true}.
*/
TRIM_HEADER_SPACES(true),

/**
* Feature that determines how stream of records (usually CSV lines, but sometimes
* multiple lines when linefeeds are included in quoted values) is exposed:
* multiple lines when line-feeds are included in quoted values) is exposed:
* either as a sequence of Objects (false), or as an Array of Objects (true).
* Using stream of Objects is convenient when using
* <code>ObjectMapper.readValues(...)</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void testSimpleHeader() throws Exception
{
try (CsvParser parser = (CsvParser) MAPPER.reader(CsvSchema.emptySchema().withHeader())
.createParser("name, age, other\nfoo,2,xyz\n")) {
// need to enable first-line-as-schema handling:
assertToken(JsonToken.START_OBJECT, parser.nextToken());
CsvSchema schema = parser.getSchema();
assertEquals(3, schema.size());
Expand All @@ -49,7 +48,28 @@ public void testSimpleHeader() throws Exception
}

// But! Can change header name trimming:
// [dataformats-text#31]: Allow disabling header name trimming
// [dataformats-text#31]: Allow disabling header row trimming
try (CsvParser parser = (CsvParser) MAPPER.reader(CsvSchema.emptySchema().withHeader())
.without(CsvReadFeature.TRIM_HEADER_SPACES)
.createParser(
"name, age,other \nfoo,2,xyz\n")) {
assertToken(JsonToken.START_OBJECT, parser.nextToken());
CsvSchema schema = parser.getSchema();
assertEquals(3, schema.size());

// Verify header names are NOT trimmed when disabled
assertEquals("name", schema.columnName(0));
assertEquals(" age", schema.columnName(1));
assertEquals("other ", schema.columnName(2));

assertEquals("name", parser.nextName());
assertEquals("foo", parser.nextStringValue());
assertEquals(" age", parser.nextName());
assertEquals("2", parser.nextStringValue());
assertEquals("other ", parser.nextName());
assertEquals("xyz", parser.nextStringValue());
assertToken(JsonToken.END_OBJECT, parser.nextToken());
}
}

public void testSimpleQuotes() throws Exception
Expand Down
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,7 @@ Burdyug Pavel (@Pavel38l)
* Reported #485: (csv) CSVDecoder: No Long and Int out of range exceptions
(2.18.0)

Robert DiFalco (@rdifalco)

* Reported #31: Header names seem to be trimmed
(2.19.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Active Maintainers:

2.19.0 (not yet released)

#31: Header names seem to be trimmed
(reported by Robert D)
#502: Add an optional extended parser subclass (`YAMLAnchorReplayingFactory`)
able to inline anchors
(contributed by Heiko B)
Expand Down

0 comments on commit 040b27d

Please sign in to comment.