Skip to content

Commit 2cf63d6

Browse files
authored
[3.x] Adapt to SnakeYAML 2.0 changes (#5793)
1 parent 511c1e2 commit 2cf63d6

File tree

22 files changed

+844
-41
lines changed

22 files changed

+844
-41
lines changed

config/yaml-mp/src/main/java/io/helidon/config/yaml/mp/YamlMpConfigSource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
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.
@@ -39,6 +39,7 @@
3939
import io.helidon.config.MutabilitySupport;
4040

4141
import org.eclipse.microprofile.config.spi.ConfigSource;
42+
import org.yaml.snakeyaml.LoaderOptions;
4243
import org.yaml.snakeyaml.Yaml;
4344
import org.yaml.snakeyaml.constructor.SafeConstructor;
4445

@@ -380,7 +381,7 @@ public String getName() {
380381
static Map toMap(Reader reader) {
381382
// the default of Snake YAML is a Map, safe constructor makes sure we never deserialize into anything
382383
// harmful
383-
Yaml yaml = new Yaml(new SafeConstructor());
384+
Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
384385
return (Map) yaml.loadAs(reader, Object.class);
385386
}
386387
}

config/yaml/src/main/java/io/helidon/config/yaml/YamlConfigParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
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.
@@ -29,6 +29,7 @@
2929
import io.helidon.config.spi.ConfigParserException;
3030

3131
import jakarta.annotation.Priority;
32+
import org.yaml.snakeyaml.LoaderOptions;
3233
import org.yaml.snakeyaml.Yaml;
3334
import org.yaml.snakeyaml.constructor.SafeConstructor;
3435

@@ -113,7 +114,7 @@ public ObjectNode parse(Content content) throws ConfigParserException {
113114
static Map toMap(Reader reader) {
114115
// the default of Snake YAML is a Map, safe constructor makes sure we never deserialize into anything
115116
// harmful
116-
Yaml yaml = new Yaml(new SafeConstructor());
117+
Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions()));
117118
return (Map) yaml.loadAs(reader, Object.class);
118119
}
119120

dependencies/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
<version.lib.prometheus>0.9.0</version.lib.prometheus>
142142
<version.lib.slf4j>1.7.32</version.lib.slf4j>
143143
<version.lib.smallrye-openapi>2.1.16</version.lib.smallrye-openapi>
144-
<version.lib.snakeyaml>1.32</version.lib.snakeyaml>
144+
<version.lib.snakeyaml>2.0</version.lib.snakeyaml>
145145
<version.lib.typesafe-config>1.4.2</version.lib.typesafe-config>
146146
<version.lib.tyrus>2.0.4</version.lib.tyrus>
147147
<version.lib.weld-api>4.0.SP1</version.lib.weld-api>

examples/todo-app/backend/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<version.datastax.driver.core>4.9.0</version.datastax.driver.core>
4343
<version.datastax.driver.query.builder>4.9.0</version.datastax.driver.query.builder>
4444
<version.codahale.metrics.core>3.0.2</version.codahale.metrics.core>
45+
<version.lib.snakeyaml.override>1.32</version.lib.snakeyaml.override>
4546
</properties>
4647

4748
<dependencyManagement>
@@ -57,6 +58,11 @@
5758
</exclusion>
5859
</exclusions>
5960
</dependency>
61+
<dependency>
62+
<groupId>org.yaml</groupId>
63+
<artifactId>snakeyaml</artifactId>
64+
<version>${version.lib.snakeyaml.override}</version>
65+
</dependency>
6066
</dependencies>
6167
</dependencyManagement>
6268

microprofile/tests/tck/tck-openapi/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@
3030
<artifactId>tck-openapi</artifactId>
3131
<name>Helidon Microprofile Tests TCK OpenAPI</name>
3232

33+
<properties>
34+
<selectedSnakeYamlVersion>1.32</selectedSnakeYamlVersion>
35+
</properties>
36+
37+
<!-- The TCK uses Jackson which in turn uses SnakeYAML APIs that have disappeared in 2.0. So, for running the TCK
38+
use the earlier SnakeYAML release (until an updated Jackson release becomes available). -->
39+
<dependencyManagement>
40+
<dependencies>
41+
<dependency>
42+
<groupId>org.yaml</groupId>
43+
<artifactId>snakeyaml</artifactId>
44+
<version>${selectedSnakeYamlVersion}</version>
45+
</dependency>
46+
</dependencies>
47+
</dependencyManagement>
48+
3349
<dependencies>
3450
<dependency>
3551
<groupId>io.helidon.microprofile.tests</groupId>

openapi/src/main/java/io/helidon/openapi/CustomConstructor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
2+
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
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.
@@ -30,6 +30,7 @@
3030
import org.eclipse.microprofile.openapi.models.responses.APIResponse;
3131
import org.eclipse.microprofile.openapi.models.responses.APIResponses;
3232
import org.eclipse.microprofile.openapi.models.security.SecurityRequirement;
33+
import org.yaml.snakeyaml.LoaderOptions;
3334
import org.yaml.snakeyaml.TypeDescription;
3435
import org.yaml.snakeyaml.constructor.Constructor;
3536
import org.yaml.snakeyaml.error.Mark;
@@ -149,7 +150,7 @@ record ChildMapType<P, C>(Class<P> parentType,
149150
Function<Class<?>, ExpandedTypeDescription.MapLikeTypeDescription<P, C>> typeDescriptionFactory) { }
150151

151152
/**
152-
* Type information about a map-resembling interface in which a child can have 0, 1, or more values i.e., the child is
153+
* Type information about a map-resembling interface in which a child can have 0, 1, or more values (i.e., the child is
153154
* a list).
154155
*
155156
* @param <P> parent type
@@ -166,7 +167,7 @@ record ChildMapListType<P, C>(
166167
private static final Logger LOGGER = Logger.getLogger(CustomConstructor.class.getName());
167168

168169
CustomConstructor(TypeDescription td) {
169-
super(td);
170+
super(td, new LoaderOptions());
170171
yamlClassConstructors.put(NodeId.mapping, new ConstructMapping());
171172
}
172173

openapi/src/main/java/io/helidon/openapi/ImplTypeDescription.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
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.
@@ -61,46 +61,18 @@ public Tag getTag() {
6161
return delegate.getTag();
6262
}
6363

64-
@Override
65-
public void setTag(Tag tag) {
66-
delegate.setTag(tag);
67-
}
68-
69-
@Override
70-
public void setTag(String tag) {
71-
delegate.setTag(tag);
72-
}
73-
7464
@Override
7565
@Deprecated
7666
public void putListPropertyType(String property, Class<?> type) {
7767
delegate.putListPropertyType(property, type);
7868
}
7969

80-
@Override
81-
@Deprecated
82-
public Class<? extends Object> getListPropertyType(String property) {
83-
return delegate.getListPropertyType(property);
84-
}
85-
8670
@Override
8771
@Deprecated
8872
public void putMapPropertyType(String property, Class<?> key, Class<?> value) {
8973
delegate.putMapPropertyType(property, key, value);
9074
}
9175

92-
@Override
93-
@Deprecated
94-
public Class<? extends Object> getMapKeyType(String property) {
95-
return delegate.getMapKeyType(property);
96-
}
97-
98-
@Override
99-
@Deprecated
100-
public Class<? extends Object> getMapValueType(String property) {
101-
return delegate.getMapValueType(property);
102-
}
103-
10476
@Override
10577
public void addPropertyParameters(String pName, Class<?>... classes) {
10678
delegate.addPropertyParameters(pName, classes);

openapi/src/main/java/io/helidon/openapi/OpenAPISupport.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
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.
@@ -86,6 +86,7 @@
8686
import org.jboss.jandex.IndexView;
8787
import org.yaml.snakeyaml.TypeDescription;
8888
import org.yaml.snakeyaml.introspector.Property;
89+
import org.yaml.snakeyaml.introspector.PropertySubstitute;
8990

9091
import static io.helidon.webserver.cors.CorsEnabledServiceHelper.CORS_CONFIG_KEY;
9192

@@ -148,6 +149,11 @@ MediaType mediaType() {
148149

149150
private static final JsonReaderFactory JSON_READER_FACTORY = Json.createReaderFactory(Collections.emptyMap());
150151

152+
// As a static we keep a reference to the logger, thereby making sure any changes we make are persistent. (JUL holds
153+
// only weak references to loggers internally.)
154+
private static final Logger SNAKE_YAML_INTROSPECTOR_LOGGER =
155+
Logger.getLogger(PropertySubstitute.class.getPackage().getName());
156+
151157
/**
152158
* The SnakeYAMLParserHelper is generated by a maven plug-in.
153159
*/
@@ -262,6 +268,11 @@ private void registerJsonpSupport(ServerRequest req, ServerResponse res) {
262268
static SnakeYAMLParserHelper<ExpandedTypeDescription> helper() {
263269
return access(HELPER_ACCESS, () -> {
264270
if (helper == null) {
271+
Config config = Config.create();
272+
boolean allowSnakeYamlWarnings = (config.get("openapi.parsing.warnings.enabled").asBoolean().orElse(false));
273+
if (SNAKE_YAML_INTROSPECTOR_LOGGER.isLoggable(Level.WARNING) && !allowSnakeYamlWarnings) {
274+
SNAKE_YAML_INTROSPECTOR_LOGGER.setLevel(Level.SEVERE);
275+
}
265276
helper = SnakeYAMLParserHelper.create(ExpandedTypeDescription::create);
266277
adjustTypeDescriptions(helper.types());
267278
}

openapi/src/main/java/io/helidon/openapi/Serializer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
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.
@@ -103,16 +103,15 @@ static class CustomRepresenter extends Representer {
103103

104104
private static final String EXTENSIONS = "extensions";
105105

106-
private final DumperOptions dumperOptions;
107106
private final DumperOptions.ScalarStyle stringStyle;
108107

109108
private final Map<Class<?>, ExpandedTypeDescription> implsToTypes;
110109

111110
CustomRepresenter(Map<Class<?>, ExpandedTypeDescription> types,
112111
Map<Class<?>, ExpandedTypeDescription> implsToTypes, DumperOptions dumperOptions,
113112
DumperOptions.ScalarStyle stringStyle) {
113+
super(dumperOptions);
114114
this.implsToTypes = implsToTypes;
115-
this.dumperOptions = dumperOptions;
116115
this.stringStyle = stringStyle;
117116
types.values().stream()
118117
.map(ImplTypeDescription::new)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/*

0 commit comments

Comments
 (0)