Support YAML resources in PropertiesLoaderUtils#37071
Open
jyx-07 wants to merge 1 commit into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PropertiesLoaderUtilscurrently only special-cases the.xmlfileextension; any other filename (including
.yml/.yaml) falls throughto the plain
java.util.Propertiesparser, which cannot make sense ofYAML content.
This PR teaches
PropertiesLoaderUtilsto recognize.yml/.yamlresources and parse them as YAML, flattening nested maps and lists into
dotted/indexed property keys — the same flattening behavior already
provided by
YamlPropertiesFactoryBeaninspring-beans.Since
spring-corecannot depend onspring-beans(wrong direction)and has historically had no hard dependency on SnakeYAML, this PR:
org.yaml:snakeyamlas anoptionaldependency ofspring-core(same mechanism already used for Reactor, RxJava, Mutiny, and Kotlin
in this module).
ClassUtils.isPresent(...).(
SnakeYamlPropertiesLoader), so its bytecode — and therefore theorg.yaml.snakeyaml.*classes — is only resolved once a.yml/.yamlresource is actually parsed. This mirrors the existing soft-dependency
pattern used by
KotlinDetector/Nullness.KotlinDelegateandReactiveAdapterRegistry's reactive-library registrars..yml/.yamlresource is loaded while SnakeYAML is absent fromthe classpath, an
IllegalStateExceptionwith a clear message isthrown rather than silently mis-parsing the file as
.properties..xml/.propertiescode paths untouched.Closes gh-37066
Test plan
PropertiesLoaderUtilsTestscovering: flat YAML (.yml),flat YAML (
.yaml), nested maps/lists flattening, multi-documentYAML merging,
EncodedResource-based loading, andloadAllProperties(String)classpath loading../gradlew :spring-core:test— all tests pass (no regressions)../gradlew :spring-core:check— checkstyle, ArchUnit, andmulti-release jar validation all pass.