diff --git a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java index 457bdee9..52b9c033 100644 --- a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java +++ b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvMapper.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; import com.fasterxml.jackson.databind.util.NameTransformer; import com.fasterxml.jackson.dataformat.csv.impl.LRUMap; +import com.fasterxml.jackson.databind.util.ViewMatcher; /** * Specialized {@link ObjectMapper}, with extended functionality to @@ -360,15 +361,27 @@ public CsvSchema schema() { * just defined to be exposed as String tokens). */ public CsvSchema schemaFor(JavaType pojoType) { - return _schemaFor(pojoType, _untypedSchemas, false); + return _schemaFor(pojoType, _untypedSchemas, false, null); + } + + public CsvSchema schemaForWithView(JavaType pojoType, Class view) { + return _schemaFor(pojoType, _untypedSchemas, false, view); } public final CsvSchema schemaFor(Class pojoType) { - return _schemaFor(constructType(pojoType), _untypedSchemas, false); + return _schemaFor(constructType(pojoType), _untypedSchemas, false, null); + } + + public final CsvSchema schemaForWithView(Class pojoType, Class view) { + return _schemaFor(constructType(pojoType), _untypedSchemas, false, view); } public final CsvSchema schemaFor(TypeReference pojoTypeRef) { - return _schemaFor(constructType(pojoTypeRef.getType()), _untypedSchemas, false); + return _schemaFor(constructType(pojoTypeRef.getType()), _untypedSchemas, false, null); + } + + public final CsvSchema schemaForWithView(TypeReference pojoTypeRef, Class view) { + return _schemaFor(constructType(pojoTypeRef.getType()), _untypedSchemas, false, view); } /** @@ -379,15 +392,27 @@ public final CsvSchema schemaFor(TypeReference pojoTypeRef) { * (especially for numeric types like java.lang.Integer). */ public CsvSchema typedSchemaFor(JavaType pojoType) { - return _schemaFor(pojoType, _typedSchemas, true); + return _schemaFor(pojoType, _typedSchemas, true, null); + } + + public CsvSchema typedSchemaForWithView(JavaType pojoType, Class view) { + return _schemaFor(pojoType, _typedSchemas, true, view); } public final CsvSchema typedSchemaFor(Class pojoType) { - return _schemaFor(constructType(pojoType), _typedSchemas, true); + return _schemaFor(constructType(pojoType), _typedSchemas, true, null); + } + + public final CsvSchema typedSchemaForWithView(Class pojoType, Class view) { + return _schemaFor(constructType(pojoType), _typedSchemas, true, view); } public final CsvSchema typedSchemaFor(TypeReference pojoTypeRef) { - return _schemaFor(constructType(pojoTypeRef.getType()), _typedSchemas, true); + return _schemaFor(constructType(pojoTypeRef.getType()), _typedSchemas, true, null); + } + + public final CsvSchema typedSchemaForWithView(TypeReference pojoTypeRef, Class view) { + return _schemaFor(constructType(pojoTypeRef.getType()), _typedSchemas, true, view); } /* @@ -397,7 +422,7 @@ public final CsvSchema typedSchemaFor(TypeReference pojoTypeRef) { */ protected CsvSchema _schemaFor(JavaType pojoType, LRUMap schemas, - boolean typed) + boolean typed, Class view) { synchronized (schemas) { CsvSchema s = schemas.get(pojoType); @@ -407,7 +432,7 @@ protected CsvSchema _schemaFor(JavaType pojoType, LRUMap sch } final AnnotationIntrospector intr = _deserializationConfig.getAnnotationIntrospector(); CsvSchema.Builder builder = CsvSchema.builder(); - _addSchemaProperties(builder, intr, typed, pojoType, null); + _addSchemaProperties(builder, intr, typed, pojoType, null, view); CsvSchema result = builder.build(); synchronized (schemas) { schemas.put(pojoType, result); @@ -415,6 +440,11 @@ protected CsvSchema _schemaFor(JavaType pojoType, LRUMap sch return result; } + @Deprecated // since 2.11 (remove from 3.0 at latest) + protected CsvSchema _schemaFor(JavaType pojoType, LRUMap schemas, boolean typed) { + return _schemaFor(pojoType, schemas, typed, null); + } + protected boolean _nonPojoType(JavaType t) { if (t.isPrimitive() || t.isEnumType()) { @@ -444,8 +474,7 @@ protected boolean _nonPojoType(JavaType t) } protected void _addSchemaProperties(CsvSchema.Builder builder, AnnotationIntrospector intr, - boolean typed, - JavaType pojoType, NameTransformer unwrapper) + boolean typed, JavaType pojoType, NameTransformer unwrapper, Class view) { // 09-Aug-2015, tatu: From [dataformat-csv#87], realized that one can not have // real schemas for primitive/wrapper @@ -455,6 +484,15 @@ protected void _addSchemaProperties(CsvSchema.Builder builder, AnnotationIntrosp BeanDescription beanDesc = getSerializationConfig().introspect(pojoType); for (BeanPropertyDefinition prop : beanDesc.findProperties()) { + if (view != null) { + Class[] views = prop.findViews(); + if (views == null) { + views = beanDesc.findDefaultViews(); + } + if (!ViewMatcher.construct(views).isVisibleForView(view)) { + continue; + } + } // ignore setter-only properties: if (!prop.couldSerialize()) { continue; @@ -468,7 +506,7 @@ protected void _addSchemaProperties(CsvSchema.Builder builder, AnnotationIntrosp nextUnwrapper = NameTransformer.chainedTransformer(unwrapper, nextUnwrapper); } JavaType nextType = m.getType(); - _addSchemaProperties(builder, intr, typed, nextType, nextUnwrapper); + _addSchemaProperties(builder, intr, typed, nextType, nextUnwrapper, view); continue; } } diff --git a/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestFiltering.java b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestFiltering.java index 0f3458b8..7ca57b83 100644 --- a/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestFiltering.java +++ b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestFiltering.java @@ -2,12 +2,18 @@ import java.io.BufferedReader; import java.io.StringReader; -import java.util.*; +import java.util.Arrays; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonFilter; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.annotation.JsonView; -import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter.FilterExceptFilter; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; + import com.fasterxml.jackson.dataformat.csv.CsvMapper; +import com.fasterxml.jackson.dataformat.csv.CsvMappingException; import com.fasterxml.jackson.dataformat.csv.CsvSchema; import com.fasterxml.jackson.dataformat.csv.ModuleTestBase; @@ -76,6 +82,41 @@ public void testWithJsonView() throws Exception assertEquals("2", result.aa); assertEquals("7", result.b); } + + public void testSchemaWithJsonViewSerialization() throws Exception + { + CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader(); + String actual = MAPPER.writer(schema).withView(ViewB.class).writeValueAsString(new Bean()); + MAPPER.writer(schema).withView(ViewB.class); + + BufferedReader br = new BufferedReader(new StringReader(actual.trim())); + assertEquals("a,b", br.readLine()); + assertEquals("1,3", br.readLine()); + assertNull(br.readLine()); + } + + public void testSchemaWithJsonViewDeserialization() throws Exception + { + CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader(); + final String input = "a,b\n5,7\n"; + Bean result = MAPPER.readerFor(Bean.class).with(schema).withView(ViewB.class).readValue(input); + + assertEquals("5", result.a); + // due to filtering, ought to use default + assertEquals("2", result.aa); + assertEquals("7", result.b); + } + + public void testSchemaWithJsonViewDeserializationFail() throws Exception + { + CsvSchema schema = MAPPER.schemaForWithView(Bean.class, ViewB.class).withLineSeparator("\n").withHeader(); + final String input = "a,aa,b\n5,6,7\n"; + try { + MAPPER.readerFor(Bean.class).with(schema).withView(ViewB.class).readValue(input); + fail(); + } catch (CsvMappingException ignore) { + } + } public void testWithJsonFilter() throws Exception { diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 835e069a..0dda94e8 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -113,6 +113,10 @@ Tyler Carpenter-Rivers (tyler2cr@github) (2.11.0) Yohann BONILLO (ybonillo@github) - * Reported #174: (csv) `CsvParser.Feature.SKIP_EMPTY_LINES` results in a mapping error (2.11.0) + +Damian Servin (Qnzvna@github) +* Contributed #195 (csv) Adds schema creating csv schema with View + (2.11.0) + diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 9ee9e182..492f6d04 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -22,6 +22,8 @@ Modules: (reported, fix contributed by Timo R) #191: (csv) `ArrayIndexOutOfBoundsException` when skipping empty lines, comments (reported by f-julian@github) +#195 (csv) Adds schema creating csv schema with View + (contributed by Damian S) 2.10.4 (not yet released)