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

Apply Kotlin Value Class unboxing to generated Property Accessors #3087

Closed
mp911de opened this issue Apr 29, 2024 · 0 comments
Closed

Apply Kotlin Value Class unboxing to generated Property Accessors #3087

mp911de opened this issue Apr 29, 2024 · 0 comments
Labels
type: bug A general bug

Comments

@mp911de
Copy link
Member

mp911de commented Apr 29, 2024

Providing a boxed value type via reflection works while using generated property accessors fails when the underlying type uses a unboxed representation.

@JvmInline
value class Auditor(val str: String)

data class MyEntity(
    @Id val id: Long = 0L,
    val name: String,
    @CreatedBy val createdBy: Auditor = Auditor("UNKNOWN"),
)

@Configuration
@EnableR2dbcAuditing
class AuditConfig {
    @Bean
    fun auditorAware(): ReactiveAuditorAware<Auditor> = ReactiveAuditorAware {
        Auditor("test")
    }
}

In Spring Data 3.2.5, when setting audit parameters, exception throws like

java.lang.ClassCastException: class mypackage.Auditor cannot be cast to class java.lang.String (mypackage.Auditor is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
	at mypackage.MyEntity_Accessor_uf7rrw.setProperty(Unknown Source)
	at org.springframework.data.mapping.model.ClassGeneratingPropertyAccessorFactory$KotlinValueBoxingAdapter.setProperty(ClassGeneratingPropertyAccessorFactory.java:1451)
	at org.springframework.data.mapping.model.InstantiationAwarePropertyAccessor.setProperty(InstantiationAwarePropertyAccessor.java:80)
	at org.springframework.data.mapping.model.SimplePersistentPropertyPathAccessor.setProperty(SimplePersistentPropertyPathAccessor.java:108)
	at org.springframework.data.mapping.model.SimplePersistentPropertyPathAccessor.setProperty(SimplePersistentPropertyPathAccessor.java:127)
	at org.springframework.data.auditing.MappingAuditableBeanWrapperFactory$MappingMetadataAuditableBeanWrapper.lambda$setProperty$0(MappingAuditableBeanWrapperFactory.java:232)
	at java.base/java.lang.Iterable.forEach(Iterable.java:75)
	at org.springframework.data.auditing.MappingAuditableBeanWrapperFactory$MappingMetadataAuditableBeanWrapper.setProperty(MappingAuditableBeanWrapperFactory.java:232)
	at org.springframework.data.auditing.MappingAuditableBeanWrapperFactory$MappingMetadataAuditableBeanWrapper.setLastModifiedBy(MappingAuditableBeanWrapperFactory.java:207)
	at org.springframework.data.auditing.AuditingHandlerSupport.touchAuditor(AuditingHandlerSupport.java:173)
	at org.springframework.data.auditing.AuditingHandlerSupport.lambda$touch$0(AuditingHandlerSupport.java:136)
	at java.base/java.util.Optional.map(Optional.java:260)
	at org.springframework.data.auditing.AuditingHandlerSupport.touch(AuditingHandlerSupport.java:134)
	at org.springframework.data.auditing.AuditingHandlerSupport.markModified(AuditingHandlerSupport.java:127)
	at org.springframework.data.auditing.ReactiveAuditingHandler.lambda$markModified$1(ReactiveAuditingHandler.java:90)
        ...

Originally posted by @w3-3w in #2868 (comment)

@mp911de mp911de added the type: bug A general bug label Apr 29, 2024
@mp911de mp911de added this to the 3.2.6 (2023.1.6) milestone Apr 29, 2024
mp911de added a commit that referenced this issue Apr 29, 2024
Unwrap wrapped value types if necessary when using generated property accessors.

Closes #3087
natedanner pushed a commit to natedanner/spring-projects__spring-data-commons that referenced this issue May 20, 2024
Unwrap wrapped value types if necessary when using generated property accessors.

Closes spring-projects#3087
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

1 participant