You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "rootvector2 (via GitHub)" <gi...@apache.org> on 2026/07/28 08:07:43 UTC
[PR] quote null value that starts a record in minimal quote mode [commons-csv]
rootvector2 opened a new pull request, #629:
URL: https://github.com/apache/commons-csv/pull/629
`print(Object, CharSequence, Appendable, boolean)` sends a null value straight to `out.append(value)` and never reaches `printWithQuotes`, so the encapsulation `MINIMAL` does for an empty value at the start of a record is skipped. That rule exists precisely for this case, as its own comment says an empty line has no tokens. The result is that a record whose first value is null, and whose null string renders empty, prints as a blank line. `CSVFormat.DEFAULT` enables `ignoreEmptyLines`, so the record is silently dropped when the output is read back: printing `[a]`, `[null]`, `[b]` gives `a\r\n\r\nb\r\n`, which parses as 2 records. The same loss hits any `DEFAULT`-derived format, including `MONGODB_CSV` and `INFORMIX_UNLOAD`, and any format using `setNullString("")`. Found round-tripping printer output with null values back through the parser.
The fix encapsulates the empty null value when it starts a record, a quote character is set, and the quote mode is `MINIMAL` or unset, which is the same condition `printWithQuotes` already applies to an empty `CharSequence`. `ALL_NON_NULL` and `NON_NUMERIC` are left alone since they use the bare empty field to encode null, and formats without a quote character have nothing to encapsulate with. Two existing assertions move because a leading null now prints like a leading `""`; `testPrintRecordsWithObjectArray` was pinning six blank lines for six records, which is the bug itself.
- [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
- [ ] Read the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) if you use Artificial Intelligence (AI).
- [ ] I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
- [x] Run a successful build using the default [Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command line by itself.
- [x] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
- [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
- [x] Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Quote null value that starts a record in minimal quote mode [commons-csv]
Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory merged PR #629:
URL: https://github.com/apache/commons-csv/pull/629
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Quote null value that starts a record in minimal quote mode [commons-csv]
Posted by "rootvector2 (via GitHub)" <gi...@apache.org>.
rootvector2 commented on PR #629:
URL: https://github.com/apache/commons-csv/pull/629#issuecomment-5116795281
@garydgregory reviewed the copilot comments. The suggestion to extend the fix to `QuoteMode.ALL` doesn't hold: `JiraCsv203Test.testWithoutNullString` (CSV-203) pins that `ALL` writes null as the bare empty field to keep it distinct from a quoted empty string, same rationale as `ALL_NON_NULL`. I tried the extension and it fails that test, so `ALL` stays excluded and the code comment now cites CSV-203. Applied the other two: added an `ALL` assertion to `testPrintNullValueStartingRecord` pinning that encoding, and derived the size assertion in `testPrintRecordsWithObjectArray` from the expected string instead of a magic number. Default `mvn` build is green.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] quote null value that starts a record in minimal quote mode [commons-csv]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #629:
URL: https://github.com/apache/commons-csv/pull/629#discussion_r3665163030
##########
src/test/java/org/apache/commons/csv/CSVPrinterTest.java:
##########
@@ -1721,6 +1729,29 @@ void testPrinter7() throws IOException {
}
}
+ @Test
+ void testPrintNullValueStartingRecord() throws IOException {
Review Comment:
Given the regression is about ‘leading null renders empty -> blank line -> dropped by ignoreEmptyLines’, it would be valuable to add an assertion for `QuoteMode.ALL` here as well. That both documents the intended behavior for ‘quote all’ with null/empty values and will catch the remaining dropped-record scenario if `QuoteMode.ALL` isn’t handled in the runtime fix.
##########
src/test/java/org/apache/commons/csv/CSVPrinterTest.java:
##########
@@ -1721,6 +1729,29 @@ void testPrinter7() throws IOException {
}
}
+ @Test
+ void testPrintNullValueStartingRecord() throws IOException {
+ final StringWriter sw = new StringWriter();
+ try (CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
+ printer.printRecord("a");
+ printer.printRecord((Object) null);
+ printer.printRecord("b");
+ }
+ final String csv = sw.toString();
+ assertEquals("a" + RECORD_SEPARATOR + "\"\"" + RECORD_SEPARATOR + "b" + RECORD_SEPARATOR, csv);
+ try (CSVParser parser = CSVParser.parse(csv, CSVFormat.DEFAULT)) {
+ final List<CSVRecord> records = parser.getRecords();
+ assertEquals(3, records.size());
+ assertArrayEquals(new String[] { "" }, records.get(1).values());
+ }
+ // An explicit MINIMAL quote mode encapsulates it too.
+ assertEquals("\"\"" + RECORD_SEPARATOR, printNullRecord(CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.MINIMAL).get()));
+ // ALL_NON_NULL encodes null as the bare empty field, so it must stay unquoted.
+ assertEquals(RECORD_SEPARATOR, printNullRecord(CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.ALL_NON_NULL).get()));
+ // Without a quote character there is nothing to encapsulate with.
+ assertEquals(RECORD_SEPARATOR, printNullRecord(CSVFormat.DEFAULT.builder().setQuote(null).get()));
Review Comment:
Given the regression is about ‘leading null renders empty -> blank line -> dropped by ignoreEmptyLines’, it would be valuable to add an assertion for `QuoteMode.ALL` here as well. That both documents the intended behavior for ‘quote all’ with null/empty values and will catch the remaining dropped-record scenario if `QuoteMode.ALL` isn’t handled in the runtime fix.
##########
src/main/java/org/apache/commons/csv/CSVFormat.java:
##########
@@ -2272,7 +2281,17 @@ private void print(final Object object, final CharSequence value, final Appendab
out.append(getDelimiterString());
}
if (object == null) {
- out.append(value);
+ if (len == 0 && newRecord && isQuoteCharacterSet() && isMinimalQuoteMode()) {
+ // Encapsulate like printWithQuotes does for an empty value that starts a record: an
+ // unquoted one makes the whole line empty, and a parser with ignoreEmptyLines enabled
+ // then drops the record. ALL_NON_NULL and NON_NUMERIC are excluded because they encode
+ // null as the bare empty field.
+ final char quoteChar = quoteCharacter.charValue(); // Explicit unboxing is intentional
+ out.append(quoteChar);
+ out.append(quoteChar);
+ } else {
+ out.append(value);
+ }
Review Comment:
This fixes the dropped-record case for MINIMAL/unset, but the same dropped-record behavior can still occur for `QuoteMode.ALL` (which conceptually should quote empty fields too). With `quoteMode == ALL`, a leading null that renders empty will still be output as a blank line and can be dropped when parsing with `ignoreEmptyLines`. Consider extending the condition to also encapsulate for `QuoteMode.ALL` (while still excluding `ALL_NON_NULL`/`NON_NUMERIC` as intended), or otherwise aligning null-handling with the quote-mode semantics so ‘quote all’ doesn’t emit an empty record as an empty line.
##########
src/test/java/org/apache/commons/csv/CSVPrinterTest.java:
##########
@@ -1854,8 +1885,8 @@ void testPrintRecordsWithObjectArray() throws IOException {
printer.printRecords(objectArray);
assertEquals(objectArray.length, printer.getRecordCount());
}
- assertEquals(6, charArrayWriter.size());
- assertEquals("\n\n\n\n\n\n", charArrayWriter.toString());
+ assertEquals(16, charArrayWriter.size());
+ assertEquals("\"\"\n\"\"\n\"\"\n\n\"\"\n\"\"\n", charArrayWriter.toString());
Review Comment:
The `16` here is a magic number that duplicates the information already encoded by the expected string, which can make future changes more error-prone (string changes without updating the size). Consider deriving the expected size from the expected string (e.g., compare `charArrayWriter.size()` to `expected.length()`), or dropping the separate size assertion if the full string assertion is sufficient.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Quote null value that starts a record in minimal quote mode [commons-csv]
Posted by "rootvector2 (via GitHub)" <gi...@apache.org>.
rootvector2 commented on code in PR #629:
URL: https://github.com/apache/commons-csv/pull/629#discussion_r3673490461
##########
src/main/java/org/apache/commons/csv/CSVFormat.java:
##########
@@ -2272,7 +2281,17 @@ private void print(final Object object, final CharSequence value, final Appendab
out.append(getDelimiterString());
}
if (object == null) {
- out.append(value);
+ if (len == 0 && newRecord && isQuoteCharacterSet() && isMinimalQuoteMode()) {
+ // Encapsulate like printWithQuotes does for an empty value that starts a record: an
+ // unquoted one makes the whole line empty, and a parser with ignoreEmptyLines enabled
+ // then drops the record. ALL_NON_NULL and NON_NUMERIC are excluded because they encode
+ // null as the bare empty field.
+ final char quoteChar = quoteCharacter.charValue(); // Explicit unboxing is intentional
+ out.append(quoteChar);
+ out.append(quoteChar);
+ } else {
+ out.append(value);
+ }
Review Comment:
Extending this to `ALL` breaks `JiraCsv203Test.testWithoutNullString` (CSV-203), which pins that `ALL` writes null as the bare empty field so it stays distinct from a quoted `""`. That's the same encoding rationale as `ALL_NON_NULL`, so `ALL` stays excluded. Updated the code comment to say so and added a test assertion pinning it.
##########
src/test/java/org/apache/commons/csv/CSVPrinterTest.java:
##########
@@ -1721,6 +1729,29 @@ void testPrinter7() throws IOException {
}
}
+ @Test
+ void testPrintNullValueStartingRecord() throws IOException {
Review Comment:
Added an `ALL` assertion. It pins the current behavior: null stays the bare empty field per CSV-203, since quoting it would break the null vs `""` distinction that `JiraCsv203Test` checks.
##########
src/test/java/org/apache/commons/csv/CSVPrinterTest.java:
##########
@@ -1721,6 +1729,29 @@ void testPrinter7() throws IOException {
}
}
+ @Test
+ void testPrintNullValueStartingRecord() throws IOException {
+ final StringWriter sw = new StringWriter();
+ try (CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) {
+ printer.printRecord("a");
+ printer.printRecord((Object) null);
+ printer.printRecord("b");
+ }
+ final String csv = sw.toString();
+ assertEquals("a" + RECORD_SEPARATOR + "\"\"" + RECORD_SEPARATOR + "b" + RECORD_SEPARATOR, csv);
+ try (CSVParser parser = CSVParser.parse(csv, CSVFormat.DEFAULT)) {
+ final List<CSVRecord> records = parser.getRecords();
+ assertEquals(3, records.size());
+ assertArrayEquals(new String[] { "" }, records.get(1).values());
+ }
+ // An explicit MINIMAL quote mode encapsulates it too.
+ assertEquals("\"\"" + RECORD_SEPARATOR, printNullRecord(CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.MINIMAL).get()));
+ // ALL_NON_NULL encodes null as the bare empty field, so it must stay unquoted.
+ assertEquals(RECORD_SEPARATOR, printNullRecord(CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.ALL_NON_NULL).get()));
+ // Without a quote character there is nothing to encapsulate with.
+ assertEquals(RECORD_SEPARATOR, printNullRecord(CSVFormat.DEFAULT.builder().setQuote(null).get()));
Review Comment:
Covered by the new `ALL` assertion above, pinning the bare empty field per CSV-203.
##########
src/test/java/org/apache/commons/csv/CSVPrinterTest.java:
##########
@@ -1854,8 +1885,8 @@ void testPrintRecordsWithObjectArray() throws IOException {
printer.printRecords(objectArray);
assertEquals(objectArray.length, printer.getRecordCount());
}
- assertEquals(6, charArrayWriter.size());
- assertEquals("\n\n\n\n\n\n", charArrayWriter.toString());
+ assertEquals(16, charArrayWriter.size());
+ assertEquals("\"\"\n\"\"\n\"\"\n\n\"\"\n\"\"\n", charArrayWriter.toString());
Review Comment:
Done, the size assertion is now derived from the expected string.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org