Skip to content

Incorrect deserialization of a kotlin class when loading it from mongodb #4733

@gasabr

Description

@gasabr

I'm sorry if this is the wrong place to file an issue, I've not found the right one in 10 minutes of googling.

Environment

  • spring: 3.0.4
  • kotlin 1.8.10
  • jvm 17
  • kotlin("plugin.spring") 1.8.10
  • mongo 6.0 local

Here is the class I'm trying to read from the database

@Document(collection = "users")
internal data class User(
    @Id
    val userId: UserId,
    val telegramChatId: Long,
)

here is the repository

@Repository
internal interface UserRepository : CrudRepository<User, String> {
    fun findByTelegramChatId(telegramChatId: Long): User?
}

Saving works just fine, but when I'm trying to load it from the database, I'm getting a weird error Parameter org.springframework.data.mapping.Parameter@a972a9cc does not have a name, inside of the org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator#extractInvocationArguments. I've debugged the code and noticed that the last parameter in the extractInvocationArguments method is null and it identifies a default constructor. So, I added PersistenceCreator to my class like this

    companion object {
        @JvmStatic
        @PersistenceCreator
        fun create(
            userId: UserId,
            telegramChatId: Long,
        ) = User(userId, telegramChatId)
    }

and everything works correctly now. The root cause of the problem lies in spring data adding an argument to the default class constructor, PersistenceCreator fixes it, because it is being parsed by the spring correctly. If needed I can debug once again and point to the functions there I think resolution is incorrect. The UserId is a value class, which has dedicated converter, but it works as expected.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions