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

Macro codec fails to decode a Map with a key matching a case class field

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 4.6.1
    • Affects Version/s: 4.6.0
    • Component/s: Codecs, Scala
    • Labels:
      None

      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)_
      

       

       

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            nic.wichmann@cloudator.com Nic Wichmann
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: