Skip to content

Add support for list value resolution from YAML block-style in Environment.getProperty() #35204

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
2 participants