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
Nested object is not initialized if no matching property is defined with constructor binding #18917
Comments
|
Is it a good idea to solve this problem by throwing an exception? @snicoll |
We've decided to always create the object no matter what to promote the fact browsing through the hierarchy is guaranteed. |
If you do so,how would you solve the problem that lose the signal that the user didn't provide any key for that nested Security object. |
We should probably extend the scope of this issue to other use cases that are similar, i.e:
Given the team decision, it looks like we should do this as well for consistency. @philwebb I can see this issue is on hold and I don't remember why that is. |
🤷♀ I can't remember either |
Hello team, I've just found this problem in my project. I see on-hold tag removed, does this mean this bug will we fixed in next release? Thanks! |
That's not what the label means. On hold means we can't make progress at the moment. We don't quite remember why we added it so now it's back to the "issues to fix in 2.2.x" bucket. There is no guarantee that it will be fixed for the next release though. |
@snicoll thanks for letting me know. Would you be interested in co-op, like a PR? I'd just need a few pointers, where to start looking. |
@zeratul021 Thanks for the offer but I think it's better if we handle this one. I'd started looking into but we ran into a few issues which is why the issue was put on hold. I'll pick this up again when time permits. |
@mbhave any update on this? |
There is a |
Turns out everything was ok. The only downside with that approach is that you can write something like this now: @ConfigurationProperties("test")
public class EmptyDefaultValueProperties {
@ConstructorBinding
public EmptyDefaultValueProperties(@DefaultValue String name) {
this.name = name;
}
....
} This won't generate a default value at all (no-op). |
Consider the following example:
If this object is bound and no
acme.security.*
property is available in the environment,null
is provided to the top-level constructor. If a matching property is found, theSecurity
type is initialized and provided to the constructor.This is consistent with what we do with JavaBean binding. The main difference here is that the object may have default values assigned to the constructor (here a default
user
). One solution would be to provide an instance no matter what. The alternative is for the user to check if the constructor provided anull
instance and then create a defaultSecurity
.There are pros and cons for each:
Security
objectnull
and then create an instance with a copy paste of the@DefaultValue
we already provided.A middle-ground would be to make this configurable, perhaps using
@Nullable
as we do for actuator endpoints.The text was updated successfully, but these errors were encountered: