Skip to content

Kotlin constructor detection fails during KType derivation #3041

Closed
@huberchrigu

Description

@huberchrigu

When there is a Kotlin class with multiple constructors that all have default values, there might occur a bug since the latest release (it used to work in Spring Boot 3.1.6, but does not in Spring 3.2.2).

Example
Repository: https://github.com/huberchrigu/spring-data-kotlin-test
I have a MongoDB document

data class TestDocument(val id: ObjectId?, val notes: Map<String, String> = emptyMap()) {
    constructor(notes: Map<String, String>, additionalNotes: Map<String, String> = emptyMap()) : this(null, notes + additionalNotes)
}

Kotlin generates two synthetic constructors with the same parameter count. Hence, Spring Data compares detected constructors with synthetic constructors that may have different types, leading to the error java.lang.IllegalArgumentException: Class declares 2 type parameters, but 0 were provided.

Problem
In my oppinion, the problem is in the class org.springframework.data.mapping.model.KotlinInstantiationDelegate, line 212:

if ((detectedConstructor.getParameterCount() + syntheticParameters) != candidate.getParameterCount())

This line skips the comparison between the detected and the candidate constructor, if the parameter count is not equal. In my example, this does not prevent the comparison of ObjectId and Map<String,String>, which then leads to the described error when calling Reflection.typeOf(kotlinClass) for the map without generics.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions