Using Jackson 2.9.6, i came across the following, which to me looks like a 
bug. Abstract superclass A has two subclasses, B and C, where B again has a 
subclass D

Adding the *@JsonTypeInfo* and *@JsonSubTypes* annotations as shown below, 
I would expect that B,C and D should all be serialized with a *type=name 
discriminator* field, but it appears that B, which "sits in the middle" of 
the object hierarchy is instead serialized using the simple class name as 
the type value.


JsonTypeInfo(use = NAME, include = PROPERTY, property = "type")
@JsonSubTypes({
        @Type(value = B.class, name = "nameB"),
        @Type(value = C.class, name = "nameC"),

})
abstract class A {
    private final int i;

    public int getI() {
        return i;
    }

    public A(int i) {
        this.i = i;
    }

}

@JsonTypeInfo(use = NAME, include = PROPERTY, property = "type")
@JsonSubTypes({
        @Type(value = D.class, name = "nameD"),

})
class B extends A {

    public B(int i) {
        super(i);
    }

}

class C extends A {

    public C(int i) {
        super(i);
    }

}

class D extends B {

    public D(int i) {
        super(i);
    }

}


Reproduce with the following test:


Test
public void testA() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new
 B(42)));
    
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new
 C(42)));
    
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new
 D(42)));

} 


which yields :


{
  "type" : "B",
  "i" : 42
}
{
  "type" : "nameC",
  "i" : 42
}
{
  "type" : "nameD",
  "i" : 42
}




-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to