Skip to content

Ensure PropertySourcesPlaceholderConfigurer uses the ConversionService from the Environment #34936

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mbazos
Copy link

@mbazos mbazos commented May 23, 2025

I noticed an issue with PropertySourcesPlaceholderConfigurer moving from spring framework 6.2.6 to 6.2.7.

Basically there is a new propertyResolver being created but it is not setting the environments configured conversion service. In the context of spring boot this could cause some issues if custom converters are registered as the propertyResolver if it doesn't have a conversion service set then uses the default and would not include any registered custom converters with the environment.

I don't think this PR is the exact correct fix and I didn't add any tests but this did resolve the issue with the propertyResolver having the environments conversion service. I figured this would be the best way to show the problem and how to fix it. If I have more time I can do a proper PR but might need some guidance if the fix is not in the right place.

If you look at PropertySourcesPlaceholderConfigurer in spring 6.2.6 you will see that the environments conversion service is set on the propertyResolver.

…n service from the Environment

I noticed an issue with PropertySourcesPlaceholderConfigurer moving from spring framework 6.2.6 to 6.2.7.

Basically there is a new propertyResolver being created but it is not setting the environments configured conversion service. In the context of spring boot this could cause some issues if custom converters are registered as the propertyResolver if it doesn't have a conversion service set then uses the default and would not include any registered custom converters with the environment.

I don't think this PR is the exact correct fix and I didn't add any tests but this did resolve the issue with the propertyResolver having the environments conversion service.

If you look at PropertySourcesPlaceholderConfigurer in spring 6.2.6 you will see that the environments conversion service is set on the propertyResolver.

Signed-off-by: Michael Bazos <[email protected]>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 23, 2025
@mbazos mbazos changed the title Fixing PropertySourcesPlaceholderConfigurer.java to set the conversion service from the Environment Fixing PropertySourcesPlaceholderConfigurer.java to set the conversion service from the Environment Issue with Spring Framework 6.2.7 May 23, 2025
@sbrannen sbrannen self-assigned this May 23, 2025
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label May 23, 2025
if(this.environment instanceof ApplicationServletEnvironment) {
propertyResolver.setConversionService(((ApplicationServletEnvironment) this.environment).getConversionService());
}

Copy link
Member

@sbrannen sbrannen May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (this.environment instanceof ConfigurableEnvironment configurableEnvironment) {
propertyResolver.setConversionService(configurableEnvironment.getConversionService());
}

I looked into a possible fix before looking at your proposal, and I first came up with the analogous solution above.

We cannot use ApplicationServletEnvironment here, since that class is specific to Spring Boot and Servlet deployments.

Thus, downcasting to ConfigurableEnvironment using instanceof pattern matching would be the correct way to do that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks something didn't feel right with the change I made

@sbrannen
Copy link
Member

Hi @mbazos,

Congratulations on submitting your first PR for the Spring Framework! 👍

And thanks for reporting the issue, which appears to be a regression caused by the changes made in conjunction with #34861.

After having pondered it a bit more, I think your proposal (as well as the similar one I posted) may not be the best approach to restoring the original behavior in 6.2.x.

If we set the ConversionService on the supplied ConfigurablePropertyResolver in processProperties(...), that ConversionService would then apply to all PropertySources which would include local properties.

However, that was not the previous behavior.

Previously, the ConversionService used by the Environment was only applied to PropertySources in the Environment, and I plan to reinstate that behavior.

We could consider applying a custom ConversionService to PropertySourcesPlaceholderConfigurer in general, but that would be a new feature and a change in behavior that we would likely only want to apply in Spring Framework 7.0.


At this point, I will introduce tests to confirm that this is a regression, and if it's a regression I will reinstate the previous behavior (automatic conversion of values from PropertySources in the Environment using the ConversionService configured in the Environment).

In light of the above, I plan to decline this PR; however, I will leave it open until I have completed the aforementioned tasks.

Cheers,

Sam

@sbrannen sbrannen added type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 23, 2025
@sbrannen sbrannen added this to the 6.2.8 milestone May 23, 2025
@sbrannen sbrannen changed the title Fixing PropertySourcesPlaceholderConfigurer.java to set the conversion service from the Environment Issue with Spring Framework 6.2.7 Ensure PropertySourcesPlaceholderConfigurer uses the ConversionService from the Environment May 23, 2025
@mbazos
Copy link
Author

mbazos commented May 23, 2025

Thanks @sbrannen appreciate the thorough and prompt review. Let me know if you need anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants