Skip to content

Conversation

@xyraclius
Copy link

@xyraclius xyraclius commented Jul 15, 2025

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 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


  • I have read the Spring Framework contribution guidelines
  • The fix is verified by a test cases
  • I included a Signed-off-by trailer in the commit
  • I avoided unrelated formatting changes
  • I added myself as the author where applicable

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 15, 2025
@xyraclius xyraclius changed the title Environment.getProperty() can retrieve list items from YAML config Add support for list value resolution from YAML block-style in Environment.getProperty() Jul 27, 2025
@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 4, 2025
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) status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Environment.getProperty() method cannot process list items

3 participants