[JAVA-4326] codecs for traits (not sealed traits) Created: 29/Sep/21  Updated: 04/May/22

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

Type: Improvement Priority: Unknown
Reporter: Andy Lii Assignee: Ross Lawley
Resolution: Unresolved Votes: 0
Labels: external-user
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Is it possible to use Macro to derive codecs for traits (not sealed traits)? If not, how do I make one myself?

Suppose the following:

trait Foo
 
case class Bar(foos: List[Foo])
 
case class Foo1(int: Int) extends Foo
 
case class Foo2(string: String) extends Foo
 
val bar = Bar(List(Foo1(42), Foo2("foo2")))

If I register the registry and codec like this:

val registry = fromRegistries(fromProviders(classOf[Bar], classOf[Foo], classOf[Foo1], classOf[Foo2]), DEFAULT_MONGO_REGISTRY)
val codec = registry.get(classOf[Bar])

Then I am able to encode (i.e. turn bar into a BsonDocument, but not decode - it will fail with next on empty iterator on FooMacroCodec$1.getClassName.

Is there a way to do this? I've simplified my code a lot, in reality I simply cannot turn Foo into a sealed trait.

Thanks in advance.



 Comments   
Comment by Ross Lawley [ 12/Oct/21 ]

Hi andylii@mobagel.com,

Thanks for the ticket. I think this should be supported as it is in the Java driver for POJOs. This will be scheduled for a future fix.

Ross

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