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

@ConfigurationProperties no longer works on a mutable Kotlin data classes #34500

Closed
Azbesciak opened this issue Mar 7, 2023 · 8 comments
Closed
Labels
type: regression A regression from a previous release
Milestone

Comments

@Azbesciak
Copy link

Hello, as in discusion on Gitter there is an issue after update from 3.0.3 to 3.0.4 with @ConfigurationProperty and @Component on bean.

Repro repo: https://github.com/Azbesciak/spring-3-configuration-prop-issue/tree/spring-boot-3.0.4-config-prop-component-issue (please notice branch spring-boot-3.0.4-config-prop-component-issue)

I suppose it existed also in 3.0.0 (that repo contains the example I also reported - #33509)
@wilkinsona also mentioned #34407

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 7, 2023
@wilkinsona wilkinsona changed the title @ConfigurationProperty and @Component - cannot run app after update to 3.0.4 @ConfigurationProperties and @Component - cannot run app after update to 3.0.4 Mar 7, 2023
@hho
Copy link
Contributor

hho commented Mar 7, 2023

I (probably) hit the same issue when upgrading to 3.0.4. Had a Kotlin data class with var fields and default values and wanted to apply @ConfigurationProperties like this:

@Bean
@ConfigurationProperties("event-hubs.producer")
fun eventHubProducerProperties() = EventHubProducerProperties()

In 3.0.0 this didn't work, then in 3.0.2 it did, and now in 3.0.4 it doesn't work again.
I guess that fixing #34407 led to a regression for #33710?

I stepped through it with a debugger: there are two instances created, one gets the properties injected, but the other one is used for the rest of the application context.

@philwebb
Copy link
Member

philwebb commented Mar 7, 2023

@Azbesciak You sample has the SomeConfig class annotated with @Component which makes Spring attempt to create it as a regular bean, rather than a configuration properties bean. If you remove @Component from SomeConfig and add @ConfigurationPropertiesScan to your DemoApplication class then things work.

@philwebb
Copy link
Member

philwebb commented Mar 7, 2023

@hho I expect your problem is similar. If you define a @Bean method then you are in control of creating the bean and constructor binding cannot be used. You could try @EnableConfigurationProperties(EventHubProducerProperties.class) if you want to create the EventHubProducerProperties instance using constructor binding.

If that doesn't work, please provide a sample that I can take a look at.

@philwebb philwebb added the status: waiting-for-feedback We need additional information before we can continue label Mar 7, 2023
@Azbesciak
Copy link
Author

@philwebb thank you for the quick reply.
My question is: is it a temporary solution or you stated that and no step back?
You know, that once occurred, I have tens of beans like that, some of them are @Bean, some of them are @Component, some just have @Bean and @ConfigurationProperty in configuration - not declared on class directly.

If it is permanent, I need to know that, but... you know, legacy, backward compatibility... not a good way to introduce in a patch release. Not a good way at all since it was working like this for as long as I remember, and it occurs even in tutorials.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Mar 7, 2023
@philwebb
Copy link
Member

philwebb commented Mar 7, 2023

Sorry, I think I messed up. I assumed that Kotlin data classes were immutable, but it looks like they can have setters. I'll need to change this logic

@philwebb philwebb added type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Mar 7, 2023
@philwebb philwebb added this to the 3.0.x milestone Mar 7, 2023
@Azbesciak
Copy link
Author

As I remember they can be immutable in spring boot, and still it is possible to use them as properties with @ConfigurationProperty, but to do that some special freamework configuration is required. @sdeleuze will know better :)

But yes, in general, Kotlin data class can be mutable.

@hho
Copy link
Contributor

hho commented Mar 7, 2023

OK, I took an hour to minimize my repro, and now I'm four minutes late 😄

Thank you both!

@wilkinsona wilkinsona changed the title @ConfigurationProperties and @Component - cannot run app after update to 3.0.4 @ConfigurationProperties no longer works on a mutable Kotlin data class annotated with @Component Mar 8, 2023
@hho
Copy link
Contributor

hho commented Mar 13, 2023

@wilkinsona Sorry, but I think that title is slightly misleading: The current issue affects all mutable data classes used with @ConfigurationProperties, not just the @Component annotated ones.

@philwebb philwebb changed the title @ConfigurationProperties no longer works on a mutable Kotlin data class annotated with @Component @ConfigurationProperties no longer works on a mutable Kotlin data classes Mar 14, 2023
@philwebb philwebb modified the milestones: 3.0.x, 3.0.5 Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

4 participants