Skip to content
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

Kotlin constructor detection fails during KType derivation #3041

Closed
huberchrigu opened this issue Feb 16, 2024 · 1 comment
Closed

Kotlin constructor detection fails during KType derivation #3041

huberchrigu opened this issue Feb 16, 2024 · 1 comment
Assignees
Labels
in: kotlin Kotlin support type: bug A general bug

Comments

@huberchrigu
Copy link

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 16, 2024
@mp911de mp911de added type: bug A general bug in: kotlin Kotlin support and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 20, 2024
@christophstrobl
Copy link
Member

Thank you @huberchrigu. As yo mentioned the issue stems from the attempt to look up constructor parameter types that have their generic type signature erased. We're investigating if we can fill up missing args with KTypeProjection.star when calling Reflection.typeOf.

mp911de pushed a commit that referenced this issue Feb 21, 2024

Verified

This commit was signed with the committer’s verified signature.
mp911de Mark Paluch
We now fill up missing KTypeProjection arguments with star because the Kotlin Reflection.typeOf resolution fails if arguments are not provided.

Closes #3041
Original pull request: #3048
mp911de added a commit that referenced this issue Feb 21, 2024

Verified

This commit was signed with the committer’s verified signature.
mp911de Mark Paluch
Tiny naming tweaks.

See #3041
Original pull request: #3048
mp911de added a commit that referenced this issue Feb 21, 2024

Verified

This commit was signed with the committer’s verified signature.
mp911de Mark Paluch
Tiny naming tweaks.

See #3041
Original pull request: #3048
@mp911de mp911de changed the title Spring Data Mapping fails on multiple synthetic Kotlin constructors with same argument count Kotlin constructor detection fails during KType derivation Feb 21, 2024
@mp911de mp911de added this to the 3.2.4 (2023.1.4) milestone Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: kotlin Kotlin support type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants