Skip to content

Commit d1d990a

Browse files
wilkinsonaphilwebb
authored andcommitted
Improve compatibility with SnakeYAML 2.0
Closes gh-35414
1 parent 307f3c3 commit d1d990a

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedYamlLoader.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.boot.env;
1818

19+
import java.lang.invoke.MethodHandle;
20+
import java.lang.invoke.MethodHandles;
21+
import java.lang.invoke.MethodType;
1922
import java.util.ArrayList;
2023
import java.util.List;
2124
import java.util.Map;
@@ -45,6 +48,7 @@
4548
import org.springframework.boot.origin.TextResourceOrigin;
4649
import org.springframework.boot.origin.TextResourceOrigin.Location;
4750
import org.springframework.core.io.Resource;
51+
import org.springframework.util.ReflectionUtils;
4852

4953
/**
5054
* Class to load {@code .yml} files into a map of {@code String} to
@@ -73,8 +77,8 @@ protected Yaml createYaml() {
7377

7478
private Yaml createYaml(LoaderOptions loaderOptions) {
7579
BaseConstructor constructor = new OriginTrackingConstructor(loaderOptions);
76-
Representer representer = new Representer();
7780
DumperOptions dumperOptions = new DumperOptions();
81+
Representer representer = new Representer(dumperOptions);
7882
LimitedResolver resolver = new LimitedResolver();
7983
return new Yaml(constructor, representer, dumperOptions, loaderOptions, resolver);
8084
}
@@ -167,7 +171,12 @@ private static Node get(Node node) {
167171
/**
168172
* {@link Resolver} that limits {@link Tag#TIMESTAMP} tags.
169173
*/
170-
private static class LimitedResolver extends Resolver {
174+
static class LimitedResolver extends Resolver {
175+
176+
private static final MethodType SNAKE_YAML_2_ADD_IMPLICIT_RESOLVER_METHOD_TYPE = MethodType
177+
.methodType(void.class, Tag.class, Pattern.class, String.class, int.class);
178+
179+
private volatile MethodHandle snakeYaml2ImplicitResolverMethod;
171180

172181
@Override
173182
public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
@@ -177,6 +186,30 @@ public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
177186
super.addImplicitResolver(tag, regexp, first);
178187
}
179188

189+
public void addImplicitResolver(Tag tag, Pattern regexp, String first, int limit) {
190+
// Support for SnakeYAML 2.0
191+
if (tag == Tag.TIMESTAMP) {
192+
return;
193+
}
194+
try {
195+
getSnakeYaml2ImplicitResolverMethod().invoke(this, tag, regexp, first, limit);
196+
}
197+
catch (Throwable ex) {
198+
ReflectionUtils.rethrowRuntimeException(ex);
199+
}
200+
}
201+
202+
private MethodHandle getSnakeYaml2ImplicitResolverMethod()
203+
throws NoSuchMethodException, IllegalAccessException {
204+
MethodHandle snakeYaml2ImplicitResolverMethod = this.snakeYaml2ImplicitResolverMethod;
205+
if (snakeYaml2ImplicitResolverMethod == null) {
206+
snakeYaml2ImplicitResolverMethod = MethodHandles.lookup().findSpecial(Resolver.class,
207+
"addImplicitResolver", SNAKE_YAML_2_ADD_IMPLICIT_RESOLVER_METHOD_TYPE, getClass());
208+
this.snakeYaml2ImplicitResolverMethod = snakeYaml2ImplicitResolverMethod;
209+
}
210+
return snakeYaml2ImplicitResolverMethod;
211+
}
212+
180213
}
181214

182215
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.env;
18+
19+
import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
20+
21+
/**
22+
* Tests for {@link YamlPropertySourceLoader} with SnakeYAML 2.0.
23+
*
24+
* @author Andy Wilkinson
25+
*/
26+
@ClassPathOverrides("org.yaml:snakeyaml:2.0")
27+
class YamlPropertySourceLoaderSnakeYaml20Tests extends YamlPropertySourceLoaderTests {
28+
29+
}

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@
5454
<suppress files="ValidatorPropertiesWithDefaultValues\.java" checks="SpringMethodVisibility" />
5555
<suppress files="DevToolsR2dbcAutoConfigurationTests" checks="HideUtilityClassConstructor" />
5656
<suppress files="AbstractLaunchScriptIntegrationTests" checks="IllegalImport" />
57+
<suppress files="OriginTrackedYamlLoader" checks="SpringMethodVisibility" message="addImplicitResolver" />
5758
</suppressions>

0 commit comments

Comments
 (0)