spring-framework
spring-framework copied to clipboard
Add support for list value resolution from YAML block-style in Environment.getProperty()
Overview
This PR enables Environment.getProperty() to retrieve list values defined using YAML block-style notation, aligning its behavior with the already-supported inline style.
Previously, this method supported only inline list syntax (key: i,like,fish) but failed when the YAML used block-style notation:
key:
- i
- like
- fish
As a result, calling:
List<String> values = environment.getProperty("key", (Class<List<String>>) List.class);
would return null for block-style YAML. The issue is tracked in GitHub issue #35179.
Spring’s Environment.getProperty() currently supports retrieving List<String> values only when defined as a comma-separated string. YAML block-style list definitions, which are semantically equivalent, are ignored.
✅ YAML Supported (Inline):
first:
second: i,like,fish
This works as expected:
List<String> values = environment.getProperty("first.second", List.class);
❌ YAML Not Supported (Block-style):
first:
second:
- i
- like
- fish
This returns null for the same code:
List<String> values = environment.getProperty("first.second", List.class);
The Solution
This PR enhances property resolution logic to support indexed keys such as:
first.second[0]=i
first.second[1]=love
first.second[2]=spring
This format corresponds to block-style YAML and is now properly aggregated into a List when calling:
environment.getProperty("first.second", List.class);
Internally, PropertySource#getProperty() is updated to collect indexed entries into a list when a direct match is not found.
Test Coverage
This fix is verified by the following test cases:
@Test
void returnsListOfStringsFromIndexedKeys() { ... }
@Test
void returnsListOfMixedTypesFromIndexedKeys() { ... }
@Test
void returnsListOfIntegersFromIndexedKeys() { ... }
@Test
void returnsNullWhenNoDirectMatchAndNoIndexedKeys() { ... }
These tests ensure list values can be resolved from indexed keys when using Environment.getProperty().
Related Issue
- Fixes: #35179
- [x] I have read the Spring Framework contribution guidelines
- [x] The fix is verified by a test cases
- [x] I included a Signed-off-by trailer in the commit
- [x] I avoided unrelated formatting changes
- [x] I added myself as the author where applicable