Skip to content

Commit

Permalink
Backport #195: Adds schema creating csv schema with View
Browse files Browse the repository at this point in the history
  • Loading branch information
Qnzvna authored and cowtowncoder committed Apr 25, 2020
1 parent 997858d commit d14a30c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 14 deletions.
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

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

protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> schemas,
boolean typed)
boolean typed, Class<?> view)
{
synchronized (schemas) {
CsvSchema s = schemas.get(pojoType);
Expand All @@ -407,14 +432,19 @@ protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> 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);
}
return result;
}

@Deprecated // since 2.11 (remove from 3.0 at latest)
protected CsvSchema _schemaFor(JavaType pojoType, LRUMap<JavaType,CsvSchema> schemas, boolean typed) {
return _schemaFor(pojoType, schemas, typed, null);
}

protected boolean _nonPojoType(JavaType t)
{
if (t.isPrimitive() || t.isEnumType()) {
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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
{
Expand Down
6 changes: 5 additions & 1 deletion release-notes/CREDITS-2.x
Expand Up @@ -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)

2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Expand Up @@ -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)

Expand Down

0 comments on commit d14a30c

Please sign in to comment.