Skip to content

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

Closed
@ederfmatos

Description

@ederfmatos

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?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions