Skip to content

Commit b4c1f77

Browse files
committed
Merge pull request #367 from UnquietCode/master
Support type modifiers in type id name resolution.
2 parents 4f3c165 + 0ffbca8 commit b4c1f77

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeNameIdResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static TypeNameIdResolver construct(MapperConfig<?> config,
8282
@Override
8383
public String idFromValue(Object value)
8484
{
85-
Class<?> cls = value.getClass();
85+
Class<?> cls = _typeFactory.constructType(value.getClass()).getRawClass();
8686
final String key = cls.getName();
8787
String name;
8888
synchronized (_typeToId) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fasterxml.jackson.databind.module;
2+
3+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
4+
import com.fasterxml.jackson.databind.JavaType;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.type.TypeBindings;
7+
import com.fasterxml.jackson.databind.type.TypeFactory;
8+
import com.fasterxml.jackson.databind.type.TypeModifier;
9+
import com.fasterxml.jackson.test.BaseTest;
10+
11+
import java.lang.reflect.Type;
12+
13+
public class TestTypeModifierNameResolution extends BaseTest {
14+
15+
interface MyType {
16+
String getData();
17+
void setData(String data);
18+
}
19+
20+
static class MyTypeImpl implements MyType {
21+
private String data;
22+
23+
public String getData() {
24+
return data;
25+
}
26+
27+
public void setData(String data) {
28+
this.data = data;
29+
}
30+
}
31+
32+
static class CustomTypeModifier extends TypeModifier {
33+
@Override
34+
public JavaType modifyType(JavaType type, Type jdkType, TypeBindings context, TypeFactory typeFactory) {
35+
if (type.getRawClass().equals(MyTypeImpl.class)) {
36+
return typeFactory.constructType(MyType.class);
37+
}
38+
return type;
39+
}
40+
}
41+
42+
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.WRAPPER_OBJECT)
43+
public interface Mixin { }
44+
45+
// Expect that the TypeModifier kicks in when the type id is written.
46+
public void testTypeModiferNameResolution() throws Exception
47+
{
48+
ObjectMapper mapper = new ObjectMapper();
49+
mapper.setTypeFactory(mapper.getTypeFactory().withModifier(new CustomTypeModifier()));
50+
mapper.addMixInAnnotations(MyType.class, Mixin.class);
51+
52+
MyType obj = new MyTypeImpl();
53+
obj.setData("something");
54+
55+
String s = mapper.writer().writeValueAsString(obj);
56+
assertTrue(s.startsWith("{\"TestTypeModifierNameResolution$MyType\":"));
57+
}
58+
}

0 commit comments

Comments
 (0)