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
Provide configuration property metadata for binding to Map<String, RichType> and List<RichType> #9945
One example of such a Map is in Spring Cloud stream. As things stand, there's no metadata for any of the properties on BindingProperties. I think it would be interesting to explore the possibility of providing metadata for those properties, perhaps using a wildcard for the key in the map:
I've thought long and hard about this and I don't think we should do this (See also #9894). There are several reasons:
Scalar vs. Group: the only way for us to harvest nested object is by using @NestedConfigurationProperty (essentially, this is not a scalar value, please create a "sub-group" for all the properties defined by that type). If we support that for collections or maps now, how do we tell if the value is scalar or not?
Scope: if we find a way to fix this and we offer automatic harvesting of POJOs, where do we stop? The next step is to expect this to be somehow recursive (BindingProperties may hold another nested object).
Duplication: the use of the * above is what I ended up considering myself but that pojo can be used in multiple places, in multiples maps and/or in different objects. It means that the metadata is duplicated. If you don't want that, we need a third root objects (to harvest the pojo) and a way to reference to it somehow).
For "1)" the @NestedConfigurationProperty could be the indicator that the value isn't a scalar. So @NestedConfigurationProperty Map<String, Foo> is a hint that Foo can be expanded with the * syntax, where as @NestedConfigurationProperty Map<String, Foo> cannot (and we expect to be able to convert a String to a Foo somehow.
For "2)" and "3)" I'd probably go with duplicate *. I don't see the duplication being all that much of a problem, as long as there isn't an infinite loop (e.g. Foo contains a Map<String,Foo>).
and we expect to be able to convert a String to a Foo somehow.
That would break backward compatibility but I think having to add an annotation to ask the AP to "expand" is a nice way to solve the scope problem. Great idea! So 1) and 2) could be fixed by some signals that the AP should expand the metadata (and the binding should work as it does now, regardless of the annotation). That's pretty much what happens today for @NestedConfigurationProperty anyway.
As for 3, duplication is much easier but we need some feedback from IDE developers first IMO (ping @YannCebron@kdvolder and @AlexFalappa).
(By the way, I am kind of changing my mind because I realized recently there is no way to add hints for those types and implementing this would be a nice way to fix that).
Reacted by Gottlieb Wilhelm Brückner(Buks) van der Lingen
changed the title [-]Provide configuration property metadata for binding to Map<String, RichType>[/-][+]Provide configuration property metadata for binding to Map<String, RichType> and List<RichType>[/+]on Mar 9, 2018
Since this issue was raised, the IDEs have plugged the gap to varying degrees. A new gap will appear if we add support for immutable configuration properties that use constructor injection. Without metadata, they'd have to plug that gap and look at the constructor rather than the property setters to figure out what properties to offer.
@philwebb thanks for the feedback. I did not mean to complain about the issue still being open but to rather state the point of view in light of the arguments from the discussion above, having recently experienced weird issues in this direction, particularly with the kotlin spring-boot integration. Maybe I could have chosen my words more elegantly. I fully understand that there are a lot of high priority issues.
In the end it would simply make a lot of sense to have the annotation processor generate properties for all fields involved in the chain of properties in order to provide developers with reasonable insights on how to set the right switches through IDE completions.
In my particular case I wrote a POJO for configuring spring security overloadings, more specifically for adding CORS Configurations per URI-Path (e.g. "/**", "/internal","/public", etc.) including various settings. Then I ended up getting properties not generated from the List which was extremely frustrating as security information should ideally be as-is generated and not manually added (generated from actual code) regardless of the code. Alongside, using Kotlin creates issues since datatypes are not properly translated for default values (e.g. java.lang.Boolean false is generated then the default initialization is true). Lastly, there was weird behavior with the data classes constructor initialization, where it would generate certain properties only if not all attributes where initialized with default values:
val b: Boolean = true
val list: List = listOf()
Would fail to generate info, whereas changing to "val list: List" would then yield the property b in the generated file.
Furthermore, the documentation and widely available tutorials on the topic do not yield a sufficient solution. In fact the overall impression from searching for a solution was that it is an antipattern to provide another @ConfigurationProperties Annotation in an inner Record/Data Class.
Wrapping it up: I hope this helps to understand why it makes sense to output generated data of properties containing potentially recurring POJOs for "type": "java.util.List/Map". I guess that it would be of great benefit to make this behavior selectable, e.g. as an additional option to the @NestedConfigurationProperty() or @ConfigurationProperties() annotation, indicating that POJOs in collections should be generated as well, thereby not changing current standard behavior in regards to the mentioned concerns.
It would even be an improvement to add the POJO class reference into the generated metadata, so that we can at least see what type this List references and then do a lookup ourselves.
I hope this feedback gives some more insight into the topic, again sorry if I came across a little too blunt earlier.
Cheers.
The proposed changes provide configuration property metadata support for binding to Map<String, RichType> and List<RichType>. Consider the following example class:
I've used the prefix * for Map types and the prefix [*] for Collection types. I
intentionally avoided using * for both to ensure the IDE can clearly distinguish between Map and Collection types.
@nosan I was preparing some configuration properties and then I faced this issue.
When the classes are nested, the generated type is java.util.Map<java.lang.String,com.example.MyClass$MyRecord>. So when I see only java.util.Map in your example, it doesn't look correct.
Edit:
As far as I can see, even if it's an inner class, the config metadata of com.example.MyClass$MyRecord will still not be properly generated.
As far as I can see, even if it's an inner class, the config metadata of com.example.MyClass$MyRecord will still not be properly generated.
Map or List fields/methods must be annotated with @NestedConfigurationProperty to signal to the annotation processor that it is a rich type and should be expanded; otherwise, there is no way to determine whether properties should be generated. For example, in Map<String, Duration>, Duration is not a simple (scalar) property like java.lang.String, but the annotation processor should not try to generate properties for it.
When the classes are nested, the generated type is java.util.Map<java.lang.String,com.example.MyClass$MyRecord>. So when I see only java.util.Map in your example, it doesn't look correct.
Sorry, what is the example? Could you please point it out to me?
@philwebb I'm very cautious in asking such questions, because we all know how open source is. By all means I'm not demanding anything. But do you have an idea when would have news about this issue and/or would labels be changed if the spring team starts to actively work on this?
@renannprado The labels would change and the issue would be assigned to someone once we start work on it. I'll flag it for a team discussion on our next call since it's quite an old issue and there's not been much movement on it.
@philwebb I just happen to remember about this issue and I saw that the team-meeting label has been removed. It would be really great to have this fixed (or at least moving it forward a little bit) although now with AI you can simply ask for the additional-spring-configuration-metadata.json to be generated.
Activity
snicoll commentedon Aug 14, 2017
I've thought long and hard about this and I don't think we should do this (See also #9894). There are several reasons:
@NestedConfigurationProperty(essentially, this is not a scalar value, please create a "sub-group" for all the properties defined by that type). If we support that for collections or maps now, how do we tell if the value is scalar or not?BindingPropertiesmay hold another nested object).*above is what I ended up considering myself but that pojo can be used in multiple places, in multiples maps and/or in different objects. It means that the metadata is duplicated. If you don't want that, we need a third root objects (to harvest the pojo) and a way to reference to it somehow).Let's see what the rest of the team thinks.
philwebb commentedon Aug 16, 2017
For "1)" the
@NestedConfigurationPropertycould be the indicator that the value isn't a scalar. So@NestedConfigurationProperty Map<String, Foo>is a hint thatFoocan be expanded with the*syntax, where as@NestedConfigurationProperty Map<String, Foo>cannot (and we expect to be able to convert aStringto aFoosomehow.For "2)" and "3)" I'd probably go with duplicate
*. I don't see the duplication being all that much of a problem, as long as there isn't an infinite loop (e.g.Foocontains aMap<String,Foo>).philwebb commentedon Aug 16, 2017
FWIW, I think we shouldn't invest too much time thinking about this until after 2.0.
snicoll commentedon Aug 27, 2017
That would break backward compatibility but I think having to add an annotation to ask the AP to "expand" is a nice way to solve the scope problem. Great idea! So 1) and 2) could be fixed by some signals that the AP should expand the metadata (and the binding should work as it does now, regardless of the annotation). That's pretty much what happens today for
@NestedConfigurationPropertyanyway.As for 3, duplication is much easier but we need some feedback from IDE developers first IMO (ping @YannCebron @kdvolder and @AlexFalappa).
(By the way, I am kind of changing my mind because I realized recently there is no way to add hints for those types and implementing this would be a nice way to fix that).
[-]Provide configuration property metadata for binding to Map<String, RichType>[/-][+]Provide configuration property metadata for binding to Map<String, RichType> and List<RichType>[/+]wilkinsona commentedon Jan 15, 2019
Since this issue was raised, the IDEs have plugged the gap to varying degrees. A new gap will appear if we add support for immutable configuration properties that use constructor injection. Without metadata, they'd have to plug that gap and look at the constructor rather than the property setters to figure out what properties to offer.
17 remaining items
dgensert commentedon Feb 12, 2025
philwebb commentedon Feb 12, 2025
dgensert commentedon Feb 12, 2025
@philwebb thanks for the feedback. I did not mean to complain about the issue still being open but to rather state the point of view in light of the arguments from the discussion above, having recently experienced weird issues in this direction, particularly with the kotlin spring-boot integration. Maybe I could have chosen my words more elegantly. I fully understand that there are a lot of high priority issues.
In the end it would simply make a lot of sense to have the annotation processor generate properties for all fields involved in the chain of properties in order to provide developers with reasonable insights on how to set the right switches through IDE completions.
In my particular case I wrote a POJO for configuring spring security overloadings, more specifically for adding CORS Configurations per URI-Path (e.g. "/**", "/internal","/public", etc.) including various settings. Then I ended up getting properties not generated from the List which was extremely frustrating as security information should ideally be as-is generated and not manually added (generated from actual code) regardless of the code. Alongside, using Kotlin creates issues since datatypes are not properly translated for default values (e.g. java.lang.Boolean false is generated then the default initialization is true). Lastly, there was weird behavior with the data classes constructor initialization, where it would generate certain properties only if not all attributes where initialized with default values:
Would fail to generate info, whereas changing to "val list: List" would then yield the property b in the generated file.
Furthermore, the documentation and widely available tutorials on the topic do not yield a sufficient solution. In fact the overall impression from searching for a solution was that it is an antipattern to provide another @ConfigurationProperties Annotation in an inner Record/Data Class.
Wrapping it up: I hope this helps to understand why it makes sense to output generated data of properties containing potentially recurring POJOs for "type": "java.util.List/Map". I guess that it would be of great benefit to make this behavior selectable, e.g. as an additional option to the @NestedConfigurationProperty() or @ConfigurationProperties() annotation, indicating that POJOs in collections should be generated as well, thereby not changing current standard behavior in regards to the mentioned concerns.
It would even be an improvement to add the POJO class reference into the generated metadata, so that we can at least see what type this List references and then do a lookup ourselves.
I hope this feedback gives some more insight into the topic, again sorry if I came across a little too blunt earlier.
Cheers.
nosan commentedon Apr 5, 2025
I've been working on this issue for a while and have come up with some changes: main...nosan:spring-boot:gh-9945
The proposed changes provide configuration property metadata support for binding to
Map<String, RichType>andList<RichType>. Consider the following example class:Details
The generated JSON metadata will be:
Details
{ "groups": [ { "name": "config", "type": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties" }, { "name": "config.custom-list", "type": "org.springframework.boot.configurationsample.specific.RichProperties$CustomList", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getCustomList()" }, { "name": "config.custom-list.[*].addresses", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person", "sourceMethod": "getAddresses()" }, { "name": "config.custom-map", "type": "org.springframework.boot.configurationsample.specific.RichProperties$CustomMap", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getCustomMap()" }, { "name": "config.custom-map.*.addresses", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person", "sourceMethod": "getAddresses()" }, { "name": "config.list", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getList()" }, { "name": "config.list-of-list", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getListOfList()" }, { "name": "config.list-of-list.[*].[*].addresses", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person", "sourceMethod": "getAddresses()" }, { "name": "config.list-of-map", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getListOfMap()" }, { "name": "config.list-of-map.[*].*.addresses", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person", "sourceMethod": "getAddresses()" }, { "name": "config.list-of-unresolved-generic", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getListOfUnresolvedGeneric()" }, { "name": "config.list-of-wildcard", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getListOfWildcard()" }, { "name": "config.list.[*].addresses", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person", "sourceMethod": "getAddresses()" }, { "name": "config.map", "type": "java.util.Map", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getMap()" }, { "name": "config.map-of-list", "type": "java.util.Map", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getMapOfList()" }, { "name": "config.map-of-list.*.[*].addresses", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person", "sourceMethod": "getAddresses()" }, { "name": "config.map-of-map", "type": "java.util.Map", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getMapOfMap()" }, { "name": "config.map-of-map.*.*.addresses", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person", "sourceMethod": "getAddresses()" }, { "name": "config.map-of-unresolved-generic", "type": "java.util.Map", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getMapOfUnresolvedGeneric()" }, { "name": "config.map-of-wildcard", "type": "java.util.Map", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getMapOfWildcard()" }, { "name": "config.map.*.addresses", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person", "sourceMethod": "getAddresses()" }, { "name": "config.raw-list", "type": "java.util.List", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getRawList()" }, { "name": "config.raw-map", "type": "java.util.Map", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties", "sourceMethod": "getRawMap()" } ], "properties": [ { "name": "config.custom-list.[*].addresses.[*].street", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Address" }, { "name": "config.custom-list.[*].name", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person" }, { "name": "config.custom-map.*.addresses.[*].street", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Address" }, { "name": "config.custom-map.*.name", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person" }, { "name": "config.list-of-list.[*].[*].addresses.[*].street", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Address" }, { "name": "config.list-of-list.[*].[*].name", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person" }, { "name": "config.list-of-map.[*].*.addresses.[*].street", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Address" }, { "name": "config.list-of-map.[*].*.name", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person" }, { "name": "config.list.[*].addresses.[*].street", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Address" }, { "name": "config.list.[*].name", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person" }, { "name": "config.map-of-list.*.[*].addresses.[*].street", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Address" }, { "name": "config.map-of-list.*.[*].name", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person" }, { "name": "config.map-of-map.*.*.addresses.[*].street", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Address" }, { "name": "config.map-of-map.*.*.name", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person" }, { "name": "config.map.*.addresses.[*].street", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Address" }, { "name": "config.map.*.name", "type": "java.lang.String", "sourceType": "org.springframework.boot.configurationsample.specific.RichProperties$Person" } ], "hints": [], "ignored": { "properties": [] } }I've used the prefix
*forMaptypes and the prefix[*]forCollectiontypes. Iintentionally avoided using
*for both to ensure the IDE can clearly distinguish betweenMapandCollectiontypes.renannprado commentedon Jun 13, 2025
@nosan I was preparing some configuration properties and then I faced this issue.
When the classes are nested, the generated
typeisjava.util.Map<java.lang.String,com.example.MyClass$MyRecord>. So when I see onlyjava.util.Mapin your example, it doesn't look correct.Edit:
As far as I can see, even if it's an inner class, the config metadata of
com.example.MyClass$MyRecordwill still not be properly generated.nosan commentedon Jun 16, 2025
MaporListfields/methods must be annotated with@NestedConfigurationPropertyto signal to the annotation processor that it is a rich type and should be expanded; otherwise, there is no way to determine whether properties should be generated. For example, inMap<String, Duration>,Durationis not a simple (scalar) property likejava.lang.String, but the annotation processor should not try to generate properties for it.Sorry, what is the example? Could you please point it out to me?
renannprado commentedon Feb 9, 2026
@nosan I meant that in your example I see it generating
"type": "java.util.List"instead of"type": "java.util.List<com.company.Clazz">.Is there any workarounds for this? even if it means adding a few entries manually just to help the tooling figure out?
@philwebb is it realistic someone outside the spring team could contribute to get this issue fixed?
philwebb commentedon Feb 9, 2026
I think it's unlikely TBH. This one probably needs deep consideration from the team. I'll update the labels on the issue.
renannprado commentedon Feb 10, 2026
@philwebb I'm very cautious in asking such questions, because we all know how open source is. By all means I'm not demanding anything. But do you have an idea when would have news about this issue and/or would labels be changed if the spring team starts to actively work on this?
Thanks for the quick reply btw.
philwebb commentedon Feb 10, 2026
@renannprado The labels would change and the issue would be assigned to someone once we start work on it. I'll flag it for a team discussion on our next call since it's quite an old issue and there's not been much movement on it.
renannprado commentedon Aug 27, 2026
@philwebb I just happen to remember about this issue and I saw that the
team-meetinglabel has been removed. It would be really great to have this fixed (or at least moving it forward a little bit) although now with AI you can simply ask for theadditional-spring-configuration-metadata.jsonto be generated.