Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit b249d53

Browse files
committed
Add more tests for #72; fix an accidental exclusion of empty String for deserialization
1 parent a4c1046 commit b249d53

File tree

4 files changed

+101
-43
lines changed

4 files changed

+101
-43
lines changed

src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public void setSchema(FormatSchema schema)
316316
if (schema instanceof CsvSchema) {
317317
_schema = (CsvSchema) schema;
318318
String str = _schema.getNullValueString();
319-
_nullValue = str.isEmpty() ? null : str;
319+
_nullValue = str;
320320
} else if (schema == null) {
321321
schema = EMPTY_SCHEMA;
322322
} else {

src/main/java/com/fasterxml/jackson/dataformat/csv/CsvSchema.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* </li>
4242
* <li>nullValue (String) [default: "" (empty String)]: When asked to write Java `null`,
4343
* this String value will be used instead.<br />
44-
* NOTE: NOT used for reading at this point (this may change in future)
44+
* With 2.6, value will also be recognized during value reads.
4545
* </li>
4646
* </ul>
4747
*<p>
@@ -545,7 +545,7 @@ public Builder setLineSeparator(char lf) {
545545
}
546546

547547
public Builder setNullValue(String nvl) {
548-
return setNullValue(nvl.toCharArray());
548+
return setNullValue((nvl == null) ? null : nvl.toCharArray());
549549
}
550550

551551
public Builder setNullValue(char[] nvl) {

src/test/java/com/fasterxml/jackson/dataformat/csv/deser/NullRead72Test.java

-40
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.fasterxml.jackson.dataformat.csv.deser;
2+
3+
import com.fasterxml.jackson.databind.*;
4+
import com.fasterxml.jackson.dataformat.csv.*;
5+
6+
public class NullReadTest extends ModuleTestBase
7+
{
8+
final CsvMapper MAPPER = mapperForCsv();
9+
10+
// For [dataformat-csv#72]: recognize "null value" for reading too
11+
public void testReadNullValue72() throws Exception
12+
{
13+
CsvSchema schema = CsvSchema.builder()
14+
.setNullValue("n/a")
15+
.addColumn("id")
16+
.addColumn("desc")
17+
.build();
18+
19+
// start by writing, first
20+
String csv = MAPPER.writer(schema).writeValueAsString(new IdDesc("id", null));
21+
// MUST use doubling for quotes!
22+
assertEquals("id,n/a\n", csv);
23+
24+
// but read back
25+
26+
ObjectReader r = MAPPER.readerFor(IdDesc.class)
27+
.with(schema);
28+
29+
IdDesc result = r.readValue(csv);
30+
assertNotNull(result);
31+
assertEquals("id", result.id);
32+
assertNull(result.desc);
33+
34+
// also try the other combination
35+
result = r.readValue("n/a,Whatevs\n");
36+
assertNotNull(result);
37+
assertNull(result.id);
38+
assertEquals("Whatevs", result.desc);
39+
}
40+
41+
public void testReadNullValueFromEmptyString() throws Exception
42+
{
43+
// first: empty String should work as default
44+
CsvSchema schemaWithDefault = CsvSchema.builder()
45+
.addColumn("id")
46+
.addColumn("desc")
47+
.build();
48+
49+
// start by writing, first
50+
String csv = MAPPER.writer(schemaWithDefault).writeValueAsString(new IdDesc("id", null));
51+
assertEquals("id,\n", csv);
52+
53+
// but read back
54+
55+
ObjectReader r = MAPPER.readerFor(IdDesc.class).with(schemaWithDefault);
56+
57+
IdDesc result = r.readValue(csv);
58+
assertNotNull(result);
59+
assertEquals("id", result.id);
60+
assertNull(result.desc);
61+
62+
// also try the other combination
63+
result = r.readValue(",Whatevs\n");
64+
assertNotNull(result);
65+
assertNull(result.id);
66+
assertEquals("Whatevs", result.desc);
67+
68+
// And then with explicit Empty String
69+
CsvSchema schemaWithExplicitEmpty = CsvSchema.builder()
70+
.setNullValue("")
71+
.addColumn("id")
72+
.addColumn("desc")
73+
.build();
74+
75+
csv = MAPPER.writer(schemaWithExplicitEmpty).writeValueAsString(new IdDesc("id", null));
76+
assertEquals("id,\n", csv);
77+
r = MAPPER.readerFor(IdDesc.class).with(schemaWithExplicitEmpty);
78+
result = r.readValue(csv);
79+
assertNotNull(result);
80+
assertEquals("id", result.id);
81+
assertNull(result.desc);
82+
83+
// and finally with explicit `null`
84+
CsvSchema schemaWithExplicitNull = CsvSchema.builder()
85+
.setNullValue((String) null)
86+
.addColumn("id")
87+
.addColumn("desc")
88+
.build();
89+
90+
csv = MAPPER.writer(schemaWithExplicitNull).writeValueAsString(new IdDesc("id", null));
91+
assertEquals("id,\n", csv);
92+
r = MAPPER.readerFor(IdDesc.class).with(schemaWithExplicitNull);
93+
result = r.readValue(csv);
94+
assertNotNull(result);
95+
assertEquals("id", result.id);
96+
assertNull(result.desc);
97+
}
98+
}

0 commit comments

Comments
 (0)