Skip to content

Commit 9651f5e

Browse files
brendanburnsbrendandburns
authored andcommitted
Add a test to validate loading.
1 parent ccfcc91 commit 9651f5e

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,12 @@ protected NodeTuple representJavaBeanProperty(
360360
}
361361

362362
/** @return An instantiated SnakeYaml Object. */
363-
public static org.yaml.snakeyaml.Yaml getSnakeYaml(Class<?> type) {
363+
@Deprecated
364+
public static org.yaml.snakeyaml.Yaml getSnakeYaml() {
365+
return getSnakeYaml(null);
366+
}
367+
368+
private static org.yaml.snakeyaml.Yaml getSnakeYaml(Class<?> type) {
364369
if (type != null) {
365370
return new org.yaml.snakeyaml.Yaml(new CustomConstructor(type), new CustomRepresenter());
366371
}
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)