[JAVA-4611] Macro codec fails to decode a Map with a key matching a case class field Created: 13/May/22  Updated: 28/Oct/23  Resolved: 23/May/22

Status: Closed
Project: Java Driver
Component/s: Codecs, Scala
Affects Version/s: 4.6.0
Fix Version/s: 4.6.1

Type: Bug Priority: Minor - P4
Reporter: Nic Wichmann Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

The codec for a case class generated using a macro is unable to decode a case class containing a Map where a key in the Map is the same as a field in the case class and the type of the values are not the same. For example the case class field is an integer and the map field is a string. This currently fails with an exception. The expected outcome is that decoding works. See below example.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

Java 11

Scala 2.12.15

mongo-scala-driver 4.6.0

mongodb-driver-sync 4.6.0

How to Reproduce

 

import org.bson.codecs.configuration.CodecRegistries.fromProviders
import org.bson.codecs.DecoderContext
import org.bson.json.JsonReader
import org.mongodb.scala.MongoClient.DEFAULT_CODEC_REGISTRY
import org.mongodb.scala.bson.codecs.Macros._
 
case class Model(field: Int, map: Map[String, String])
 
object Reproducer extends App {
 
  val registry = fromProviders(
    createCodecProvider[Model],
    DEFAULT_CODEC_REGISTRY
  )
 
  val json =
    """{
      |  "field": 1,
      |  "map": {
      |    "field": "value"
      |  }
      |}""".stripMargin
 
  print(registry.get(classOf[Model], registry).decode(new JsonReader(json), DecoderContext.builder.build))
 
} 

 

Additional Background

The above code results in the exception:

 

_Exception in thread "main" org.bson.BsonInvalidOperationException: Invalid numeric type, found: STRING     at org.bson.codecs.NumberCodecHelper.decodeInt(NumberCodecHelper.java:59)     at org.bson.codecs.IntegerCodec.decode(IntegerCodec.java:38)     at org.bson.codecs.IntegerCodec.decode(IntegerCodec.java:29)     at org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec.readValue(MacroCodec.scala:193)     at org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec.readValue$(MacroCodec.scala:179)     at Reproducer$$anon$1$ModelMacroCodec$1.readValue(Reproducer.scala:12)     at org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec.readDocument(MacroCodec.scala:250)     at org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec.readDocument$(MacroCodec.scala:227)     at Reproducer$$anon$1$ModelMacroCodec$1.readDocument(Reproducer.scala:12)     at org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec.readValue(MacroCodec.scala:188)     at org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec.readValue$(MacroCodec.scala:179)     at Reproducer$$anon$1$ModelMacroCodec$1.readValue(Reproducer.scala:12)     at org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec.decode(MacroCodec.scala:107)     at org.mongodb.scala.bson.codecs.macrocodecs.MacroCodec.decode$(MacroCodec.scala:96)     at Reproducer$$anon$1$ModelMacroCodec$1.decode(Reproducer.scala:12)     at Reproducer$.delayedEndpoint$Reproducer$1(Reproducer.scala:24)     at Reproducer$delayedInit$body.apply(Reproducer.scala:9)     at scala.Function0.apply$mcV$sp(Function0.scala:39)     at scala.Function0.apply$mcV$sp$(Function0.scala:39)     at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)     at scala.App.$anonfun$main$1$adapted(App.scala:80)     at scala.collection.immutable.List.foreach(List.scala:431)     at scala.App.main(App.scala:80)     at scala.App.main$(App.scala:78)     at Reproducer$.main(Reproducer.scala:9)     at Reproducer.main(Reproducer.scala)_

 

 



 Comments   
Comment by Githook User [ 23/May/22 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}

Message: Fix Scala case class macro codec lookup bug (#945)

Fixes an issue when determining the codec to use for a
case class that contains a Map type that has a value that
contains the same key name and field name.

JAVA-4611
Branch: 4.6.x
https://github.com/mongodb/mongo-java-driver/commit/608154ce1dd6894dca81b6935ff0275f459b3742

Comment by Githook User [ 23/May/22 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}

Message: Fix Scala case class macro codec lookup bug (#945)

Fixes an issue when determining the codec to use for a
case class that contains a Map type that has a value that
contains the same key name and field name.

JAVA-4611
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/38e87c21595e9ad851b2387b51cf4e8d0745443b

Comment by Ross Lawley [ 20/May/22 ]

Thanks nic.wichmann@cloudator.com,

For the ticket and the code reproducing this issue a fix will be released in 4.6.1

Ross

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