Decoding a class with type parameters

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Cannot Reproduce
    • Priority: Minor - P4
    • None
    • Affects Version/s: 5.6.2
    • Component/s: BSON
    • None
    • None
    • Java Drivers
    • Hide

      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?

      Show
      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?
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

      I want to deserialize a Kotlin class with type parameters, using the `org.mongodb:bson-kotlin` (reflection-based) driver.

      My understanding is that I should call the `CodecProvider.get(Class<T>, List<Type>)` overload to specify the type parameters; since they are removed at compile-time, the reflection-based implementation cannot figure them out by itself.

      I therefore have the following type:

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

      data class BsonDocumentTypeParameter<T>(
          val value: T,
      )

      codecRegistry.get(BsonDocumentTypeParameter::class, listOf(BsonDocumentUser::class))

      However, this does not return the codec.

      Looking at the implementation:

      • In `org.bson.internal.ProvidersCodecRegistry#get(java.lang.Class<T>, java.util.List<java.lang.reflect.Type>)`, a new ChildCodecRegistry is created, but does nothing
      • In `org.bson.internal.ProvidersCodecRegistry#get(org.bson.internal.ChildCodecRegistry<T>)`, a new CodecCacheKey is created.
      • Immediately, `org.bson.internal.CodecCache#get` is called on the new cache key.

      At this point, `org.bson.internal.CodecCache#putIfAbsent` has not been called, so of course the codec cache returns null.

      Am I supposed to register the type somewhere so the codec cache is populated in advance? Is there something else I'm doing wrong?

      Versions

      • Java driver 5.6.2
      • The MongoDB server version & topology should not impact this; I'm using 7.0.31 standalone

       

            Assignee:
            Nabil Hachicha
            Reporter:
            Ivan Canet (EXT)
            None
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: