Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4966

Macro for sealed trait failing

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.10.0
    • Affects Version/s: None
    • Component/s: Scala
    • None
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Summary

      The codec for a sealed trait using macro does not always work. If the sealed trait and the Macro are not defined is the same module, we have the following error `No known subclasses of the sealed trait`
      There is an issue in Scala describing the problem _https://github.com/scala/bug/issues/7755_

      How to Reproduce

      mongo-scala-driver 4.9.1

      scala 2.13.10

      Define a sealed trait in a module and use the Macro in a an another module.

      // Module 1
      sealed trait TestIssue
      case class Issue1(name: String) extends TestIssue
      case class Issue2(name: String, description: String) extends TestIssue

       

      // Module 2
      Macros.createCodecProviderIgnoreNone[TestIssue]()

       

       

       

      To fix this issue, we can change the method `isCaseClass` in `org.mongodb.scala.bson.codecs.macrocodecs.CaseClassCodec`:

      def isCaseClass(t: Type): Boolean = {
        val _ = t.typeSymbol.typeSignature
        t.typeSymbol.isClass && t.typeSymbol.asClass.isCaseClass && !t.typeSymbol.isModuleClass 
      }

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            istria.kevin@gmail.com Istria Kévin
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: