From 67d192bb345061ca16ccd1f8823f75ed11f513dd Mon Sep 17 00:00:00 2001 From: Kiss Laszlo Date: Tue, 13 Jul 2021 13:13:18 +0200 Subject: [PATCH 1/2] handle 'id' property in model objects --- .../src/main/kotlin/org/litote/kmongo/util/MongoIdUtil.kt | 2 +- .../src/test/kotlin/org/litote/kmongo/jackson/UUIDTest.kt | 2 +- .../test/kotlin/org/litote/kmongo/util/MongoIdUtilTest.kt | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/kmongo-jackson-mapping/src/main/kotlin/org/litote/kmongo/util/MongoIdUtil.kt b/kmongo-jackson-mapping/src/main/kotlin/org/litote/kmongo/util/MongoIdUtil.kt index 2a46dec6..0c3ff2f9 100644 --- a/kmongo-jackson-mapping/src/main/kotlin/org/litote/kmongo/util/MongoIdUtil.kt +++ b/kmongo-jackson-mapping/src/main/kotlin/org/litote/kmongo/util/MongoIdUtil.kt @@ -79,7 +79,7 @@ internal object MongoIdUtil { private fun getIdProperty(type: KClass<*>): KProperty1<*, *>? = try { - type.memberProperties.find { "_id" == it.name } + type.memberProperties.find { "_id" == it.name || "id" == it.name } } catch (error: KotlinReflectionInternalError) { //ignore null diff --git a/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/jackson/UUIDTest.kt b/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/jackson/UUIDTest.kt index 1754f502..b6103810 100644 --- a/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/jackson/UUIDTest.kt +++ b/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/jackson/UUIDTest.kt @@ -63,7 +63,7 @@ class UUIDTest val bytes = mapper.writeValueAsBytes(testObject) // Check the type of the binary representation of the UUID - assertEquals(if(uuidRepresentation == UuidRepresentation.STANDARD) 4 else 3, bytes[12].toInt()) + assertEquals(if(uuidRepresentation == UuidRepresentation.STANDARD) 4 else 3, bytes[13].toInt()) val decodedObject: TestingObject = mapper.readValue(bytes) diff --git a/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/util/MongoIdUtilTest.kt b/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/util/MongoIdUtilTest.kt index b1bc0db1..c69876a5 100644 --- a/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/util/MongoIdUtilTest.kt +++ b/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/util/MongoIdUtilTest.kt @@ -72,5 +72,11 @@ class MongoIdUtilTest : KMongoRootTest() { assertEquals(Foo::class.memberProperties.first { it.name == "id" }, Foo::class.idProperty) } + data class TestObjWithId(val id : String?) + + @Test + fun extractIdWithoutUnderscore() { + assertEquals("id", KMongoUtil.extractId(TestObjWithId("id"), TestObjWithId::class)) + } } \ No newline at end of file From 76e3627577932f6e93fee0f37974731381ece796 Mon Sep 17 00:00:00 2001 From: Kiss Laszlo Date: Wed, 1 Sep 2021 12:36:17 +0200 Subject: [PATCH 2/2] add 'kmongo.id.enabled' system property --- .../org/litote/kmongo/util/MongoIdUtil.kt | 22 +++++++++---------- .../org/litote/kmongo/jackson/UUIDTest.kt | 2 +- .../org/litote/kmongo/util/MongoIdUtilTest.kt | 10 +++++++++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/kmongo-jackson-mapping/src/main/kotlin/org/litote/kmongo/util/MongoIdUtil.kt b/kmongo-jackson-mapping/src/main/kotlin/org/litote/kmongo/util/MongoIdUtil.kt index 0c3ff2f9..b434b94f 100644 --- a/kmongo-jackson-mapping/src/main/kotlin/org/litote/kmongo/util/MongoIdUtil.kt +++ b/kmongo-jackson-mapping/src/main/kotlin/org/litote/kmongo/util/MongoIdUtil.kt @@ -66,20 +66,21 @@ internal object MongoIdUtil { } private val propertyIdCache: MutableMap, IdPropertyWrapper> - by lazySoft { ConcurrentHashMap, IdPropertyWrapper>() } + by lazySoft { ConcurrentHashMap, IdPropertyWrapper>() } fun findIdProperty(type: KClass<*>): KProperty1<*, *>? = propertyIdCache.getOrPut(type) { (getAnnotatedMongoIdProperty(type) - ?: getIdProperty(type)) + ?: getIdProperty(type)) ?.let { IdPropertyWrapper.IdProperty(it) } - ?: NO_ID + ?: NO_ID }.property private fun getIdProperty(type: KClass<*>): KProperty1<*, *>? = try { - type.memberProperties.find { "_id" == it.name || "id" == it.name } + val idEnabled = System.getProperty("kmongo.id.enabled").toBoolean() + type.memberProperties.find { "_id" == it.name || (idEnabled && "id" == it.name) } } catch (error: KotlinReflectionInternalError) { //ignore null @@ -93,7 +94,7 @@ internal object MongoIdUtil { } else { type.memberProperties.find { p -> p.javaField?.isAnnotationPresent(BsonId::class.java) == true - || p.getter.javaMethod?.isAnnotationPresent(BsonId::class.java) == true + || p.getter.javaMethod?.isAnnotationPresent(BsonId::class.java) == true } } } catch (error: KotlinReflectionInternalError) { @@ -104,11 +105,11 @@ internal object MongoIdUtil { private fun findPrimaryConstructorParameter(type: KClass<*>): KParameter? = try { type.primaryConstructor?.parameters?.firstOrNull { it.findAnnotation() != null } - ?: type.superclasses - .asSequence() - .map { findPrimaryConstructorParameter(it) } - .filterNotNull() - .firstOrNull() + ?: type.superclasses + .asSequence() + .map { findPrimaryConstructorParameter(it) } + .filterNotNull() + .firstOrNull() } catch (error: KotlinReflectionInternalError) { //ignore null @@ -118,5 +119,4 @@ internal object MongoIdUtil { idProperty.isAccessible = true return idProperty.get(instance) } - } \ No newline at end of file diff --git a/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/jackson/UUIDTest.kt b/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/jackson/UUIDTest.kt index b6103810..1754f502 100644 --- a/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/jackson/UUIDTest.kt +++ b/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/jackson/UUIDTest.kt @@ -63,7 +63,7 @@ class UUIDTest val bytes = mapper.writeValueAsBytes(testObject) // Check the type of the binary representation of the UUID - assertEquals(if(uuidRepresentation == UuidRepresentation.STANDARD) 4 else 3, bytes[13].toInt()) + assertEquals(if(uuidRepresentation == UuidRepresentation.STANDARD) 4 else 3, bytes[12].toInt()) val decodedObject: TestingObject = mapper.readValue(bytes) diff --git a/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/util/MongoIdUtilTest.kt b/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/util/MongoIdUtilTest.kt index c69876a5..1f047d99 100644 --- a/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/util/MongoIdUtilTest.kt +++ b/kmongo-jackson-mapping/src/test/kotlin/org/litote/kmongo/util/MongoIdUtilTest.kt @@ -67,6 +67,13 @@ class MongoIdUtilTest : KMongoRootTest() { assertEquals(id, KMongoUtil.extractId(Obj(id), Obj::class)) } + @Test + fun extractIdIfIdNotEnabled() { + System.setProperty("kmongo.id.enabled", "false") + val id = ObjectId() + assertEquals(id, KMongoUtil.extractId(Obj(id), Obj::class)) + } + @Test fun `id property is detected even for java classes`() { assertEquals(Foo::class.memberProperties.first { it.name == "id" }, Foo::class.idProperty) @@ -76,7 +83,10 @@ class MongoIdUtilTest : KMongoRootTest() { @Test fun extractIdWithoutUnderscore() { + System.setProperty("kmongo.id.enabled", "true") assertEquals("id", KMongoUtil.extractId(TestObjWithId("id"), TestObjWithId::class)) + System.setProperty("kmongo.id.enabled", "false") } + } \ No newline at end of file