Skip to content

Commit

Permalink
Fix #727
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 19, 2015
1 parent a06883b commit 468eba8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project: jackson-databind
#700: Cannot Change Default Abstract Type Mapper from LinkedHashMap
(reported by wealdtech@github)
#725: Auto-detect multi-argument constructor with implicit names if it is the only visible creator
#727: Improve `ObjectWriter.forType()` to avoid forcing base type for container types
- Remove old cglib compatibility tests; cause problems in Eclipse

2.5.2 (not yet released)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,16 @@ public ObjectWriter forType(JavaType rootType)
pf = Prefetch.empty;
} else {
// 15-Mar-2013, tatu: Important! Indicate that static typing is needed:
rootType = rootType.withStaticTyping();
/* 19-Mar-2015, tatu: Except when dealing with Collection, Map types, where
* this does more harm than help.
*/
if (!rootType.isContainerType()) {
rootType = rootType.withStaticTyping();
}
pf = _prefetchRootSerializer(_config, rootType);
}
return (pf == _prefetch) ? this : _new(_generatorSettings, pf);
}
}

/**
* Method that will construct a new instance that uses specific type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

import java.util.*;


import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestGenericTypes extends BaseMapTest
{
/*
/**********************************************************
/* Helper types
/**********************************************************
*/

static class Account {
private Long id;
private String name;
Expand Down Expand Up @@ -90,25 +85,41 @@ class Element {
public Element(T v) { value = v; }
}
}

// For [databind#728]
static class Base727 {
public int a;
}

@JsonPropertyOrder(alphabetic=true)
static class Impl727 extends Base727 {
public int b;

public Impl727(int a, int b) {
this.a = a;
this.b = b;
}
}
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/

// final ObjectMapper MAPPER = new ObjectMapper();
final ObjectMapper MAPPER = objectMapper();

@SuppressWarnings("unchecked")
public void testIssue468a() throws Exception
{
Person1 p1 = new Person1("John");
p1.setAccount(new Key<Account>(new Account("something", 42L)));

// First: ensure we can serialize (pre 1.7 this failed)
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(p1);
String json = MAPPER.writeValueAsString(p1);

// and then verify that results make sense
Map<String,Object> map = mapper.readValue(json, Map.class);
Map<String,Object> map = MAPPER.readValue(json, Map.class);
assertEquals("John", map.get("name"));
Object ob = map.get("account");
assertNotNull(ob);
Expand All @@ -131,11 +142,10 @@ public void testIssue468b() throws Exception
p2.setAccounts(accounts);

// serialize without error:
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(p2);
String json = MAPPER.writeValueAsString(p2);

// then verify output
Map<String,Object> map = mapper.readValue(json, Map.class);
Map<String,Object> map = MAPPER.readValue(json, Map.class);
assertEquals("John", map.get("name"));
Object ob = map.get("accounts");
assertNotNull(ob);
Expand All @@ -145,14 +155,28 @@ public void testIssue468b() throws Exception
}

/**
* Issue [JACKSON-572] is about unbound type variables, usually resulting
* Test related to unbound type variables, usually resulting
* from inner classes of generic classes (like Sets).
*/
public void testUnboundIssue572() throws Exception
public void testUnboundTypes() throws Exception
{
GenericBogusWrapper<Integer> list = new GenericBogusWrapper<Integer>(Integer.valueOf(7));
String json = new ObjectMapper().writeValueAsString(list);
String json = MAPPER.writeValueAsString(list);
assertEquals("{\"wrapped\":{\"value\":7}}", json);
}
}

public void testRootTypeForCollections727() throws Exception
{
List<Base727> input = new ArrayList<Base727>();
input.add(new Impl727(1, 2));

final String EXP = aposToQuotes("[{'a':1,'b':2}]");
// Without type enforcement, produces expected output:
assertEquals(EXP, MAPPER.writeValueAsString(input));
assertEquals(EXP, MAPPER.writer().writeValueAsString(input));

// but enforcing type will hinder:
TypeReference<?> typeRef = new TypeReference<List<Base727>>() { };
assertEquals(EXP, MAPPER.writer().forType(typeRef).writeValueAsString(input));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Whether this is wrong, and if so, can we fix it, is unknown at this point
* (2.3): quite possibly this can not be changed.
*/
public class TestPolymorphicDeserialization extends BaseMapTest
public class TestPolymorphicDeserialization283 extends BaseMapTest
{
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = ClassA.class)
@JsonSubTypes({
Expand Down

0 comments on commit 468eba8

Please sign in to comment.