spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

Support for Kotlin's value class as document's @Id

Open gavvvr opened this issue 3 years ago • 2 comments

Hi. I think it's a good idea to use Kotlin's inline class to enforce a restriction at the compile time and prevent yourself from mixing up an id of one entity with id on another entity.

Usually the document class looks like this:

@Document
data class Person(@Id var id: String? = null, val name: String)

And you can pass any String to repo's findById method. I would like to use an inline class for @Id as follows:

@JvmInline
value class PersonId(val value: String)

@Document
data class Person(@Id var id: PersonId? = null, val name: String)

The problem is that it's not fully supported. The repository's save operation works fine (as far as I can see by inspecting my local Mongo instance), but, for example, findAll and findByIdOrNull fail to act as expected in different manners.

Below is an example source code, which can be put into a single test file of a project scaffolded with start.spring.io:

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Repository

@JvmInline
value class PersonId(val value: String)

@Document
data class Person(@Id var id: PersonId? = null, val name: String)

@Repository
interface PersonRepository : MongoRepository<Person, PersonId>

@DataMongoTest
class PersonRepositoryTest(@Autowired private val repo: PersonRepository) {

    @Test
    fun `successfully saves person`() {
        val newPerson = Person(name = "Bob")
        val savedPerson = repo.insert(newPerson)
        assertThat(savedPerson.id).isNotNull
    }

    @Nested
    inner class WhenPersonSavedToRepository {

        private var id: PersonId? = null

        @BeforeEach
        fun persistNewPerson() {
            val newPerson = Person(name = "Bob")
            val savedPerson = repo.save(newPerson)
            id = savedPerson.id
        }

        @Test
        fun `finds all persons in the repository (unfortunately throws MappingException)`() {
            val all = repo.findAll()
            assertThat(all).isNotEmpty
        }

        @Test
        fun `finds person by id (unfortunately returns nothing)`() {
            val found = repo.findByIdOrNull(id!!)
            assertThat(found).isNotNull
        }
    }
}

gavvvr avatar Jun 05 '22 05:06 gavvvr

Depends on spring-projects/spring-data-commons#1947

mp911de avatar Jun 14 '22 07:06 mp911de