Skip to content

Commit d14a30c

Browse files
Qnzvnacowtowncoder
authored andcommitted
Backport #195: Adds schema creating csv schema with View
1 parent 997858d commit d14a30c

File tree

4 files changed

+99
-14
lines changed

4 files changed

+99
-14
lines changed

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

+49-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
1010
import com.fasterxml.jackson.databind.util.NameTransformer;
1111
import com.fasterxml.jackson.dataformat.csv.impl.LRUMap;
12+
import com.fasterxml.jackson.databind.util.ViewMatcher;
1213

1314
/**
1415
* Specialized {@link ObjectMapper}, with extended functionality to
@@ -360,15 +361,27 @@ public CsvSchema schema() {
360361
* just defined to be exposed as String tokens).
361362
*/
362363
public CsvSchema schemaFor(JavaType pojoType) {
363-
return _schemaFor(pojoType, _untypedSchemas, false);
364+
return _schemaFor(pojoType, _untypedSchemas, false, null);
365+
}
366+
367+
public CsvSchema schemaForWithView(JavaType pojoType, Class<?> view) {
368+
return _schemaFor(pojoType, _untypedSchemas, false, view);
364369
}
365370

366371
public final CsvSchema schemaFor(Class<?> pojoType) {
367-
return _schemaFor(constructType(pojoType), _untypedSchemas, false);
372+
return _schemaFor(constructType(pojoType), _untypedSchemas, false, null);
373+
}
374+
375+
public final CsvSchema schemaForWithView(Class<?> pojoType, Class<?> view) {
376+
return _schemaFor(constructType(pojoType), _untypedSchemas, false, view);
368377
}
369378

370379
public final CsvSchema schemaFor(TypeReference<?> pojoTypeRef) {
371-
return _schemaFor(constructType(pojoTypeRef.getType()), _untypedSchemas, false);
380+
return _schemaFor(constructType(pojoTypeRef.getType()), _untypedSchemas, false, null);
381+
}
382+
383+
public final CsvSchema schemaForWithView(TypeReference<?> pojoTypeRef, Class<?> view) {
384+
return _schemaFor(constructType(pojoTypeRef.getType()), _untypedSchemas, false, view);
372385
}
373386

374387
/**
@@ -379,15 +392,27 @@ public final CsvSchema schemaFor(TypeReference<?> pojoTypeRef) {
379392
* (especially for numeric types like java.lang.Integer).
380393
*/
381394
public CsvSchema typedSchemaFor(JavaType pojoType) {
382-
return _schemaFor(pojoType, _typedSchemas, true);
395+
return _schemaFor(pojoType, _typedSchemas, true, null);
396+
}
397+
398+
public CsvSchema typedSchemaForWithView(JavaType pojoType, Class<?> view) {
399+
return _schemaFor(pojoType, _typedSchemas, true, view);
383400
}
384401

385402
public final CsvSchema typedSchemaFor(Class<?> pojoType) {
386-
return _schemaFor(constructType(pojoType), _typedSchemas, true);
403+
return _schemaFor(constructType(pojoType), _typedSchemas, true, null);
404+
}
405+
406+
public final CsvSchema typedSchemaForWithView(Class<?> pojoType, Class<?> view) {
407+
return _schemaFor(constructType(pojoType), _typedSchemas, true, view);
387408
}
388409

389410
public final CsvSchema typedSchemaFor(TypeReference<?> pojoTypeRef) {
390-
return _schemaFor(constructType(pojoTypeRef.getType()), _typedSchemas, true);
411+
return _schemaFor(constructType(pojoTypeRef.getType()), _typedSchemas, true, null);
412+
}
413+
414+
public final CsvSchema typedSchemaForWithView(TypeReference<?> pojoTypeRef, Class<?> view) {
415+
return _schemaFor(constructType(pojoTypeRef.getType()), _typedSchemas, true, view);
391416
}
392417

393418
/*
@@ -397,7 +422,7 @@ public final CsvSchema typedSchemaFor(TypeReference<?> pojoTypeRef) {
397422
*/
398423

399424
protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> schemas,
400-
boolean typed)
425+
boolean typed, Class<?> view)
401426
{
402427
synchronized (schemas) {
403428
CsvSchema s = schemas.get(pojoType);
@@ -407,14 +432,19 @@ protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> sch
407432
}
408433
final AnnotationIntrospector intr = _deserializationConfig.getAnnotationIntrospector();
409434
CsvSchema.Builder builder = CsvSchema.builder();
410-
_addSchemaProperties(builder, intr, typed, pojoType, null);
435+
_addSchemaProperties(builder, intr, typed, pojoType, null, view);
411436
CsvSchema result = builder.build();
412437
synchronized (schemas) {
413438
schemas.put(pojoType, result);
414439
}
415440
return result;
416441
}
417442

443+
@Deprecated // since 2.11 (remove from 3.0 at latest)
444+
protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> schemas, boolean typed) {
445+
return _schemaFor(pojoType, schemas, typed, null);
446+
}
447+
418448
protected boolean _nonPojoType(JavaType t)
419449
{
420450
if (t.isPrimitive() || t.isEnumType()) {
@@ -444,8 +474,7 @@ protected boolean _nonPojoType(JavaType t)
444474
}
445475

446476
protected void _addSchemaProperties(CsvSchema.Builder builder, AnnotationIntrospector intr,
447-
boolean typed,
448-
JavaType pojoType, NameTransformer unwrapper)
477+
boolean typed, JavaType pojoType, NameTransformer unwrapper, Class<?> view)
449478
{
450479
// 09-Aug-2015, tatu: From [dataformat-csv#87], realized that one can not have
451480
// real schemas for primitive/wrapper
@@ -455,6 +484,15 @@ protected void _addSchemaProperties(CsvSchema.Builder builder, AnnotationIntrosp
455484

456485
BeanDescription beanDesc = getSerializationConfig().introspect(pojoType);
457486
for (BeanPropertyDefinition prop : beanDesc.findProperties()) {
487+
if (view != null) {
488+
Class<?>[] views = prop.findViews();
489+
if (views == null) {
490+
views = beanDesc.findDefaultViews();
491+
}
492+
if (!ViewMatcher.construct(views).isVisibleForView(view)) {
493+
continue;
494+
}
495+
}
458496
// ignore setter-only properties:
459497
if (!prop.couldSerialize()) {
460498
continue;
@@ -468,7 +506,7 @@ protected void _addSchemaProperties(CsvSchema.Builder builder, AnnotationIntrosp
468506
nextUnwrapper = NameTransformer.chainedTransformer(unwrapper, nextUnwrapper);
469507
}
470508
JavaType nextType = m.getType();
471-
_addSchemaProperties(builder, intr, typed, nextType, nextUnwrapper);
509+
_addSchemaProperties(builder, intr, typed, nextType, nextUnwrapper, view);
472510
continue;
473511
}
474512
}

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

+43-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
import java.io.BufferedReader;
44
import java.io.StringReader;
5-
import java.util.*;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import com.fasterxml.jackson.annotation.JsonFilter;
9+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
10+
import com.fasterxml.jackson.annotation.JsonView;
611

7-
import com.fasterxml.jackson.annotation.*;
812
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter.FilterExceptFilter;
913
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
14+
1015
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
16+
import com.fasterxml.jackson.dataformat.csv.CsvMappingException;
1117
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
1218
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;
1319

@@ -76,6 +82,41 @@ public void testWithJsonView() throws Exception
7682
assertEquals("2", result.aa);
7783
assertEquals("7", result.b);
7884
}
85+
86+
public void testSchemaWithJsonViewSerialization() throws Exception
87+
{
88+
CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader();
89+
String actual = MAPPER.writer(schema).withView(ViewB.class).writeValueAsString(new Bean());
90+
MAPPER.writer(schema).withView(ViewB.class);
91+
92+
BufferedReader br = new BufferedReader(new StringReader(actual.trim()));
93+
assertEquals("a,b", br.readLine());
94+
assertEquals("1,3", br.readLine());
95+
assertNull(br.readLine());
96+
}
97+
98+
public void testSchemaWithJsonViewDeserialization() throws Exception
99+
{
100+
CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader();
101+
final String input = "a,b\n5,7\n";
102+
Bean result = MAPPER.readerFor(Bean.class).with(schema).withView(ViewB.class).readValue(input);
103+
104+
assertEquals("5", result.a);
105+
// due to filtering, ought to use default
106+
assertEquals("2", result.aa);
107+
assertEquals("7", result.b);
108+
}
109+
110+
public void testSchemaWithJsonViewDeserializationFail() throws Exception
111+
{
112+
CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader();
113+
final String input = "a,aa,b\n5,6,7\n";
114+
try {
115+
MAPPER.readerFor(Bean.class).with(schema).withView(ViewB.class).readValue(input);
116+
fail();
117+
} catch (CsvMappingException ignore) {
118+
}
119+
}
79120

80121
public void testWithJsonFilter() throws Exception
81122
{

release-notes/CREDITS-2.x

+5-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ Tyler Carpenter-Rivers (tyler2cr@github)
113113
(2.11.0)
114114

115115
Yohann BONILLO (ybonillo@github)
116-
117116
* Reported #174: (csv) `CsvParser.Feature.SKIP_EMPTY_LINES` results in a mapping error
118117
(2.11.0)
118+
119+
Damian Servin (Qnzvna@github)
120+
* Contributed #195 (csv) Adds schema creating csv schema with View
121+
(2.11.0)
122+

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Modules:
2222
(reported, fix contributed by Timo R)
2323
#191: (csv) `ArrayIndexOutOfBoundsException` when skipping empty lines, comments
2424
(reported by f-julian@github)
25+
#195 (csv) Adds schema creating csv schema with View
26+
(contributed by Damian S)
2527

2628
2.10.4 (not yet released)
2729

0 commit comments

Comments
 (0)