Skip to content

Clarify documentation regarding relaxed binding of environment variables #22974

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

Closed
Maxeh opened this issue Aug 16, 2020 · 3 comments
Closed

Clarify documentation regarding relaxed binding of environment variables #22974

Maxeh opened this issue Aug 16, 2020 · 3 comments
Assignees
Labels
type: documentation A documentation update
Milestone

Comments

@Maxeh
Copy link

Maxeh commented Aug 16, 2020

It seems that there are wrong information in the docs in the section Binding from Environment Variables. A green info section states the following:

Underscores cannot be used to replace the dashes in property names. If you attempt to use SPRING_MAIN_LOG_STARTUP_INFO with the example above, no value will be bound.

After some testing with the newest version of spring boot it turns out that it is actually possible. For example we can not only bind the environment variable TEST_MYPROJECT_MYVALUE but also TEST_MY_PROJECT_MY_VALUE to @ConfigurationPropertis and @Value like this:

@ConfigurationProperties("test.my-project")
public class MyValueConfig {
  private String myValue;
}
@Value("${test.my-project.my-value}")

So this section in the docs should be adjusted I guess. Moreover, in section @ConfigurationProperties vs. @Value I do not understand why Relaxed Binding is marked as "limited" for @Value. From my testing all environment name combinations could be used for both @ConfigurationProperties and @Value. E.g. all of those combinations could be bound to the @ConfigurationProperties and @Value shown above:

test.my-project.my-value=hello
test.myProject.my-value=hello
TEST.myProject.my-value=hello
TEST.MY_PROJECT.my-value=hello
TEST.MY-PROJECT.my-value=hello
TEST_MYPROJECT_MYVALUE=hello
TEST_MY_PROJECT_MY_VALUE=hello

I could not find a single combination that was working for @ConfigurationProperties but not for @Value.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 16, 2020
@snicoll
Copy link
Member

snicoll commented Aug 17, 2020

Thanks for the report. There has been some improvements in relaxed binding that makes it more ubiquitous and I agree the section you've referenced could use a review.

@snicoll snicoll added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 17, 2020
@snicoll snicoll added this to the 2.2.x milestone Aug 17, 2020
@yayayahei
Copy link

hahahaha, have found the same problem, and I've asked at gitter but got no response. I'm suprised to find this issue.

@snicoll snicoll changed the title Documentation for config properties is incorrect Relaxed binding examples to environment variables is outdated Aug 30, 2020
@snicoll snicoll added the for: team-attention An issue we'd like other members of the team to review label Aug 31, 2020
@philwebb philwebb removed the for: team-attention An issue we'd like other members of the team to review label Sep 16, 2020
@wilkinsona
Copy link
Member

#10873 added support for replacing - with _. It was added for compatibility with 1.5 and we don't want to encourage its usage. That said, the current note in the docs is inaccurate so I think we should remove it.

Relaxed binding is working with @Value as the canonical, kebab-case form is being used:

@Value("${test.my-project.my-value}")

This is covered in the paragraph to which the limited entry in the table refers:

If you do want to use @Value, we recommend that you refer to property names using their canonical form (kebab-case using only lowercase letters). This will allow Spring Boot to use the same logic as it does when relaxed binding @ConfigurationProperties. For example, @Value("{demo.item-price}") will pick up demo.item-price and demo.itemPrice forms from the application.properties file, as well as DEMO_ITEMPRICE from the system environment. If you used @Value("{demo.itemPrice}") instead, demo.item-price and DEMO_ITEMPRICE would not be considered.

The table refers to it is a note, but it is a plain paragraph in the documentation. I think we could improve things by turning it into a NOTE:.

@wilkinsona wilkinsona self-assigned this Nov 4, 2020
@wilkinsona wilkinsona changed the title Relaxed binding examples to environment variables is outdated Clarify documentation regarding relaxed binding of environment variables Nov 4, 2020
@wilkinsona wilkinsona modified the milestones: 2.2.x, 2.2.12 Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

6 participants