You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow @ConfigurationProperties binding for immutable POJOs #8762
Currently, it seems we are forced to use Kotlin classes with mutable nullable properties and default constructor with @ConfigurationProperties while idiomatic Kotlin code would be using classes with immutable properties initialized via constructor. I think there is a way to supporting that by leveraging kotlin-reflect library like jackson-module-kotlin do.
More concretely, in MiXiT app I would like to be able to convert this MixitProperties class implementation to:
@ConfigurationProperties("mixit")
class MixitProperties(
val baseUri: String,
val admin: Credential,
val drive: Drive
)
class Credential(
val username: String,
val password: String
)
class Drive(
val fr: DriveDocuments,
val en: DriveDocuments
)
class DriveDocuments(
val sponsorform: String,
val sponsor: String,
val speaker: String,
val press: String
)
We could imagine to support optional properties by using nullable types, like val sponsorform: String? for example.
I will be happy to help. I have also already worked with @apatrida who maintains Jackson Kotlin module, he may provide us some guidance I think.
Reacted by Daniel Lavoie, Simon, Alexander L., Christoph Leiter, kitsjingkeily, Jakub Jabłoński, Ken Yee, Dariusz Baciński, Michael Barker, Suresh and 129 more
We currently use Spring Framework's binder (that's also used in the MVC world) and it does not support binding via constructor. I guess that's the problem you want to solve?
We're rewriting the binder for Boot 2.0, perhaps @philwebb has some plans about that?
@jhoeller just pinged me to mention SPR-15199. Would be pretty cool if we could use that somehow but I guess the best course of action is our binder and 2.0 anyway...
There are two models that work with Kotlin. The first, is that a plugin model to the binder let's Kotlin describe the constructors and properties providing information about which can be used to construct the class (or a combination of constructor + properties set after). The second, is that a source of all settable things is made available and the decision is made by a Kotlin instantiator that does the work based on what it has available.
The best support considers all of:
what properties are set in the constructor
what properties are writeable after construction
for any property if it is nullable
for any property in the constructor has a default value that can be used if it is absent
Kotlin reflection has a callBy method on the constructor that can be used in the case that default values are being used, otherwise it should use call method.
So the binder should give the opportunity to a plugin to do the introspection and instantiation so that the plugin can do the best job for the language.
Binding examples for Kotlin:
Jackson Kotlin Module (pulls parameter name information, then hands it back along with actual parameters to a custom instantiator)
JDBI 3 (the Kotlin module handles all binding tasks when the target is a Kotlin class)
The experimental binder code is here, but it's still very much a work in progress. The plan is to make a binder that's specifically designed for binding from our configuration properties. It'll allow us to do some nice things like track the line/column number that a property was loaded from.
The Binder class is pretty complete, and there are quite a few tests. It deals with Collections, Arrays, Maps and Scalar types directly. When it finds something it can't handle it delegates to BeanBinder implementations.
We currently have a JavaBeanBinder that works with mutable classes but the plan is to add a couple more. We'd like to have a ValueObjectBeanBinder that binds using constructor arguments (this should work well with Kotlin).
We've also got plans for an InterfaceBeanBinder. The idea would be that you can so something like this:
@apatrida Since the new binder is now in master, any chance you could have a look to identify what needs to be done to provide first class support for immatable data classes in Kotlin?
To summarize the discussion I just had with @sdeleuze we have interface binding on the pipe for 2.0 (see #1254). This issue is really about supporting constructor binding as well and it brings a number of challenges. One of them is that we need to upgrade the annotation processor to be able to detect and generate the metadata in such a case.
I bet Java developers will be interested by this feature as well and will use it with Lombok which will force us to detect all of these cases and I anticipate it is a lot of work. And supporting constructor binding without the metadata support is a no-go on my end.
I am flagging this one for team attention so that the rest of the team has a chance to review this.
Compare this class from my project (please, ignore the missing ElasticsearchSource definition):
@Component
@ConfigurationProperties
class ElasticsearchConfiguration {
var origin : ElasticsearchSource? = null
var destination : ElasticsearchSource? = null
override fun toString() = """
|ElasticsearchConfiguration:
|origin=$origin
|destination=$destination
""".trimMargin()
}
to the version I tried first:
@Component
@ConfigurationProperties
data class ElasticsearchConfiguration(val origin: ElasticsearchSource, val destination :ElasticsearchSource?)
Several features are dropped:
properties have to be mutable
properties have to be nullable or use lateinit
equals, hashCode and toString are not included, and have to be written by hand (as I did in one case, just for debugging)
copy and componentN are not included neither
From a Kotlin point of view, this type of configuration is obviously an immutable data class. There is some (very small, I concede) cognitive dissonance not being able to use a value type instead of a regular class for a limitation in the framework. Plus, the component is open to same rogue code altering a property.
I would really love to have this issue sorted out, as it would lead to beautiful, terse, expressive and idiomatic Kotlin data classes. :)
I just updated the previous comment. I thought that lateinit var would work... and it doesn't. :(
So, furthermore, we lose the distinction between optional and required properties. Perhaps it could be somewhat softened by using @required annotations and validating, I don't know, but it's not ideal.
The current refers to the current, i.e. latest, Spring Boot release that is GA. Once 2.2 reaches that state, the current link will point to the documentation for 2.2.0.RELEASE.
@thekalinga Immutable configuration property binding works with both Java and Kotlin. If you believe you've found a situation where it's not working as expected, please open a new issue with a complete and minimal sample that reproduces the problem. A project zipped and attached to the new issue or hosted in a separate GitHub repository is ideal.
Activity
snicoll commentedon Mar 28, 2017
We currently use Spring Framework's binder (that's also used in the MVC world) and it does not support binding via constructor. I guess that's the problem you want to solve?
We're rewriting the binder for Boot 2.0, perhaps @philwebb has some plans about that?
snicoll commentedon Mar 28, 2017
@jhoeller just pinged me to mention SPR-15199. Would be pretty cool if we could use that somehow but I guess the best course of action is our binder and 2.0 anyway...
Having said that, perhaps we can reuse some bits?
apatrida commentedon Mar 28, 2017
There are two models that work with Kotlin. The first, is that a plugin model to the binder let's Kotlin describe the constructors and properties providing information about which can be used to construct the class (or a combination of constructor + properties set after). The second, is that a source of all settable things is made available and the decision is made by a Kotlin instantiator that does the work based on what it has available.
The best support considers all of:
Kotlin reflection has a
callBymethod on the constructor that can be used in the case that default values are being used, otherwise it should usecallmethod.So the binder should give the opportunity to a plugin to do the introspection and instantiation so that the plugin can do the best job for the language.
Binding examples for Kotlin:
Jackson Kotlin Module (pulls parameter name information, then hands it back along with actual parameters to a custom instantiator)
JDBI 3 (the Kotlin module handles all binding tasks when the target is a Kotlin class)
apatrida commentedon Mar 28, 2017
If there is a branch to track for the new binder, I can look at it and see what needs to be done to support Kotlin, and add that support.
philwebb commentedon Mar 28, 2017
The experimental binder code is here, but it's still very much a work in progress. The plan is to make a binder that's specifically designed for binding from our configuration properties. It'll allow us to do some nice things like track the line/column number that a property was loaded from.
The Binder class is pretty complete, and there are quite a few tests. It deals with Collections, Arrays, Maps and Scalar types directly. When it finds something it can't handle it delegates to BeanBinder implementations.
We currently have a
JavaBeanBinderthat works with mutable classes but the plan is to add a couple more. We'd like to have aValueObjectBeanBinderthat binds using constructor arguments (this should work well with Kotlin).We've also got plans for an
InterfaceBeanBinder. The idea would be that you can so something like this:Which would be used to generate a proxy class with the appropriate implementation.
The first step is to get the binder working with our existing mutable configuration property beans. We'll then look at extending the support.
bclozel commentedon Apr 28, 2017
Quick update here: #8868 has been merged, the new binder is in master.
sdeleuze commentedon Jun 5, 2017
@apatrida Since the new binder is now in master, any chance you could have a look to identify what needs to be done to provide first class support for immatable data classes in Kotlin?
serandel commentedon Jun 28, 2017
Is this planned for the 2.0 release or for later in the roadmap? @ConfigurationProperties are a bit confusing without it.
snicoll commentedon Jun 28, 2017
To summarize the discussion I just had with @sdeleuze we have interface binding on the pipe for 2.0 (see #1254). This issue is really about supporting constructor binding as well and it brings a number of challenges. One of them is that we need to upgrade the annotation processor to be able to detect and generate the metadata in such a case.
I bet Java developers will be interested by this feature as well and will use it with Lombok which will force us to detect all of these cases and I anticipate it is a lot of work. And supporting constructor binding without the metadata support is a no-go on my end.
I am flagging this one for team attention so that the rest of the team has a chance to review this.
wilkinsona commentedon Jun 28, 2017
@serandel Can you please expand a bit on what you find confusing at the moment?
serandel commentedon Jun 28, 2017
@wilkinsona Of course. :)
Compare this class from my project (please, ignore the missing ElasticsearchSource definition):
to the version I tried first:
Several features are dropped:
or use lateinitFrom a Kotlin point of view, this type of configuration is obviously an immutable data class. There is some (very small, I concede) cognitive dissonance not being able to use a value type instead of a regular class for a limitation in the framework. Plus, the component is open to same rogue code altering a property.
I would really love to have this issue sorted out, as it would lead to beautiful, terse, expressive and idiomatic Kotlin data classes. :)
serandel commentedon Jun 28, 2017
I just updated the previous comment. I thought that
lateinit varwould work... and it doesn't. :(So, furthermore, we lose the distinction between optional and required properties. Perhaps it could be somewhat softened by using @required annotations and validating, I don't know, but it's not ideal.
73 remaining items
Polish
wilkinsona commentedon Apr 16, 2019
The documentation refers to
@ConfigurationPropertyDefaultValuebut the annotation was renamed to@DefaultValue.Update docs to reflect rename of @ConfigurationPropertiesDefaultValue
Azbesciak commentedon May 16, 2019
In the doc there is info that this is still not supported...
snicoll commentedon May 16, 2019
That link is for the wrong version of Spring Boot.
This was updated properly: https://docs.spring.io/spring-boot/docs/2.2.0.BUILD-SNAPSHOT/reference/html/spring-boot-features.html#boot-features-kotlin-configuration-properties
Azbesciak commentedon May 16, 2019
Ok, but this is first in the Google... and says that this is current.
wilkinsona commentedon May 16, 2019
The
currentrefers to the current, i.e. latest, Spring Boot release that is GA. Once 2.2 reaches that state, thecurrentlink will point to the documentation for 2.2.0.RELEASE.prog110 commentedon May 21, 2019
@wilkinsona Does this work with Java aswell (or) it works just with kotlin?
I tried with the following configuration & it did not resolve properties. Not sure if I had any mistake
application.ymlExternalSystemConfig.javaDemoApplication.javaBut the
configobject that gets injected have all values set to nullI'm using
2.2.0.BUILD-SNAPSHOTversion for thisAny idea if I'm doing something wrong
Thanks!
prog110 commentedon May 21, 2019
If I make my object mutable, the binding works fine tho
prog110 commentedon May 21, 2019
This is how my (lombok) generated class looks like
wilkinsona commentedon May 21, 2019
@thekalinga Immutable configuration property binding works with both Java and Kotlin. If you believe you've found a situation where it's not working as expected, please open a new issue with a complete and minimal sample that reproduces the problem. A project zipped and attached to the new issue or hosted in a separate GitHub repository is ideal.
prog110 commentedon May 21, 2019
@wilkinsona Just opened an issue with test project
#16928