-
Notifications
You must be signed in to change notification settings - Fork 41.1k
Support config property --> object field custom mapping #37105
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
Comments
Could you explain why you would like to use a different key in config file? public: ***
publicBucket: ${public} |
Sure.
and the config from the top level looks like this:
So now image we have a bucket named
but we already have a bunch of them named like:
So we would break the naming convention. Or we need to change the naming convention just becasue of one bucket. It is clearly easier to just name the bucket in Java code as I hope it is clear now. |
I got the point. |
Introducing a new annotation will make things more complicated, I think custom setter |
I do not think so. I have encoutererd this predicament multiple times, and the question itself was viewed 7k+ times, so I assume I am not alone. I can of course each time get by using custom setters, but that's again unclear for the reader of the code on why this setter exists, becuase this setter is not utilized anywhere in the code base, so people delete it and you got the idea etc. It is a real experience, so I think providing inforamtion about mapping sometimes by annotation or by some other means will be great. P.S: In |
FWIW I don't think an additional annotation would make things too much more confusing. I'll flag this issue for team attention to see what others think. |
I don't think it would add too much confusion either. In addition to a "standard" Java class where a field could be annotated, we'd also need to consider things like records and Kotlin data classes so the implementation may not be completely straightforward. |
I like it, too. |
@quaff another example for needing this ... we have a property that we map to |
Hi, |
Thanks for looking at this. We're not sure exactly how this should be implemented. Above, we'd discussed introducing a new annotation. Perhaps |
… name Makes the Name annotation applicable on fields and takes this name when looking up the corresponding property. See spring-projectsgh-37105
Hi @wilkinsona, |
From a brief review of #39452 I think the PR might be a good starting point. We'll probably need updates to the configuration properties annotation processor to generate the correct metadata. |
Closing in favor of #39452. |
The following SO question illustrates the problem well.
Long story short, sometimes I want to have a property in my config file, which name is different from the object field. As far as I know, and seems it is still the case, there is no way to do it other than generating some custom setters, but that seems to be a workaround, not a solution.
It would be great to introduce a new annotation (or another way, but the annotation seems to be the smoothest) to handle this case, like:
Thanks in advance
The text was updated successfully, but these errors were encountered: