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