Problem with kotlin driver

Hello,
I’m trying to migrate from KMongo to the Kotlin driver, but I encountered an error with the codec: ‘Codec for id must implement RepresentationConfigurable to support BsonRepresentation’.

This is my MongoClient Service :

class MongoClientService {

    val logger: Logger = LoggerFactory.getLogger(MongoClientService::class.java)

    val mongoClient: MongoClient

    init {
        val codec = fromRegistries(
            MongoClientSettings.getDefaultCodecRegistry(),
            fromProviders(PojoCodecProvider.builder().automatic(true).build())
        )
        val settings = MongoClientSettings.builder()
            .applyConnectionString(ConnectionString(AppConfig.APPLICATION_MONGODB_URI!!))
            .codecRegistry(codec)
            .build()
        mongoClient = create(settings)
    }
}

and my model

data class User(
    @BsonId
    @BsonRepresentation(BsonType.OBJECT_ID)
    val id: ObjectId = ObjectId(),
    var email: String,
    var firstName: String,
    var lastName: String,
    var password: String,
    var archived: Boolean = false,
    val tenants: List<Tenant> = listOf(),
    var language: Language = Language.EN,
    val currentTenant: Tenant? = null,
    val avatar: Avatar? = null,
    val darkMode: Boolean = false,
    val mode: AppMode = AppMode.EMPLOYEE,
    val superAdmin: Boolean = false,
    val entity: EntityEmbedded = EntityEmbedded(),
)

Does anyone have an idea ?

Hmm interesting,

I don’t think you need @BsonRepresentation(BsonType.OBJECT_ID) if you are using ObjectId as the the _id type.

The PojoCodec also isnt needed for Kotlin Data classes - just make sure to add Maven Central: org.mongodb:bson-kotlin:4.10.2 to the path and it will automatically be picked up by the default registry (same goes for bson-kotlinx).

I hope that helps,

Ross

Hi!

I have the same problem. I tried different variations of id as String/ObjectId/BsonObjectId, without any annotations, with just @BsonId or @BsonRepresentation. Nothing works.
Driver version 5.1.0.
With KMongo 5.1.0 and id as ObjectId and annotation @BsonProperty all works fine.
What am I doing wrong?

Fixed the issue. Removed redundant annotations and now all works.