From c75604a564c4abf5c4d26b512b4c6d7639fa592b Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Mon, 12 Mar 2018 23:20:07 +0100 Subject: [PATCH] NIFI-4966 - JacksonCSVRecordReader - NPE with some CSV formats Signed-off-by: Matthew Burgess This closes #2535 --- .../nifi/csv/JacksonCSVRecordReader.java | 4 ++-- .../nifi/csv/TestJacksonCSVRecordReader.java | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/JacksonCSVRecordReader.java b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/JacksonCSVRecordReader.java index 91cca81097..a25df65812 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/JacksonCSVRecordReader.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/csv/JacksonCSVRecordReader.java @@ -103,7 +103,7 @@ public class JacksonCSVRecordReader implements RecordReader { CsvSchema csvSchema = csvSchemaBuilder.build(); // Add remaining config options to the mapper - List features = new ArrayList<>(3); + List features = new ArrayList<>(); features.add(CsvParser.Feature.INSERT_NULLS_FOR_MISSING_COLUMNS); if (csvFormat.getIgnoreEmptyLines()) { features.add(CsvParser.Feature.SKIP_EMPTY_LINES); @@ -114,7 +114,7 @@ public class JacksonCSVRecordReader implements RecordReader { ObjectReader objReader = mapper.readerFor(String[].class) .with(csvSchema) - .withFeatures(features.toArray(new CsvParser.Feature[3])); + .withFeatures(features.toArray(new CsvParser.Feature[features.size()])); recordStream = objReader.readValues(reader); } diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/csv/TestJacksonCSVRecordReader.java b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/csv/TestJacksonCSVRecordReader.java index 9e085949bf..66486eef1a 100644 --- a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/csv/TestJacksonCSVRecordReader.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/csv/TestJacksonCSVRecordReader.java @@ -123,6 +123,29 @@ public class TestJacksonCSVRecordReader { } } + @Test + public void testExcelFormat() throws IOException, MalformedRecordException { + final List fields = new ArrayList(); + fields.add(new RecordField("fieldA", RecordFieldType.STRING.getDataType())); + fields.add(new RecordField("fieldB", RecordFieldType.STRING.getDataType())); + final RecordSchema schema = new SimpleRecordSchema(fields); + + final String headerLine = "fieldA,fieldB"; + final String inputRecord = "valueA,valueB"; + final String csvData = headerLine + "\n" + inputRecord; + final byte[] inputData = csvData.getBytes(); + + try (final InputStream bais = new ByteArrayInputStream(inputData); + final JacksonCSVRecordReader reader = createReader(bais, schema, CSVFormat.EXCEL)) { + + final Object[] record = reader.nextRecord().getValues(); + final Object[] expectedValues = new Object[] {"valueA", "valueB"}; + Assert.assertArrayEquals(expectedValues, record); + + assertNull(reader.nextRecord()); + } + } + @Test public void testMultipleRecords() throws IOException, MalformedRecordException { final List fields = getDefaultFields();