aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicWithDefaultImpl1565.java
blob: 26e7286da4d80771aeea39ada7fcd706e7c696e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.fasterxml.jackson.databind.jsontype;

import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.databind.*;

public class TestPolymorphicWithDefaultImpl1565 extends BaseMapTest
{
    // [databind#1565]
    @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY,
        property="typeInfo",  defaultImpl = CBaseClass1565.class)
    @JsonSubTypes({
        @JsonSubTypes.Type(CDerived1565.class)
    })
    public static interface CTestInterface1565
    {
         public String getName();
         public void setName(String name);
         public String getTypeInfo();
    }

    static class CBaseClass1565 implements CTestInterface1565
    {
         private String mName;
         
         @Override
         public String getName() {
              return(mName);
         }

         @Override
         public void setName(String name) {
              mName = name;
         }

         @Override
         public String getTypeInfo() {
              return "base";
         }
    }

    @JsonTypeName("derived")
    static class CDerived1565 extends CBaseClass1565
    {
         public String description;

         @Override
         public String getTypeInfo() {
              return "derived";
         }
    }

    // [databind#1861]
    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", defaultImpl = DefaultImpl1861.class)
    @JsonSubTypes({
            @JsonSubTypes.Type(name = "a", value = Impl1861A.class)
    })
    static abstract class Bean1861 {
        public String base;
    }

    static class DefaultImpl1861 extends Bean1861 {
        public int id;
    }

    static class Impl1861A extends Bean1861 {
        public int valueA;
    }

    /*
    /**********************************************************************
    /* Test methods
    /**********************************************************************
     */

    private final ObjectMapper MAPPER = new ObjectMapper();

    // [databind#1565]
    public void testIncompatibleDefaultImpl1565() throws Exception
    {
        String value = "{\"typeInfo\": \"derived\", \"name\": \"John\", \"description\": \"Owner\"}";
        CDerived1565 result = MAPPER.readValue(value, CDerived1565.class);
        assertNotNull(result);
    }

    // [databind#1861]
    public void testWithIncompatibleTargetType1861() throws Exception
    {
        // Should allow deserialization even if `defaultImpl` incompatible
        Impl1861A result = MAPPER.readValue(aposToQuotes("{'type':'a','base':'foo','valueA':3}"),
                Impl1861A.class);
        assertNotNull(result);
    }
}