NIFI-4966 - JacksonCSVRecordReader - NPE with some CSV formats

Signed-off-by: Matthew Burgess <mattyb149@apache.org>

This closes #2535
This commit is contained in:
Pierre Villard 2018-03-12 23:20:07 +01:00 committed by Matthew Burgess
parent 29c2e18832
commit c75604a564
2 changed files with 25 additions and 2 deletions

View File

@ -103,7 +103,7 @@ public class JacksonCSVRecordReader implements RecordReader {
CsvSchema csvSchema = csvSchemaBuilder.build();
// Add remaining config options to the mapper
List<CsvParser.Feature> features = new ArrayList<>(3);
List<CsvParser.Feature> 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);
}

View File

@ -123,6 +123,29 @@ public class TestJacksonCSVRecordReader {
}
}
@Test
public void testExcelFormat() throws IOException, MalformedRecordException {
final List<RecordField> fields = new ArrayList<RecordField>();
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<RecordField> fields = getDefaultFields();