Skip to content

Commit

Permalink
Merge branch 'fix_id_handling' of https://github.com/lace00/kmongo in…
Browse files Browse the repository at this point in the history
…to lace00-fix_id_handling
  • Loading branch information
zigzago committed Sep 11, 2021
2 parents e7af1d6 + 76e3627 commit 1b50492
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,21 @@ internal object MongoIdUtil {
}

private val propertyIdCache: MutableMap<KClass<*>, IdPropertyWrapper>
by lazySoft { ConcurrentHashMap<KClass<*>, IdPropertyWrapper>() }
by lazySoft { ConcurrentHashMap<KClass<*>, 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 }
val idEnabled = System.getProperty("kmongo.id.enabled").toBoolean()
type.memberProperties.find { "_id" == it.name || (idEnabled && "id" == it.name) }
} catch (error: KotlinReflectionInternalError) {
//ignore
null
Expand All @@ -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) {
Expand All @@ -104,11 +105,11 @@ internal object MongoIdUtil {
private fun findPrimaryConstructorParameter(type: KClass<*>): KParameter? =
try {
type.primaryConstructor?.parameters?.firstOrNull { it.findAnnotation<BsonId>() != null }
?: type.superclasses
.asSequence()
.map { findPrimaryConstructorParameter(it) }
.filterNotNull()
.firstOrNull()
?: type.superclasses
.asSequence()
.map { findPrimaryConstructorParameter(it) }
.filterNotNull()
.firstOrNull()
} catch (error: KotlinReflectionInternalError) {
//ignore
null
Expand All @@ -118,5 +119,4 @@ internal object MongoIdUtil {
idProperty.isAccessible = true
return idProperty.get(instance)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,26 @@ 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)
}

data class TestObjWithId(val id : String?)

@Test
fun extractIdWithoutUnderscore() {
System.setProperty("kmongo.id.enabled", "true")
assertEquals("id", KMongoUtil.extractId(TestObjWithId("id"), TestObjWithId::class))
System.setProperty("kmongo.id.enabled", "false")
}


}

0 comments on commit 1b50492

Please sign in to comment.