bson-kotlin does not support optional arguments

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: 5.6.2
    • Component/s: BSON, Kotlin
    • None
    • None
    • Java Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

      I'm trying to deserialize the following Kotlin class:

      {{{}data class BsonDocumentUser(
          val a: String = "Bob",
          val b: Int,
      {}}})

      with the document

      {{

      {"b": 45}

      }}

      which fails with

      Caused by: org.bson.codecs.configuration.CodecConfigurationException: Unable to invoke primary constructor of BsonDocumentUser data class
      at org.bson.codecs.kotlin.DataClassCodec.decode(DataClassCodec.kt:106)
      ... 301 more
      Caused by: java.lang.reflect.InvocationTargetException
      at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
      at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
      at kotlin.reflect.jvm.internal.calls.CallerImpl$Constructor.call(CallerImpl.kt:41)
      at kotlin.reflect.jvm.internal.DescriptorKCallable.callDefaultMethod(DescriptorKCallable.kt:240)
      at kotlin.reflect.jvm.internal.DescriptorKCallable.callBy(DescriptorKCallable.kt:145)
      at org.bson.codecs.kotlin.DataClassCodec.decode(DataClassCodec.kt:104)
      ... 302 more
      Caused by: java.lang.NullPointerException: Parameter specified as non-null is null: method opensavvy.ktmongo.bson.BsonDocumentUser.<init>, parameter a
      at opensavvy.ktmongo.bson.BsonDocumentUser.<init>(BsonDocumentTests.kt)
      ... 308 more

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

      5.6.2

      Additional Background

      The problem seems to be in

      org.bson.codecs.kotlin.DataClassCodec#decode

      The call is:

      primaryConstructor.callBy(args)

      where args contains all parameters, with a null value if the parameter is not found in the document.

      If the argument was not present at all in the map, kotlin-reflect would substitute it by the default value.

       

            Assignee:
            Ross Lawley
            Reporter:
            Ivan Canet
            Slav Babanin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: