From b383c0b6d5b3ec0359d24c0ba97f5239b686dde3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Sat, 14 Feb 2026 15:23:42 +0100 Subject: [PATCH 1/3] test: Test the serialization of ObjectId --- .../commonTest/kotlin/BasicReadWriteTest.kt | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/test/src/commonTest/kotlin/BasicReadWriteTest.kt b/test/src/commonTest/kotlin/BasicReadWriteTest.kt index 33e80d2a..5996f1c0 100644 --- a/test/src/commonTest/kotlin/BasicReadWriteTest.kt +++ b/test/src/commonTest/kotlin/BasicReadWriteTest.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024-2025, OpenSavvy and contributors. + * Copyright (c) 2024-2026, OpenSavvy and contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,27 +14,35 @@ * limitations under the License. */ +@file:OptIn(ExperimentalTime::class, ExperimentalAtomicApi::class) + package opensavvy.ktmongo.sync -import kotlinx.serialization.Serializable +import opensavvy.ktmongo.bson.types.ObjectId import opensavvy.ktmongo.test.testCollection import opensavvy.prepared.runner.testballoon.preparedSuite import opensavvy.prepared.suite.config.CoroutineTimeout +import kotlin.concurrent.atomics.ExperimentalAtomicApi import kotlin.time.Duration.Companion.seconds +import kotlin.time.ExperimentalTime + +data class User( + val _id: ObjectId, + val name: String, + val age: Int, +) val BasicReadWriteTest by preparedSuite(preparedConfig = CoroutineTimeout(30.seconds)) { - @Serializable - data class User( - val name: String, - val age: Int, - ) + val id1 = ObjectId("69908384e10bb0a5f7d17c5b") + val id2 = ObjectId("699083ade6e89e315640258c") + val id3 = ObjectId("699083c9e493dd6c2e60c664") val users by testCollection("basic-users") test("Simple insert and read") { - users().insertOne(User(name = "Bob", age = 18)) + users().insertOne(User(_id = id1, name = "Bob", age = 18)) - check(User("Bob", age = 18) in users().find().toList()) + check(User(_id = id1, "Bob", age = 18) in users().find().toList()) } test("Simple upsert and read") { @@ -45,15 +53,16 @@ val BasicReadWriteTest by preparedSuite(preparedConfig = CoroutineTimeout(30.sec update = { User::name set "Bad" User::age setOnInsert 0 + User::_id setOnInsert id1 } ) - check(User("Bad", 0) in users().find().toList()) + check(User(_id = id1, "Bad", 0) in users().find().toList()) } test("Read ordered by age") { - val bob = User(name = "Bob", age = 18) - val alice = User(name = "Alice", age = 19) + val bob = User(_id = id1, name = "Bob", age = 18) + val alice = User(_id = id2, name = "Alice", age = 19) users().insertOne(bob) users().insertOne(alice) @@ -62,9 +71,9 @@ val BasicReadWriteTest by preparedSuite(preparedConfig = CoroutineTimeout(30.sec } test("Paging") { - val alice = User(name = "Alice", age = 22) - val bob = User(name = "Bob", age = 18) - val carol = User(name = "Carol", age = 19) + val alice = User(_id = id1, name = "Alice", age = 22) + val bob = User(_id = id2, name = "Bob", age = 18) + val carol = User(_id = id3, name = "Carol", age = 19) users().insertOne(alice) users().insertOne(bob) users().insertOne(carol) -- 2.51.2 From f9650e4206fa1b15741b93ce4123868d69315dbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Sun, 22 Feb 2026 16:19:18 +0100 Subject: [PATCH 2/3] fix(driver-coroutines): Fix the driver not using the KtMongo codecs --- driver-coroutines/src/jvmMain/kotlin/JvmMongoCollection.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/driver-coroutines/src/jvmMain/kotlin/JvmMongoCollection.kt b/driver-coroutines/src/jvmMain/kotlin/JvmMongoCollection.kt index db462925..5f86dbd5 100644 --- a/driver-coroutines/src/jvmMain/kotlin/JvmMongoCollection.kt +++ b/driver-coroutines/src/jvmMain/kotlin/JvmMongoCollection.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024-2025, OpenSavvy and contributors. + * Copyright (c) 2024-2026, OpenSavvy and contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,7 +54,7 @@ import java.util.concurrent.TimeUnit * To convert an existing MongoDB iterable into an instance of this class, see [asKtMongo]. */ class JvmMongoCollection internal constructor( - private val inner: com.mongodb.kotlin.client.coroutine.MongoCollection, + inner: com.mongodb.kotlin.client.coroutine.MongoCollection, nameStrategy: PropertyNameStrategy, ) : MongoCollection { @@ -68,6 +68,9 @@ class JvmMongoCollection internal constructor( nameStrategy = nameStrategy, ) + @OptIn(LowLevelApi::class) + private val inner = inner.withCodecRegistry(context.codecRegistry) + // region Find override fun find(): JvmMongoIterable = -- 2.51.2 From dc83855cb450db30bf27fd6e66e2eabe97e1670b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Sun, 22 Feb 2026 16:19:33 +0100 Subject: [PATCH 3/3] fix(driver-sync): Fix the driver not using the KtMongo codecs --- driver-sync/src/jvmMain/kotlin/JvmMongoCollection.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/driver-sync/src/jvmMain/kotlin/JvmMongoCollection.kt b/driver-sync/src/jvmMain/kotlin/JvmMongoCollection.kt index 78563e8d..09957ff2 100644 --- a/driver-sync/src/jvmMain/kotlin/JvmMongoCollection.kt +++ b/driver-sync/src/jvmMain/kotlin/JvmMongoCollection.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024-2025, OpenSavvy and contributors. + * Copyright (c) 2024-2026, OpenSavvy and contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -52,7 +52,7 @@ import java.util.concurrent.TimeUnit * To convert an existing MongoDB iterable into an instance of this class, see [asKtMongo]. */ class JvmMongoCollection internal constructor( - private val inner: com.mongodb.kotlin.client.MongoCollection, + inner: com.mongodb.kotlin.client.MongoCollection, nameStrategy: PropertyNameStrategy, ) : MongoCollection { @@ -66,6 +66,9 @@ class JvmMongoCollection internal constructor( nameStrategy = nameStrategy, ) + @OptIn(LowLevelApi::class) + private val inner = inner.withCodecRegistry(context.codecRegistry) + // region Find override fun find(): JvmMongoIterable = -- 2.51.2