Skip to content

Ensure custom KProperty include the name in the hashcode #1710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public fun <T> KProperty<T>.path(): String {
return if (this is KPropertyPath<*, T>) {
this.name
} else {
pathCache.computeIfAbsent(this.toString()) {

pathCache.computeIfAbsent("${this.javaClass.getSimpleName()}@${Integer.toHexString(hashCode())}") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Can't we just refactor the map to

private val pathCache: MutableMap<Int, String> by lazySoft { ConcurrentHashMap<Int, String>() }

then use the hashCode

pathCache.computeIfAbsent(this.hashCode())

not sure why you concatenate the class name with the hash?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose I followed the previous logic for the key.

Hashcodes alone don't provide enough protection from collisions:

    data class Name(val name: String)
    data class School(val house: String)

    @Test
    fun testToString() {
        assertEquals(Name("Merlin").hashCode(), School("Merlin").hashCode())
    }

In this case they appear to - I tested up to 2 million for naming collisions but I still have some concern that it could also cause an issue. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hashcode is calculated for the KProperty1 so it will only match if you're using the same KProperty1

data class Name(val name: String)
data class School(val name: String)

val p1: KProperty1<Name, String> = Name::name
val p2: KProperty1<School, String> = School::name

println(p1.hashCode())            // 2000641095
println(Name::name.hashCode())    // 2000641095
println(School::name.hashCode())  // -1039559637

// Check serial name - Note kotlinx.serialization.SerialName may not be on the class
// path
val serialName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.mongodb.kotlin.client.property

import com.mongodb.annotations.Sealed
import com.mongodb.kotlin.client.model.path
import java.util.Objects
import kotlin.reflect.KParameter
import kotlin.reflect.KProperty1
import kotlin.reflect.KType
Expand Down Expand Up @@ -84,6 +85,15 @@ public open class KPropertyPath<T, R>(
override fun callBy(args: Map<KParameter, Any?>): R = unSupportedOperation()
override fun get(receiver: T): R = unSupportedOperation()
override fun getDelegate(receiver: T): Any? = unSupportedOperation()
override fun hashCode(): Int = Objects.hash(previous, property, name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea to provide the hashCode and equals but these weren't involved in the code path since we return the name directly if the instance is KPropertyPath in

return if (this is KPropertyPath<*, T>) {
        this.name

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used for the direct path, but the hashcode for CustomProperty relies on the hashcode.

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as KPropertyPath<*, *>
return Objects.equals(previous, other.previous) &&
Objects.equals(property, other.property) &&
Objects.equals(name, other.name)
}

public companion object {

Expand Down Expand Up @@ -121,6 +131,13 @@ public open class KPropertyPath<T, R>(
override fun get(receiver: T): R = unSupportedOperation()
override fun getDelegate(receiver: T): Any? = unSupportedOperation()
override fun invoke(p1: T): R = unSupportedOperation()
override fun hashCode(): Int = Objects.hash(previous, name)
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as CustomProperty<*, *>
return Objects.equals(previous, other.previous) && Objects.equals(name, other.name)
}
}

/** Provides "fake" property with custom name. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,18 @@ class KPropertiesTest {
assertThrows<UnsupportedOperationException> { property.get(restaurant) }
assertThrows<UnsupportedOperationException> { property.getDelegate(restaurant) }
}

@Test
fun testNoCacheCollisions() {
for (i in 1.rangeTo(25_000)) {
assertEquals("reviews.$i", Restaurant::reviews.pos(i).path())
assertEquals("reviews.$[identifier$i]", Restaurant::reviews.filteredPosOp("identifier$i").path())
assertEquals("localeMap.$i", Restaurant::localeMap.keyProjection(i).path())

val x = i / 2
assertEquals("reviews.$[identifier$x].rating", (Restaurant::reviews.filteredPosOp("identifier$x") / Review::score).path())
assertEquals("reviews.$x.rating", (Restaurant::reviews.pos(x) / Review::score).path())
assertEquals("localeMap.$x.rating", (Restaurant::localeMap.keyProjection(x) / Review::score).path())
}
}
}