Uploaded image for project: 'Johnzon'
  1. Johnzon
  2. JOHNZON-402

Polymorphism attributes not serialized if @JsonbTypeInfo is not attached to direct parent

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 2.0.0
    • 2.0.1
    • JSON-B
    • None

    Description

      After thinking through the polymorphism code again for JONZON-401 I noticed an issue where polymorphism properties aren't taken into account if the @JsonbTypeInfo annotation is not attached to a direct parent of the serialized class.

      Take this example test:

      public class JsonbMultiLevelPolymorphismTest {
          @Rule
          public JsonbRule jsonb = new JsonbRule();
      
          @Test
          public void test() {
              ConcreteSomething info = new ConcreteSomething();
      
              assertEquals("{\"@type\":\"concrete\"}", jsonb.toJson(info));
          }
      
          @JsonbTypeInfo(
                  @JsonbSubtype(alias = "concrete", type = ConcreteSomething.class)
          )
          public static abstract class AbstractTopLevelSomething { }
          public static abstract class AbstractMiddleLevelSomething extends AbstractTopLevelSomething { }
          public static class ConcreteSomething extends AbstractMiddleLevelSomething { }
      } 

      Test result:

      org.junit.ComparisonFailure: 
      Expected :{"@type":"concrete"}
      Actual   :{} 

       

      The spec doesn't mention that @JsonbTypeInfo has to be attached to a direct parent. This also works perfectly fine in yasson, so I see no reason why it should not in johnzon.

      Attachments

        Activity

          People

            markus-jung Markus Jung
            markus-jung Markus Jung
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 20m
                20m