On Fri, Oct 12, 2018 at 7:12 AM Jan-Olav Eide <janolave...@gmail.com> 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 jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to