Skip to content

Support default, primitive Kotlin attributes. #2276

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

Closed
pelletier197 opened this issue Jun 2, 2021 · 3 comments
Closed

Support default, primitive Kotlin attributes. #2276

pelletier197 opened this issue Jun 2, 2021 · 3 comments
Assignees
Labels
type: bug A general bug

Comments

@pelletier197
Copy link

Default values for fields are not supported for constructor of node entities when querying the database with a custom query.

CREATE (person:Person { id: "first", firstName: "firstName"})
CREATE (person:Person { id: "second"})

with the following data class

@Node("Person")
data class Person(
  val id: String, 
  val firstName: String = "Unknown"
)

and the following repository

interface PersonRepository : Neo4jRepository<Person, String>

calling personRepository.findAll() results in

Could not convert NULL into String; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [String] for value 'null';
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 2, 2021
@michael-simons michael-simons self-assigned this Jun 2, 2021
@michael-simons
Copy link
Collaborator

michael-simons commented Jun 2, 2021

Do you have a reproducer at hand? Cannot reproduce it on 6.0.x nor 6.1.x?

The @Id annotation is missing in your example:

@Node
data class KotlinPersonWithDefaults(
		@Id
		val id: String,
		val firstName: String = "Unknown"
)

Tried

  • Kotlin in Java
  • Kotlin in Kotlin…
    I'm something missing somewhere.

@michael-simons michael-simons added blocked: awaiting feedback and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 2, 2021
@pelletier197
Copy link
Author

pelletier197 commented Jun 2, 2021

Sorry, my mistake. It actually work with the String, but it doesn't seem to work when the default value is for a primitive. Here is my sample

@Node("TestPerson")
data class TestPersonEntity(
    @Id
    val id: String,
    @Property("name")
    val name: String = "Unknown", // this works
    val otherPrimitive: Int = 32, // <<- this doesn't
)

@Node("TestDepartment")
data class TestDepartmentEntity(
    @Id
    val id: String,
    @Property("name")
    val name: String
)

data class PersonProjection(
    val person: TestPersonEntity,
    val department: TestDepartmentEntity
)

interface TestPersonRepositoryProjection : Neo4jRepository<TestPersonEntity, String> {
    @Query(
        """
        MATCH (person:TestPerson)
        MATCH (person)-[:MEMBER_OF]->(department:TestDepartment)
        return person, department
        """
    )
    fun findPeople(): List<PersonProjection>
}

@Configuration
class TestRepository(
    private val repository: TestPersonRepositoryProjection,
) {
    init {
            println(repository.findPeople())
    }
}

I tested with int and boolean and I get the same error. I assume it's for all primitives, but I haven't tested all of them.

And I executed the following query in Neo4j

CREATE (p1:TestPerson {id: "first", name: "First name"})
CREATE (p2:TestPerson {id: "second"})
CREATE (d:TestDepartment {id: "department", name: "Test"})
CREATE (p1)-[:MEMBER_OF]->(d)
CREATE (p2)-[:MEMBER_OF]->(d)

@michael-simons
Copy link
Collaborator

michael-simons commented Jun 2, 2021 via email

@michael-simons michael-simons added the type: bug A general bug label Jun 2, 2021
@michael-simons michael-simons changed the title Kotlin: Default values not working in entities Enable default, primitive Kotlin attributes. Jun 2, 2021
@michael-simons michael-simons changed the title Enable default, primitive Kotlin attributes. Support default, primitive Kotlin attributes. Jun 2, 2021
michael-simons added a commit that referenced this issue Jun 2, 2021
This bugfix allows primitive attributes with defaults in Kotlin based domain classes. It comes at a price however: We cannot throw a mapping exception hinting at the fact a primitive had been tried to be assigned with null in standard java. The ` MappingException`’s cause will than be a `NullpointerException`.

The value of supporting Kotlin defaults is in this case higher than the error message, which was "received" with a trick anyway (relying on the conversion service that it throws an exception.

This fixes #2276.
michael-simons added a commit that referenced this issue Jun 2, 2021
This bugfix allows primitive attributes with defaults in Kotlin based domain classes. It comes at a price however: We cannot throw a mapping exception hinting at the fact a primitive had been tried to be assigned with null in standard java. The ` MappingException`’s cause will than be a `NullpointerException`.

The value of supporting Kotlin defaults is in this case higher than the error message, which was "received" with a trick anyway (relying on the conversion service that it throws an exception.

This fixes #2276.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants