Skip to content

Commit 80d3d38

Browse files
authored
Merge pull request #1676 from brendandburns/yaml
Update the CustomConstructor class for SnakeYAML.
2 parents 7f69d7f + 9651f5e commit 80d3d38

File tree

4 files changed

+85
-13
lines changed

4 files changed

+85
-13
lines changed

util/src/main/java/io/kubernetes/client/util/Yaml.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.slf4j.LoggerFactory;
3535
import org.yaml.snakeyaml.DumperOptions;
3636
import org.yaml.snakeyaml.constructor.Constructor;
37+
import org.yaml.snakeyaml.constructor.SafeConstructor;
3738
import org.yaml.snakeyaml.introspector.Property;
3839
import org.yaml.snakeyaml.nodes.MappingNode;
3940
import org.yaml.snakeyaml.nodes.Node;
@@ -78,7 +79,7 @@ public static Object load(File f) throws IOException {
7879
* @throws IOException If an error occurs while reading the YAML.
7980
*/
8081
public static Object load(Reader reader) throws IOException {
81-
Map<String, Object> data = getSnakeYaml().load(reader);
82+
Map<String, Object> data = getSnakeYaml(null).load(reader);
8283
return modelMapper(data);
8384
}
8485

@@ -92,7 +93,7 @@ public static Object load(Reader reader) throws IOException {
9293
* @throws IOException If an error occurs while reading the YAML.
9394
*/
9495
public static <T> T loadAs(String content, Class<T> clazz) {
95-
return getSnakeYaml().loadAs(new StringReader(content), clazz);
96+
return getSnakeYaml(clazz).loadAs(new StringReader(content), clazz);
9697
}
9798

9899
/**
@@ -104,7 +105,7 @@ public static <T> T loadAs(String content, Class<T> clazz) {
104105
* @throws IOException If an error occurs while reading the YAML.
105106
*/
106107
public static <T> T loadAs(File f, Class<T> clazz) throws IOException {
107-
return getSnakeYaml().loadAs(new FileReader(f), clazz);
108+
return getSnakeYaml(clazz).loadAs(new FileReader(f), clazz);
108109
}
109110

110111
/**
@@ -117,7 +118,7 @@ public static <T> T loadAs(File f, Class<T> clazz) throws IOException {
117118
* @throws IOException If an error occurs while reading the YAML.
118119
*/
119120
public static <T> T loadAs(Reader reader, Class<T> clazz) {
120-
return getSnakeYaml().loadAs(reader, clazz);
121+
return getSnakeYaml(clazz).loadAs(reader, clazz);
121122
}
122123

123124
/**
@@ -160,7 +161,7 @@ public static List<Object> loadAll(File f) throws IOException {
160161
* @throws IOException If an error occurs while reading the YAML.
161162
*/
162163
public static List<Object> loadAll(Reader reader) throws IOException {
163-
Iterable<Object> iterable = getSnakeYaml().loadAll(reader);
164+
Iterable<Object> iterable = getSnakeYaml(null).loadAll(reader);
164165
List<Object> list = new ArrayList<Object>();
165166
for (Object object : iterable) {
166167
if (object != null) {
@@ -182,7 +183,7 @@ public static List<Object> loadAll(Reader reader) throws IOException {
182183
* @return A YAML String representing the API object.
183184
*/
184185
public static String dump(Object object) {
185-
return getSnakeYaml().dump(object);
186+
return getSnakeYaml(object.getClass()).dump(object);
186187
}
187188

188189
/**
@@ -192,7 +193,7 @@ public static String dump(Object object) {
192193
* @param writer The writer to write the YAML to.
193194
*/
194195
public static void dump(Object object, Writer writer) {
195-
getSnakeYaml().dump(object, writer);
196+
getSnakeYaml(object.getClass()).dump(object, writer);
196197
}
197198

198199
/**
@@ -202,7 +203,7 @@ public static void dump(Object object, Writer writer) {
202203
* @return A String representing the list of YAML API objects.
203204
*/
204205
public static String dumpAll(Iterator<? extends KubernetesType> data) {
205-
return getSnakeYaml().dumpAll(data);
206+
return getSnakeYaml(null).dumpAll(data);
206207
}
207208

208209
/**
@@ -212,11 +213,15 @@ public static String dumpAll(Iterator<? extends KubernetesType> data) {
212213
* @param output The writer to output the YAML String to.
213214
*/
214215
public static void dumpAll(Iterator<? extends KubernetesType> data, Writer output) {
215-
getSnakeYaml().dumpAll(data, output);
216+
getSnakeYaml(null).dumpAll(data, output);
216217
}
217218

218219
/** Defines constructor logic for custom types in this library. */
219220
public static class CustomConstructor extends Constructor {
221+
public CustomConstructor(Class<?> type) {
222+
super(type);
223+
}
224+
220225
@Override
221226
protected Object constructObject(Node node) {
222227
if (node.getType() == IntOrString.class) {
@@ -225,11 +230,9 @@ protected Object constructObject(Node node) {
225230
if (node.getType() == byte[].class) {
226231
return constructByteArray((ScalarNode) node);
227232
}
228-
229233
if (node.getType() == OffsetDateTime.class) {
230234
return constructDateTime((ScalarNode) node);
231235
}
232-
233236
return super.constructObject(node);
234237
}
235238

@@ -357,8 +360,16 @@ protected NodeTuple representJavaBeanProperty(
357360
}
358361

359362
/** @return An instantiated SnakeYaml Object. */
363+
@Deprecated
360364
public static org.yaml.snakeyaml.Yaml getSnakeYaml() {
361-
return new org.yaml.snakeyaml.Yaml(new CustomConstructor(), new CustomRepresenter());
365+
return getSnakeYaml(null);
366+
}
367+
368+
private static org.yaml.snakeyaml.Yaml getSnakeYaml(Class<?> type) {
369+
if (type != null) {
370+
return new org.yaml.snakeyaml.Yaml(new CustomConstructor(type), new CustomRepresenter());
371+
}
372+
return new org.yaml.snakeyaml.Yaml(new SafeConstructor(), new CustomRepresenter());
362373
}
363374

364375
/**
@@ -381,7 +392,7 @@ private static Object modelMapper(Map<String, Object> data) throws IOException {
381392
throw new IOException(
382393
"Unknown apiVersionKind " + apiVersion + "/" + kind + " is it registered?");
383394
}
384-
return loadAs(new StringReader(getSnakeYaml().dump(data)), clazz);
395+
return loadAs(new StringReader(getSnakeYaml(clazz).dump(data)), clazz);
385396
}
386397

387398
@Deprecated
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.util;
14+
15+
public class TestPoJ {
16+
private static boolean marker = false;
17+
18+
public TestPoJ() {
19+
marker = true;
20+
}
21+
22+
public static boolean hasBeenConstructed() {
23+
return marker;
24+
}
25+
}

util/src/test/java/io/kubernetes/client/util/YamlTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public class YamlTest {
4747

4848
private static final URL CREATED_TIMESTAMP_FILE = Resources.getResource("test-pod.yaml");
4949

50+
private static final URL TAGGED_FILE = Resources.getResource("pod-tag.yaml");
51+
5052
private static final String[] kinds =
5153
new String[] {
5254
"Pod",
@@ -256,4 +258,26 @@ public void testDateTimeRoundTrip() {
256258
assertNull("Unexpected exception: " + ex.toString(), ex);
257259
}
258260
}
261+
262+
@Test
263+
public void testYamlCantConstructObjects() {
264+
try {
265+
String data = Resources.toString(TAGGED_FILE, UTF_8);
266+
Object pod = Yaml.load(data);
267+
} catch (Exception ex) {
268+
// pass
269+
}
270+
assertFalse("Object should not be constructed!", TestPoJ.hasBeenConstructed());
271+
}
272+
273+
@Test
274+
public void testLoadAsYamlCantConstructObjects() {
275+
try {
276+
String data = Resources.toString(TAGGED_FILE, UTF_8);
277+
V1Pod pod = Yaml.loadAs(data, V1Pod.class);
278+
} catch (Exception ex) {
279+
// pass
280+
}
281+
assertFalse("Object should not be constructed!", TestPoJ.hasBeenConstructed());
282+
}
259283
}

util/src/test/resources/pod-tag.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
!!io.kubernetes.client.util.TestPoJ
2+
apiVersion: v1
3+
kind: Pod
4+
metadata:
5+
creationTimestamp: "2018-12-23T01:09:18Z"
6+
generateName: test-776d6c86cc-
7+
labels:
8+
app: test
9+
app-version: "19911"
10+
pod-template-hash: "3328274277"
11+
name: test-776d6c86cc-4zwj5
12+
namespace: default

0 commit comments

Comments
 (0)