Skip to content
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

SystemEnvironmentPropertySource should try to replace hyphens as well [SPR-13594] #18172

Closed
spring-projects-issues opened this issue Oct 21, 2015 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 21, 2015

Alex Antonov opened SPR-13594 and commented

This seems to be a common problem, with an easy solution (as noted by multiple postings on StackOverflow, i.e. - http://stackoverflow.com/questions/28158732/spring-boot-how-to-override-properties-with-dash-on-linux-env-profile?answertab=active#28182563).

When using Linux OS Environment to set properties (overrides), the property names that contain dashes (-), such as jobs.batch-size, don't get properly mapped if using an env variable JOBS_BATCH_SIZE and using @Value("${jobs.batch-size}") field declaration.
Since Linux command-line does not allow for use of dashes (-) in setting the ENV variables, this makes it hard to set those variables using environment.

A proposed fix would be to add another transformation step inside SystemEnvironmentPropertySource.java:resolvePropertyName(String name) to also do name.replace('\-', '\_') in addition to name.replace('.', '_')

private String resolvePropertyName(String name) {
	Assert.notNull(name, "Property name must not be null");
	if (containsKey(name)) {
		return name;
	}

	String usName = name.replace('.', '_').replace('-', '_');
	if (!name.equals(usName) && containsKey(usName)) {
		return usName;
	}

	String ucName = name.toUpperCase();
	if (!name.equals(ucName)) {
		if (containsKey(ucName)) {
			return ucName;
		}
		else {
			String usUcName = ucName.replace('.', '_').replace('-', '_');
			if (!ucName.equals(usUcName) && containsKey(usUcName)) {
				return usUcName;
			}
		}
	}

	return name;
}

Affects: 4.2.2

Referenced from: commits 95d6265

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Oct 25, 2015

Michael Osipov commented

FTR, that is not a dash but a hyphen. If you don't know the difference, check wikipedia.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Dec 22, 2015

Juergen Hoeller commented

Resolved in a slightly refined fashion now, allowing for either dots replaced or hyphens replaced or both replaced for any given property name.

Juergen

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 4.3 RC1 milestone Jan 11, 2019
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: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants