Serialization for Nested Polymorphic Types with FasterXML/Jackson

Hi there,

I got into a serialization issue with nested polymorphic types as described below.

I have polymorphic types defined as below:

import com.fasterxml.jackson.annotation._

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT) 
class PyContextMetaType

case class Wpbp (sepInd: Int
                 , esgGrping: Int
                 , psaGrping: Int
                 , pernr: BigInt
                 , eeGrp: Char
                 , eeSubGrp: String
                 , perArea: String
                 , perSubArea: String
                 , orgUnit: String
                 , position: String
                 , costCenter: String) extends PyContextMetaType

When serializing Wpbp object, the JSON string reflects its sub-type correctly, because of the @JsonTypeInfo annotation:

{
  "Wpbp": {
    "sepInd": 1,
    "esgGrping": 3,
    "psaGrping": 3,
    "pernr": 28000001,
    "eeGrp": "\u0001",
    "eeSubGrp": "A0",
    "perArea": "CN01",
    "perSubArea": "SH01",
    "orgUnit": "HR Dept",
    "position": "",
    "costCenter": "CC1234"
  }
}

But now I have a more complex case class LogNode which uses PyContextMetaType as nested types:

case class LogNode(var text: String,

                   @JsonBackReference
                   dadNode: Option[AnyRef] = None,

                   var data: Option[(List[List[PyContextMetaType]],List[String],List[List[PyContextMetaType]])] = None,

                   @JsonManagedReference
                   children: ListBuffer[LogNode] = new ListBuffer[LogNode],
                   detailText: String = "") extends CborSerializable

Try to create a LogNode instance and serialize it:

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.{DefaultScalaModule, ScalaObjectMapper}

object Main3 {
  def main(args: Array[String]): Unit = {
    val wpbp = Wpbp(1,3,3,28000001,1,"A0","CN01","SH01","HR Dept","","CC1234")
    val wpbpNode = LogNode("WBPB",None,Some((Nil,Nil,List(List(wpbp)))))

    val mapper = new ObjectMapper() with ScalaObjectMapper
    mapper.registerModule(DefaultScalaModule)

    val jsonStr = mapper.writeValueAsString(wpbpNode)

    println(jsonStr)
  }

}

The serialized jsonString is like this:

{
  "text": "WBPB",
  "data": [
    [],
    [],
    [
      [
        {
          "sepInd": 1,
          "esgGrping": 3,
          "psaGrping": 3,
          "pernr": 28000001,
          "eeGrp": "\u0001",
          "eeSubGrp": "A0",
          "perArea": "CN01",
          "perSubArea": "SH01",
          "orgUnit": "HR Dept",
          "position": "",
          "costCenter": "CC1234"
        }
      ]
    ]
  ],
  "children": [],
  "detailText": ""
}

This is apparently wrong, because the type info of Wpbp gets lost during serialization. The wanted result of jsonStr should be:

{
  "text": "WBPB",
  "data": [
    [],
    [],
    [
      [
        {
          "Wpbp": {
            "sepInd": 1,
            "esgGrping": 3,
            "psaGrping": 3,
            "pernr": 28000001,
            "eeGrp": "\u0001",
            "eeSubGrp": "A0",
            "perArea": "CN01",
            "perSubArea": "SH01",
            "orgUnit": "HR Dept",
            "position": "",
            "costCenter": "CC1234"
          }
        }
      ]
    ]
  ],
  "children": [],
  "detailText": ""
}

Looks like the @JsonTypeInfo annotation marked on PyContextMetaType cannot work for LogNode serialization. How should I handle polymorphic type PyContextMetaType nested in LogNode? Do I need to implement custom serializer for LogNode for this?

Any tips will be appreciated.

Seems that this is an existing issue of jackson-module-scala: