I was not aware that adding @JsonTypeInfo at the top level was enough, so in my case, if I add the D class to that annotation, things work as expected. But I guess it still is a bug if one wants to override typeinfo somewhere in subclasses. For example, specifying in B that D should instead use MINIMAL_CLASS, this also affects B, which will also use MINIMAL_CLASS
I will file a bug using this more realistic example as a reproducing case. lørdag 13. oktober 2018 23.26.31 UTC+2 skrev Tatu Saloranta følgende: > > On Fri, Oct 12, 2018 at 7:12 AM Jan-Olav Eide <[email protected] > <javascript:>> wrote: > > > > 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 > > } > > That does look like a bug: `type` for `B` should be `nameB`, and not > "default" of class name. > > As a general rule it is advisable to only add `@JsonTypeInfo` on one > supertype (here `A`), but since information > here is identical across levels, that is not problematic, just > unnecessary. > Use of chained @JsonSubTypes is supported and should work. > > So, could you please file a bug with minimal reproduction (can cut'n > paste example)? > > -+ Tatu +- > -- 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.
