Skip to content

Commit

Permalink
Fix #2034
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 14, 2018
1 parent 78e7873 commit bfeb1fa
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,7 @@ Jakub Skierbiszewski (jskierbi@github)
Carter Kozak (cakofony@github)
* Reported #2016: Delegating JsonCreator disregards JsonDeserialize info
(2.9.6)
Reinhard Prechtl (dnno@github)
* Reported #2034: Serialization problem with type specialization of nested generic types
(2.9.6)
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Project: jackson-databind
(reported by franboragina@github)
#2032: Blacklist another serialization gadget (ibatis)
(reported by Guixiong Wu)
#2034: Serialization problem with type specialization of nested generic types
(reported by Reinhard P)

2.9.5 (26-Mar-2018)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@ private String _resolveTypePlaceholders(JavaType sourceType, JavaType actualType
JavaType exp = expectedTypes.get(i);
JavaType act = actualTypes.get(i);
if (!_verifyAndResolvePlaceholders(exp, act)) {
// 14-May-2018, tatu: As per [databind#2034] it seems we better relax assignment
// rules further -- at least likely "raw" (untyped, non-generic) base should probably
// allow specialization.
if (exp.hasRawClass(Object.class)) {
continue;
}
// 19-Apr-2018, tatu: Hack for [databind#1964] -- allow type demotion
// for `java.util.Map` key type if (and only if) target type is
// `java.lang.Object`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.*;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.*;

/**
Expand All @@ -15,6 +16,7 @@
@SuppressWarnings("serial")
public class SubTypeResolution1964Test extends BaseMapTest
{
// [databind#1964]
static class AccessModel {
private Map<String, Collection<String>> repositoryPrivileges;

Expand All @@ -32,9 +34,47 @@ public void setRepositoryPrivileges(Map<String, Collection<String>> repositoryPr
this.repositoryPrivileges = repositoryPrivileges;
}
}

static class CustomMap<T> extends LinkedHashMap<Object, T> { }

// [databind#2034]: specialization from `Object` to other types probably should
// just be allowed (at least in serialization case)
interface Dummy {
List<String> getStrings();
}

static class MetaModel<M, B> extends AbstractMetaValue<M, M, B> {

@JsonProperty
protected final Map<String, AbstractMetaValue<M, ?, B>> attributes = new HashMap<>();

public <V> ListMetaAttribute<M, V, B> describeList(final String attributeName) {
final ListMetaAttribute<M, V, B> metaAttribute = new ListMetaAttribute<>();
attributes.put(attributeName, metaAttribute);
return metaAttribute;
}
}

static abstract class AbstractMetaValue<M, V, B> {
public int getBogus() { return 3; }
}

static class ListMetaAttribute<M, V, B> extends MetaAttribute<M, List<V>, B> {
public ListMetaAttribute() { }
}

static class MetaAttribute<M, V, B> extends AbstractMetaValue<M, V, B> {
public MetaAttribute() { }
}

/*
/**********************************************************************
/* Unit tests
/**********************************************************************
*/

final ObjectMapper MAPPER = newObjectMapper();

// [databind#1964]
public void testTypeCompatibility1964() throws Exception
{
// Important! Must use raw type since assignment requires effectively
Expand All @@ -49,8 +89,17 @@ public void testTypeCompatibility1964() throws Exception
AccessModel accessModel = new AccessModel();
accessModel.setRepositoryPrivileges(repoPrivilegesMap);

ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(accessModel);
String jsonStr = MAPPER.writeValueAsString(accessModel);
// ... could/should verify more, perhaps, but for now let it be.
assertNotNull(jsonStr);
}

// [databind#2034]
public void testTypeSpecialization2034() throws Exception
{
MetaModel<Dummy, Dummy> metaModel = new MetaModel<>();
metaModel.describeList("a1");
String jsonStr = MAPPER.writeValueAsString(metaModel);
// ... could/should verify more, perhaps, but for now let it be.
assertNotNull(jsonStr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public ScalarList add(Object v) {
return this;
}
}

/*
/**********************************************************
/* Unit tests
/**********************************************************
*/

final ObjectMapper MAPPER = new ObjectMapper();
final ObjectMapper MAPPER = newObjectMapper();

/**
* Ensure that per-property dynamic types work, both for "native" types
Expand Down

0 comments on commit bfeb1fa

Please sign in to comment.