Skip to content

Commit

Permalink
Partial fix for #1102; use of SimpleType itself works to some degree
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 28, 2016
1 parent b5dc929 commit ae85620
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static CollectionLikeType construct(Class<?> rawType, TypeBindings bindin
public static CollectionLikeType construct(Class<?> rawType, JavaType elemT) {
return new CollectionLikeType(rawType, null,
// !!! TODO: wrong, probably has super-types, but:
null, null,
_bogusSuperClass(rawType), null,
elemT, null, null, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static CollectionType construct(Class<?> rawType, JavaType elemT) {
// nominally component types will be just Object.class
return new CollectionType(rawType, null,
// !!! TODO: Wrong, does have supertypes, but:
null, null, elemT,
_bogusSuperClass(rawType), null, elemT,
null, null, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public static MapLikeType upgradeFrom(JavaType baseType, JavaType keyT, JavaType

@Deprecated // since 2.7
public static MapLikeType construct(Class<?> rawType, JavaType keyT, JavaType valueT) {
return new MapLikeType(rawType, null, null, null, keyT, valueT, null, null, false);
return new MapLikeType(rawType, null, _bogusSuperClass(rawType), null,
keyT, valueT, null, null, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public static MapType construct(Class<?> rawType, TypeBindings bindings,
@Deprecated // since 2.7
public static MapType construct(Class<?> rawType, JavaType keyT, JavaType valueT) {
// !!! TODO: Wrong, does have supertypes
return new MapType(rawType, null, null, null, keyT, valueT, null, null, false);
return new MapType(rawType, null, _bogusSuperClass(rawType), null,
keyT, valueT, null, null, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public static SimpleType constructUnsafe(Class<?> raw) {
@Deprecated
public static SimpleType construct(Class<?> cls)
{

/* Let's add sanity checks, just to ensure no
* Map/Collection entries are constructed
*/
Expand All @@ -113,9 +114,11 @@ public static SimpleType construct(Class<?> cls)
if (cls.isArray()) {
throw new IllegalArgumentException("Can not construct SimpleType for an array (class: "+cls.getName()+")");
}
return new SimpleType(cls);
return new SimpleType(cls, TypeBindings.emptyBindings(),
// 18-Oct-2015, tatu: Should be ok to pass very minimal supertype info?
_bogusSuperClass(cls), null, null, null, false);
}

@Override
protected JavaType _narrow(Class<?> subclass)
{
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/fasterxml/jackson/databind/type/TypeBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;

Expand Down Expand Up @@ -243,4 +242,21 @@ else if (cls == Void.TYPE) {
}
return sb;
}

/**
* Internal helper method used to figure out nominal super-class for
* deprecated factory methods / constructors, where we are not given
* properly resolved supertype hierarchy.
* Will basically give `JavaType` for `java.lang.Object` for classes
* other than `java.lafgn.Object`; null for others.
*
* @since 2.7
*/
protected static JavaType _bogusSuperClass(Class<?> cls) {
Class<?> parent = cls.getSuperclass();
if (parent == null) {
return null;
}
return TypeFactory.unknownType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Set of tests to ensure that changes between 2.6 and 2.7 can
* be handled somewhat gracefully.
*/
public class DeprecatedTypeHandlingTest extends BaseMapTest
public class DeprecatedTypeHandling1102Test extends BaseMapTest
{
static class Point {
public int x;
Expand All @@ -22,28 +22,31 @@ static class Point {
}

final ObjectMapper MAPPER = objectMapper();

@SuppressWarnings("deprecation")
public void testExplicitCollectionType() throws Exception
public void testSimplePOJOType() throws Exception
{
JavaType elem = SimpleType.construct(Point.class);

Point p = MAPPER.readValue(aposToQuotes("{'x':1,'y':2}"), elem);
assertNotNull(p);
assertEquals(1, p.x);
assertEquals(2, p.getY());

}

@SuppressWarnings("deprecation")
public void testExplicitCollectionType() throws Exception
{
JavaType elem = SimpleType.construct(Point.class);
final String json = aposToQuotes("[ {'x':1,'y':2}, {'x':3,'y':6 }]");

System.err.println("ELEM -> "+elem);
System.err.println("ELEM.raw -> "+elem.getRawClass());
JavaType t = CollectionType.construct(List.class, elem);
List<Point> l = MAPPER.readValue(json, t);
assertNotNull(l);
assertEquals(2, l.size());
Object ob = l.get(0);
assertEquals(Point.class, ob.getClass());
p = (Point) ob;
Point p = (Point) ob;
assertEquals(1, p.x);
assertEquals(2, p.getY());
}
Expand Down

0 comments on commit ae85620

Please sign in to comment.