Can't find a codec for class with generics

I have typed inline value class for typed identification…

value class Id<T>(
    val value: ObjectId = ObjectId()

I use this class on all my domain models…

data class Test(
    override var _id: Id<Test> = Id(),
    val value: String,

data class Status(
    override var _id: Id<Status> = Id(),
    var test_id: Id<Test>,
    val value: String


The database seeding goes well everything works as expected, I see documents in the right form… But when I’m trying to read from database it creates this error…

Can't find a codec for CodecCacheKey{clazz=class Id, types=null}.

Does anyone have a clue how to write Codec for typed value classes because I have no idea?

Yeeesssss! I have Solved it…

You have to define codec class for your generic class and use star operator on generic type…

class IdCodec : Codec<Id<*>> {
    override fun encode(writer: BsonWriter, value: Id<*>, encoderContext: EncoderContext) {
        return writer.writeObjectId(value.value)

    override fun decode(reader: BsonReader, decoderContext: DecoderContext): Id<*> {
        return Id<Any>(value = reader.readObjectId())

    override fun getEncoderClass(): Class<Id<*>> =

Then register this codec on created database instance…

    val idCodecRegistry = CodecRegistries.fromCodecs(IdCodec())
    var codecRegistry = CodecRegistries.fromRegistries(
    val db = mongoClient.getDatabase(db_name).withCodecRegistry(codecRegistry)