[JAVA-4966] Macro for sealed trait failing Created: 16/May/23  Updated: 28/Oct/23  Resolved: 06/Jun/23

Status: Closed
Project: Java Driver
Component/s: Scala
Affects Version/s: None
Fix Version/s: 4.10.0

Type: Bug Priority: Major - P3
Reporter: Istria Kévin Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes: Not Needed
Documentation Changes Summary:

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?


 Description   

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 
}



 Comments   
Comment by Ross Lawley [ 06/Jun/23 ]

Thanks istria.kevin@gmail.com for the report and the fix.

Comment by Githook User [ 06/Jun/23 ]

Author:

{'name': 'Kévin Istria', 'email': '35448966+kistria@users.noreply.github.com', 'username': 'kistria'}

Message: JAVA-4966 : Fix Macro for sealed trait (#1122)
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/84fddb061c4fbb5676ab4ddf0a6c4d2048186087

Comment by Cloud GitHub Webhooks [ 06/Jun/23 ]

rozza merged a pull request (JAVA-4966 Fix Macro for sealed trait) into the following branch:
master: 84fddb061c4fbb5676ab4ddf0a6c4d2048186087

Comment by Jeffrey Yemin [ 19/May/23 ]

Thanks for bringing this to our attention, istria.kevin@gmail.com. Since you seem to have found the root cause, feel free to open a pull request to fix this issue, and we can take a look.

Comment by PM Bot [ 16/May/23 ]

Hi istria.kevin@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Thu Feb 08 09:03:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.