Skip to content

Commit 931531b

Browse files
committed
CSVParser.parse(String, CSVFormat) with a null CSVFormat maps to
CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat))
1 parent 41362db commit 931531b

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
<!-- FIX -->
4545
<action type="fix" issue="CSV-317" dev="ggregory" due-to="Filipe Roque">Release history link changed from changes-report.html to changes.html #516.</action>
4646
<action type="fix" dev="ggregory" due-to="Gary Gregory">Remove -nouses directive from maven-bundle-plugin. OSGi package imports now state 'uses' definitions for package imports, this doesn't affect JPMS (from org.apache.commons:commons-parent:80).</action>
47-
<action type="fix" issue="CSV-317" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(URL, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)).</action>
47+
<action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(URL, Charset, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)).</action>
48+
<action type="fix" dev="ggregory" due-to="Gary Gregory">CSVParser.parse(String, CSVFormat) with a null CSVFormat maps to CSVFormat.DEFAULT (like CSVParser.parse(Reader, CSVFormat)).</action>
4849
<!-- ADD -->
4950
<action type="add" dev="ggregory" due-to="Gary Gregory">Define and use Maven property commons.jmh.version.</action>
5051
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVFormat.Builder.setMaxRows(long).</action>

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ public static CSVParser parse(final Reader reader, final CSVFormat format) throw
403403
* @param string
404404
* a CSV string. Must not be null.
405405
* @param format
406-
* the CSVFormat used for CSV parsing. Must not be null.
406+
* the CSVFormat used for CSV parsing, {@code null} maps to {@link CSVFormat#DEFAULT}.
407407
* @return a new parser
408408
* @throws IllegalArgumentException
409409
* If the parameters of the format are inconsistent or if either string or format are null.
@@ -413,7 +413,6 @@ public static CSVParser parse(final Reader reader, final CSVFormat format) throw
413413
*/
414414
public static CSVParser parse(final String string, final CSVFormat format) throws IOException {
415415
Objects.requireNonNull(string, "string");
416-
Objects.requireNonNull(format, "format");
417416
return parse(new StringReader(string), format);
418417
}
419418

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,8 +1454,17 @@ public void testParserUrlNullCharsetFormat() {
14541454
}
14551455

14561456
@Test
1457-
public void testParseStringNullFormat() {
1458-
assertThrows(NullPointerException.class, () -> CSVParser.parse("csv data", (CSVFormat) null));
1457+
public void testParseStringNullFormat() throws IOException {
1458+
try (CSVParser parser = CSVParser.parse("1,2,3", null)) {
1459+
// null maps to DEFAULT.
1460+
final List<CSVRecord> records = parser.getRecords();
1461+
assertEquals(1, records.size());
1462+
final CSVRecord record = records.get(0);
1463+
assertEquals(3, record.size());
1464+
assertEquals("1", record.get(0));
1465+
assertEquals("2", record.get(1));
1466+
assertEquals("3", record.get(2));
1467+
}
14591468
}
14601469

14611470
@Test

0 commit comments

Comments
 (0)