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

Support instantiation of Kotlin class with overridden read-only property #4485

Closed
ederfmatos opened this issue Aug 27, 2023 · 2 comments
Closed
Assignees
Labels
type: enhancement A general enhancement

Comments

@ederfmatos
Copy link

I have the following data class in kotlin, where the "type" attribute always has the fixed value "BILLET", this is because the interface that my class implements asks for this attribute.

data class BilletTransaction(
    override val id: String,
    override val name: TransactionName,
    override val createdAt: ZonedDateTime,
    override val value: BigDecimal,
    val dueDate: LocalDate,
) : Transaction {
    override val type: TransactionType = TransactionType.BILLET
}

I have a record in the mongo database with this information, but when trying to read it I get the following error:

java.lang.UnsupportedOperationException: No accessor to set property private final transient <PACKAGE>.TransactionType <PACKAGE>.BilletTransaction.type
	at <PACKAGE>.BilletTransaction_Accessor_wa9qij.setProperty(Unknown Source)
	at org.springframework.data.mapping.model.InstantiationAwarePropertyAccessor.setProperty(InstantiationAwarePropertyAccessor.java:81)
	at org.springframework.data.mapping.model.ConvertingPropertyAccessor.setProperty(ConvertingPropertyAccessor.java:60)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:606)

I noticed that in the class "InstantiationAwarePropertyAccessor" there is a method that tries to use a possible "setter" to configure the value, but it is not possible and the error occurs

image

Is it possible to get around this error?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 27, 2023
@ederfmatos
Copy link
Author

ederfmatos commented Aug 27, 2023

Using Transient annotation this problem is fixed, but the value of field is not saved in the database. In this case, the field is no writable field, is read only

@mp911de
Copy link
Member

mp911de commented Aug 28, 2023

The root of this is that we should refrain from reading immutable properties in the first place. Generally speaking, if the field in your domain model is set to a fixed value, it doesn't make sense to write it to the database.

As MappingMongoConverter is attempting to read a value for an immutable property, we should rather attempt to filter out such properties from being read.

@mp911de mp911de transferred this issue from spring-projects/spring-data-commons Aug 28, 2023
@mp911de mp911de added status: blocked An issue that's blocked on an external project change type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 28, 2023
@mp911de mp911de self-assigned this Aug 28, 2023
@mp911de mp911de removed the status: blocked An issue that's blocked on an external project change label Sep 4, 2024
@mp911de mp911de changed the title Problem with kotlin field with fixed value Support instantiation of Kotlin class with overridden read-only property Sep 4, 2024
@christophstrobl christophstrobl added this to the 4.4 M1 (2024.1.0) milestone Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
4 participants