cancel
Showing results for 
Search instead for 
Did you mean: 

Spring Data Rest, Kotlin & Enum

wayne
Node Clone

Greetings all,
I seem to be having some difficulties with Spring Data Rest.

Setup
Spring Boot 2.2.5.RELEASE
Kotlin 1.3.61

So as an example, I have an Orangutan NodeEntity

@NodeEntity
data class Orangutan constructor(
    @Id @GeneratedValue val id: Long? = null,
    val names:String,
    val speciesEnum: SpeciesEnum
)

with a SpeciesEnum of

enum class SpeciesEnum {
    BORNEAN, SUMATRAN
}

and a simple repository of

@Repository
interface OrangutanRepository : Neo4jRepository<Orangutan, Long>

During testing

@ExtendWith(SpringExtension::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Transactional
class OrangutanTest @Autowired constructor(
    val orangutanRepository: OrangutanRepository,
    val session: Session
) {
    val mrK:Orangutan = Orangutan (speciesEnum=SpeciesEnum.BORNEAN,names = "MrK")

    @BeforeEach
    fun beforeEach() {
        session.purgeDatabase()
        orangutanRepository.save<Orangutan>(mrK)
        session.clear()
    }

    @Test
    fun findById() {
        val orangutanRead: Orangutan = orangutanRepository.findById(mrK.id!!).orElseThrow()
        assertEquals(mrK, orangutanRead)
    }
}

I can get an error from the first line of the findById function, of

Error mapping GraphModel; nested exception is org.neo4j.ogm.exception.core.MappingException: Error mapping GraphModel
org.springframework.dao.InvalidDataAccessApiUsageException: Error mapping GraphModel; nested exception is org.neo4j.ogm.exception.core.MappingException: Error mapping GraphModel

caused indirectly by

Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [aoni.kotlinenum.demo.SpeciesEnum]

This error can be 'resolved' by taking my build.gradle.kts dependencies

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-data-neo4j")
    // remove line below to resolve issue
    implementation("org.springframework.boot:spring-boot-starter-data-rest")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
}

and removing the spring-boot-starter-data-rest implementation.

Naturally, I would rather not have to remove this.

So, are Kotlin enum conversions supported, like Java, or do I need to add my own converters ?
Or, am I missing something in the bigger picture ?
As a side note, I ran into a similar with Kotlin Int, which may well be related.

Thanks for any assistance.

0 REPLIES 0
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online