Skip to content

Commit 41e0213

Browse files
committed
Fix #51
1 parent fb53e5a commit 41e0213

File tree

6 files changed

+56
-26
lines changed

6 files changed

+56
-26
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.fasterxml.jackson.core.base.GeneratorBase;
1010
import com.fasterxml.jackson.core.json.JsonWriteContext;
1111
import com.fasterxml.jackson.core.io.IOContext;
12-
import com.fasterxml.jackson.databind.JsonMappingException;
1312
import com.fasterxml.jackson.dataformat.csv.impl.CsvEncoder;
1413

1514
public class CsvGenerator extends GeneratorBase
@@ -454,17 +453,15 @@ public void close() throws IOException
454453
public final void writeStartArray() throws IOException
455454
{
456455
_verifyValueWrite("start an array");
457-
/* Ok to create root-level array to contain Objects/Arrays, but
458-
* can not nest arrays in objects
459-
*/
456+
// Ok to create root-level array to contain Objects/Arrays, but
457+
// can not nest arrays in objects
460458
if (_writeContext.inObject()) {
461459
if ((_skipWithin == null)
462460
&& _skipValue && isEnabled(JsonGenerator.Feature.IGNORE_UNKNOWN)) {
463461
_skipWithin = _writeContext;
464462
} else if (!_skipValue) {
465463
// First: column may have its own separator
466464
String sep;
467-
468465
if (_nextColumnByName >= 0) {
469466
CsvSchema.Column col = _schema.column(_nextColumnByName);
470467
sep = col.isArray() ? col.getArrayElementSeparator() : CsvSchema.NO_ARRAY_ELEMENT_SEPARATOR;
@@ -513,9 +510,8 @@ public final void writeEndArray() throws IOException
513510
_arraySeparator = CsvSchema.NO_ARRAY_ELEMENT_SEPARATOR;
514511
_writer.write(_columnIndex(), _arrayContents.toString());
515512
}
516-
/* 20-Nov-2014, tatu: When doing "untyped"/"raw" output, this means that row
517-
* is now done. But not if writing such an array field, so:
518-
*/
513+
// 20-Nov-2014, tatu: When doing "untyped"/"raw" output, this means that row
514+
// is now done. But not if writing such an array field, so:
519515
if (!_writeContext.inObject()) {
520516
finishRow();
521517
}
@@ -527,12 +523,14 @@ public final void writeStartObject() throws IOException
527523
_verifyValueWrite("start an object");
528524
// No nesting for objects; can write Objects inside logical root-level arrays.
529525
// 14-Dec-2015, tatu: ... except, should be fine if we are ignoring the property
530-
if (_writeContext.inObject()) {
526+
if (_writeContext.inObject() ||
527+
// 07-Nov-2017, tatu: But we may actually be nested indirectly; so check
528+
(_writeContext.inArray() && !_writeContext.getParent().inRoot())) {
531529
if (_skipWithin == null) { // new in 2.7
532530
if (_skipValue && isEnabled(JsonGenerator.Feature.IGNORE_UNKNOWN)) {
533531
_skipWithin = _writeContext;
534532
} else {
535-
_reportMappingError("CSV generator does not support Object values for properties");
533+
_reportMappingError("CSV generator does not support Object values for properties (nested Objects)");
536534
}
537535
}
538536
}
@@ -922,7 +920,7 @@ protected void _releaseBuffers() {
922920
* @since 2.7
923921
*/
924922
protected void _reportMappingError(String msg) throws JsonProcessingException {
925-
throw JsonMappingException.from(this, msg);
923+
throw CsvMappingException.from(this, msg, _schema);
926924
// throw new JsonGenerationException(msg, this);
927925
}
928926

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@ public CsvMappingException(CsvParser p, String msg, CsvSchema schema) {
1919
_schema = schema;
2020
}
2121

22+
public CsvMappingException(CsvGenerator gen, String msg, CsvSchema schema) {
23+
super(gen, msg);
24+
_schema = schema;
25+
}
26+
2227
public static CsvMappingException from(CsvParser p, String msg, CsvSchema schema) {
2328
return new CsvMappingException(p, msg, schema);
2429
}
2530

31+
public static CsvMappingException from(CsvGenerator gen, String msg, CsvSchema schema) {
32+
return new CsvMappingException(gen, msg, schema);
33+
}
34+
2635
public CsvSchema getSchema() {
2736
return _schema;
2837
}

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public class CsvEncoder
154154
* stream writer.
155155
*/
156156
protected int _charsWritten;
157-
157+
158158
/*
159159
/**********************************************************
160160
/* Construction, (re)configuration
Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
package com.fasterxml.jackson.dataformat.csv.failing;
1+
package com.fasterxml.jackson.dataformat.csv.ser;
22

3+
import java.io.StringWriter;
34
import java.util.*;
45

56
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
7+
import com.fasterxml.jackson.annotation.JsonValue;
68
import com.fasterxml.jackson.core.JsonGenerator;
79
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
10+
import com.fasterxml.jackson.dataformat.csv.CsvMappingException;
811
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
912
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;
1013

1114
public class GeneratorIgnoreUnknown51Test extends ModuleTestBase
1215
{
1316
// [dataformats-text#51]
17+
@JsonPropertyOrder({ "address", "people", "phoneNumber" })
18+
protected static class MyClass
19+
{
20+
public String address;
21+
public Set<Person> people;
22+
public String phoneNumber;
23+
24+
public MyClass() { }
25+
}
26+
1427
@JsonPropertyOrder({ "name", "surname" })
1528
protected static class Person
1629
{
@@ -23,16 +36,13 @@ public Person(String name, String surname)
2336
this.name = name;
2437
this.surname = surname;
2538
}
26-
}
2739

28-
@JsonPropertyOrder({ "address", "people", "phoneNumber" })
29-
protected static class MyClass
30-
{
31-
public String address;
32-
public Set<Person> people;
33-
public String phoneNumber;
3440

35-
public MyClass() { }
41+
// 07-Nov-2017, tatu: This would be a work-around:
42+
//@JsonValue
43+
public String asString() {
44+
return ""+name+" "+surname;
45+
}
3646
}
3747

3848
/*
@@ -47,8 +57,8 @@ public void testIgnoreEmbeddedObject() throws Exception
4757
CsvMapper mapper = mapperForCsv();
4858
mapper.configure( JsonGenerator.Feature.IGNORE_UNKNOWN, true );
4959
CsvSchema schema = CsvSchema.builder()
50-
.addColumn("people") // here I'm skipping phoneNumber so I need to use IGNORE_UNKNOWN feature
5160
.addColumn("address")
61+
.addColumn("people") // here I'm skipping phoneNumber so I need to use IGNORE_UNKNOWN feature
5262
.build()
5363
.withHeader();
5464

@@ -62,9 +72,13 @@ public void testIgnoreEmbeddedObject() throws Exception
6272
myClass.address = "AAA";
6373
myClass.phoneNumber = "123";
6474

65-
String result = mapper.writer( schema ).writeValueAsString( myClass );
66-
//System.err.println("REsult: "+result);
67-
int numberOfLines = result.split( "\n" ).length;
68-
assertEquals( 2, numberOfLines ); // header and data (here fails with 3)
75+
StringWriter sw = new StringWriter();
76+
try {
77+
mapper.writer(schema).writeValue(sw, myClass);
78+
fail("Should not pass");
79+
} catch (CsvMappingException e) {
80+
verifyException(e, "CSV generator does not support");
81+
verifyException(e, "nested Objects");
82+
}
6983
}
7084
}

release-notes/CREDITS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ text dataformats project, version 2.x
66

77
Tatu Saloranta, [email protected]: author
88

9+
Simone Locci (pimuzzo@github)
10+
11+
* Reported #51 (csv): Set of custom objects with `IGNORE_UNKNOWN` brokes silently csv
12+
(2.9.3)

release-notes/VERSION

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ Modules:
88
=== Releases ===
99
------------------------------------------------------------------------
1010

11+
2.9.3 (not yet released)
12+
13+
#51 (csv): Set of custom objects with `IGNORE_UNKNOWN` brokes silently csv
14+
(reported by Simone L)
15+
1116
2.9.2 (14-Oct-2017)
1217

1318
No changes since 2.9.1

0 commit comments

Comments
 (0)