Skip to content

Support YAML resources in PropertiesLoaderUtils#37071

Open
jyx-07 wants to merge 1 commit into
spring-projects:mainfrom
jyx-07:gh-37066-yaml-support-properties-loader-utils
Open

Support YAML resources in PropertiesLoaderUtils#37071
jyx-07 wants to merge 1 commit into
spring-projects:mainfrom
jyx-07:gh-37066-yaml-support-properties-loader-utils

Conversation

@jyx-07

@jyx-07 jyx-07 commented Jul 20, 2026

Copy link
Copy Markdown

Summary

PropertiesLoaderUtils currently only special-cases the .xml file
extension; any other filename (including .yml/.yaml) falls through
to the plain java.util.Properties parser, which cannot make sense of
YAML content.

This PR teaches PropertiesLoaderUtils to recognize .yml/.yaml
resources and parse them as YAML, flattening nested maps and lists into
dotted/indexed property keys — the same flattening behavior already
provided by YamlPropertiesFactoryBean in spring-beans.

Since spring-core cannot depend on spring-beans (wrong direction)
and has historically had no hard dependency on SnakeYAML, this PR:

  • Adds org.yaml:snakeyaml as an optional dependency of spring-core
    (same mechanism already used for Reactor, RxJava, Mutiny, and Kotlin
    in this module).
  • Detects SnakeYAML at runtime via ClassUtils.isPresent(...).
  • Isolates all direct SnakeYAML API usage inside a private nested class
    (SnakeYamlPropertiesLoader), so its bytecode — and therefore the
    org.yaml.snakeyaml.* classes — is only resolved once a .yml/.yaml
    resource is actually parsed. This mirrors the existing soft-dependency
    pattern used by KotlinDetector/Nullness.KotlinDelegate and
    ReactiveAdapterRegistry's reactive-library registrars.
  • If a .yml/.yaml resource is loaded while SnakeYAML is absent from
    the classpath, an IllegalStateException with a clear message is
    thrown rather than silently mis-parsing the file as .properties.
  • Leaves the existing .xml/.properties code paths untouched.

Closes gh-37066

Test plan

  • Added PropertiesLoaderUtilsTests covering: flat YAML (.yml),
    flat YAML (.yaml), nested maps/lists flattening, multi-document
    YAML merging, EncodedResource-based loading, and
    loadAllProperties(String) classpath loading.
  • ./gradlew :spring-core:test — all tests pass (no regressions).
  • ./gradlew :spring-core:check — checkstyle, ArchUnit, and
    multi-release jar validation all pass.

PropertiesLoaderUtils previously only recognized the .xml file
extension as a special case, falling back to the standard
java.util.Properties parser for anything else. This meant .yml
and .yaml resources were parsed as if they were .properties
files, producing garbage or throwing an exception.

This commit teaches PropertiesLoaderUtils to detect .yml/.yaml
filenames and parse them as YAML, flattening nested maps and
lists into dotted/indexed property keys, mirroring the behavior
of YamlPropertiesFactoryBean in spring-beans.

Since spring-core has no hard dependency on spring-beans or
SnakeYAML, SnakeYAML is added as an optional dependency and
detected at runtime via ClassUtils.isPresent(), following the
same soft-dependency pattern already used for Reactor, RxJava,
and Kotlin in this module. The actual SnakeYAML API calls are
isolated in a private nested class so that its bytecode is only
loaded when a YAML resource is actually parsed, keeping the
XML/.properties code path completely unaffected when SnakeYAML
is absent from the classpath.

Closes spring-projectsgh-37066

Signed-off-by: jyx-07 <s25069@gsm.hs.kr>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 20, 2026
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.

YAML support under PropertiesLoaderUtils

2 participants