Skip to content

Commit

Permalink
Fix #609, Fix #728
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 20, 2015
1 parent 66e8ac6 commit 66bfe66
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 69 deletions.
5 changes: 5 additions & 0 deletions release-notes/VERSION
Expand Up @@ -6,12 +6,17 @@ Project: jackson-databind

2.5.2 (not yet released)

#609: Problem resolving locally declared generic type
(repoted by Hal H)
#691: NullSerializer for MapProperty failing when using polymorphic handling
(reported by Antibrumm@github)
#703: Multiple calls to ObjectMapper#canSerialize(Object.class) returns different values
(reported by flexfrank@github)
#705: JsonAnyGetter doesn't work with JsonSerialize (except with keyUsing)
(reported by natnan@github)
#728: TypeFactory#_fromVariable returns unknownType() even though it has enough information
to provide a more specific type
(reported by jkochaniak@github)
- Improvement to handling of custom `ValueInstantiator` for delegating mode; no more NPE
if `getDelegateCreator()` returns null

Expand Down
Expand Up @@ -114,8 +114,13 @@ public int getBindingCount() {
}
return _bindings.size();
}

@Deprecated // since 2.6, remove from 2.7
public JavaType findType(String name) {
return findType(name, true);
}

public JavaType findType(String name)
public JavaType findType(String name, boolean mustFind)
{
if (_bindings == null) {
_resolve();
Expand Down Expand Up @@ -158,6 +163,10 @@ public JavaType findType(String name)
*/
}
}

if (!mustFind) {
return null;
}

String className;
if (_contextClass != null) {
Expand Down
34 changes: 18 additions & 16 deletions src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java
Expand Up @@ -755,7 +755,7 @@ protected JavaType _fromClass(Class<?> clz, TypeBindings context)
context = new TypeBindings(this, cls);
}
*/

// First: do we have an array type?
if (clz.isArray()) {
result = ArrayType.construct(_constructType(clz.getComponentType(), null), null, null);
Expand Down Expand Up @@ -858,15 +858,15 @@ protected JavaType _fromParamType(ParameterizedType type, TypeBindings context)

// Ok: Map or Collection?
if (Map.class.isAssignableFrom(rawType)) {
JavaType subtype = constructSimpleType(rawType, pt);
JavaType subtype = constructSimpleType(rawType, rawType, pt);
JavaType[] mapParams = findTypeParameters(subtype, Map.class);
if (mapParams.length != 2) {
throw new IllegalArgumentException("Could not find 2 type parameters for Map class "+rawType.getName()+" (found "+mapParams.length+")");
}
return MapType.construct(rawType, mapParams[0], mapParams[1]);
}
if (Collection.class.isAssignableFrom(rawType)) {
JavaType subtype = constructSimpleType(rawType, pt);
JavaType subtype = constructSimpleType(rawType, rawType, pt);
JavaType[] collectionParams = findTypeParameters(subtype, Collection.class);
if (collectionParams.length != 1) {
throw new IllegalArgumentException("Could not find 1 type parameter for Collection class "+rawType.getName()+" (found "+collectionParams.length+")");
Expand All @@ -888,19 +888,21 @@ protected JavaType _fromArrayType(GenericArrayType type, TypeBindings context)

protected JavaType _fromVariable(TypeVariable<?> type, TypeBindings context)
{
/* 26-Sep-2009, tatus: It should be possible to try "partial"
* resolution; meaning that it is ok not to find bindings.
* For now this is indicated by passing null context.
*/
final String name = type.getName();
// 19-Mar-2015: Without context, all we can check are bounds.
if (context == null) {
return _unknownType();
}

// Ok: here's where context might come in handy!
String name = type.getName();
JavaType actualType = context.findType(name);
if (actualType != null) {
return actualType;
// And to prevent infinite loops, now need this:
context = new TypeBindings(this, (Class<?>) null);
} else {
// Ok: here's where context might come in handy!
/* 19-Mar-2015, tatu: As per [databind#609], may need to allow
* unresolved type variables to handle some cases where bounds
* are enough. Let's hope it does not hide real fail cases.
*/
JavaType actualType = context.findType(name, false);
if (actualType != null) {
return actualType;
}
}

/* 29-Jan-2010, tatu: We used to throw exception here, if type was
Expand All @@ -923,7 +925,7 @@ protected JavaType _fromVariable(TypeVariable<?> type, TypeBindings context)
* (T extends Comparable<T>). Need to add "placeholder"
* for resolution to catch those.
*/
context._addPlaceholder(name);
context._addPlaceholder(name);
return _constructType(bounds[0], context);
}

Expand Down
Expand Up @@ -4,11 +4,8 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand Down Expand Up @@ -128,6 +125,32 @@ static class MapWithTypedValues extends LinkedHashMap<String,String> { }
@JsonTypeInfo(use = Id.CLASS)
public static class Mixin691 { }

// For [databind#47]

public static class Wat
{
private final String wat;

@JsonCreator
Wat(String wat) {
this.wat = wat;
}

@JsonValue
public String getWat() {
return wat;
}

@Override
public String toString() {
return "(String)[Wat: " + wat + "]";
}
}

// For [databind#47]
@SuppressWarnings("serial")
static class WatMap extends HashMap<Wat,Boolean> { }

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -290,5 +313,14 @@ public void testNullJsonInTypedMap691() throws Exception {
String json = mapper.writeValueAsString(map);
assertEquals("{\"@class\":\"java.util.HashMap\",\"NULL\":null}", json);
}
}

public void testMapJsonValueKey47() throws Exception
{
WatMap input = new WatMap();
input.put(new Wat("3"), true);

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(input);
assertEquals(aposToQuotes("{'3':true}"), json);
}
}
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.databind.type;

import java.lang.reflect.Method;
import java.util.*;

import com.fasterxml.jackson.databind.BaseMapTest;
Expand All @@ -23,6 +24,31 @@ static enum MyEnum2 {
private MyEnum2(int value) { }
}

// [databind#728]
static class Issue728 {
public <C extends CharSequence> C method(C input) { return null; }
}
public void testLocalType728() throws Exception
{
TypeFactory tf = TypeFactory.defaultInstance();
Method m = Issue728.class.getMethod("method", CharSequence.class);
assertNotNull(m);

// Start with return type
// first type-erased
JavaType t = tf.constructType(m.getReturnType());
assertEquals(CharSequence.class, t.getRawClass());
// then generic
t = tf.constructType(m.getGenericReturnType());
assertEquals(CharSequence.class, t.getRawClass());

// then parameter type
t = tf.constructType(m.getParameterTypes()[0]);
assertEquals(CharSequence.class, t.getRawClass());
t = tf.constructType(m.getGenericParameterTypes()[0]);
assertEquals(CharSequence.class, t.getRawClass());
}

/*
/**********************************************************
/* Test methods
Expand Down
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.failing;
package com.fasterxml.jackson.databind.type;

import com.fasterxml.jackson.databind.*;

Expand Down

This file was deleted.

0 comments on commit 66bfe66

Please sign in to comment.