Skip to content

Commit

Permalink
Extend support for deserializing via builder if the builder has type …
Browse files Browse the repository at this point in the history
…bindings by inferring those bindings from the value type. This behavior is not ideal, but addresses the common case, and is controlled by a MapperFeature.
  • Loading branch information
Ville Koskela authored and Ville Koskela committed Jan 5, 2018
1 parent 159e268 commit 12161af
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/MapperFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ public enum MapperFeature implements ConfigFeature
*/
USE_STATIC_TYPING(false),

/**
* Feature that enables inferring builder type bindings from the value type
* being deserialized. This requires that the generic type declaration on
* the value type match that on the builder exactly.
*<p>
* Feature is disabled by default which means that deserialization does
* not support deserializing types via builders with type parameters.
*<p>
* See: https://github.com/FasterXML/jackson-databind/issues/921
*/
INFER_BUILDER_TYPE_BINDINGS(false),

/*
/******************************************************
/* View-related features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,18 @@ public JsonDeserializer<Object> createBeanDeserializer(DeserializationContext ct
}

@Override
public JsonDeserializer<Object> createBuilderBasedDeserializer(DeserializationContext ctxt,
JavaType valueType, BeanDescription beanDesc, Class<?> builderClass)
throws JsonMappingException
public JsonDeserializer<Object> createBuilderBasedDeserializer(
DeserializationContext ctxt, JavaType valueType, BeanDescription beanDesc,
Class<?> builderClass)
throws JsonMappingException
{
// First: need a BeanDescription for builder class
JavaType builderType = ctxt.constructType(builderClass);
JavaType builderType;
if (ctxt.isEnabled(MapperFeature.INFER_BUILDER_TYPE_BINDINGS)) {
builderType = ctxt.getTypeFactory().constructParametricType(builderClass, valueType.getBindings());
} else {
builderType = ctxt.constructType(builderClass);
}
BeanDescription builderDesc = ctxt.getConfig().introspectForBuilder(builderType);
return buildBuilderBasedDeserializer(ctxt, valueType, builderDesc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,29 @@ public JavaType constructParametricType(Class<?> parametrized, Class<?>... param
*/
public JavaType constructParametricType(Class<?> rawType, JavaType... parameterTypes)
{
return _fromClass(null, rawType, TypeBindings.create(rawType, parameterTypes));
return constructParametricType(rawType, TypeBindings.create(rawType, parameterTypes));
}

/**
* Factory method for constructing {@link JavaType} that
* represents a parameterized type. The type's parameters are
* specified as an instance of {@link TypeBindings}. This
* is useful if you already have the type's parameters such
* as those found on {@link JavaType}. For example, you could
* call
* <pre>
* return TypeFactory.constructParametricType(ArrayList.class, javaType.getBindings());
* </pre>
* This effectively applies the parameterized types from one
* {@link JavaType} to another class.
*
* @param rawType Actual type-erased type
* @param parameterTypes Type bindings for the raw type
* @since 3.0
*/
public JavaType constructParametricType(Class<?> rawType, TypeBindings parameterTypes)
{
return _fromClass(null, rawType, parameterTypes);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.fasterxml.jackson.failing;
package com.fasterxml.jackson.databind.deser.builder;

import java.util.List;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.LinkedHashMap;
import java.util.List;

public class BuilderDeserializationTest921
public class BuilderWithTypeParametersTest
extends BaseMapTest
{
public static class MyPOJO {
Expand Down Expand Up @@ -77,15 +80,27 @@ public MyGenericPOJOWithCreator<T> build() {
}
}

public void testWithBuilder() throws Exception {
public void testWithBuilderInferringBindings() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
mapper.enable(MapperFeature.INFER_BUILDER_TYPE_BINDINGS);
final String json = aposToQuotes("{ 'data': [ { 'x': 'x', 'y': 'y' } ] }");
final MyGenericPOJO<MyPOJO> deserialized =
mapper.readValue(json, new TypeReference<MyGenericPOJO<MyPOJO>>() {});
assertEquals(1, deserialized.data.size());
Object ob = deserialized.data.get(0);
assertNotNull(ob);
assertEquals(MyPOJO.class, ob.getClass());
}

public void testWithBuilderWithoutInferringBindings() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
final String json = aposToQuotes("{ 'data': [ { 'x': 'x', 'y': 'y' } ] }");
final MyGenericPOJO<MyPOJO> deserialized =
mapper.readValue(json, new TypeReference<MyGenericPOJO<MyPOJO>>() {});
assertEquals(1, deserialized.data.size());
Object ob = deserialized.data.get(0);
assertNotNull(ob);
assertEquals(MyPOJO.class, ob.getClass());
assertEquals(LinkedHashMap.class, ob.getClass());
}

public void testWithCreator() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,27 @@ public void testParametricTypes()
assertEquals(t, t2.containedType(1));
assertNull(t2.containedType(2));

// and then custom generic type as well
// Then using TypeBindings
JavaType t3 = tf.constructParametricType(HashSet.class, t.getBindings()); // HashSet<String>
assertEquals(CollectionType.class, t3.getClass());
assertEquals(1, t3.containedTypeCount());
assertEquals(strC, t3.containedType(0));
assertNull(t3.containedType(1));

// Then custom generic type as well
JavaType custom = tf.constructParametricType(SingleArgGeneric.class, String.class);
assertEquals(SimpleType.class, custom.getClass());
assertEquals(1, custom.containedTypeCount());
assertEquals(strC, custom.containedType(0));
assertNull(custom.containedType(1));

// and then custom generic type from TypeBindings
JavaType custom2 = tf.constructParametricType(SingleArgGeneric.class, t.getBindings());
assertEquals(SimpleType.class, custom2.getClass());
assertEquals(1, custom2.containedTypeCount());
assertEquals(strC, custom2.containedType(0));
assertNull(custom2.containedType(1));

// And finally, ensure that we can't create invalid combinations
try {
// Maps must take 2 type parameters, not just one
Expand Down

0 comments on commit 12161af

Please sign in to comment.