Skip to content

Can't construct a java object for scalar tag:yaml.org,2002:timestamp; No String constructor found. #368

@mattnworb

Description

@mattnworb

In an application using io.kubernetes:client-java:2.0.0, I have some code that queries a cluster to fetch pods that meet a certain condition and then transform the data in various ways.

In writing a test of this code, I am trying to (in my unit test) load YAML returned by kubectl get pod/foobar... -o yaml to construct a real-like V1Pod for the code-under-test to work with, with code like:

InputStream stream = this.getClass().getResourceAsStream("crashing-pod.yaml");
V1Pod pod;
try (InputStreamReader reader = new InputStreamReader(stream)) {
  pod = Yaml.loadAs(reader, V1Pod.class);
}

With crashing-pod.yaml looking like:

apiVersion: v1
kind: Pod
metadata:
  annotations:
    podpreset.admission.spotify.com/revision: 6104d05
  creationTimestamp: 2018-09-06T15:12:24Z
  ...

This throws an exception, seemingly because the snakeyaml reader is not configured to know how to deserialize strings into org.joda.time.DateTime instances:

Cannot create property=metadata for JavaBean=class V1Pod {
    apiVersion: v1
    kind: Pod
    metadata: null
    spec: null
    status: null
}
 in 'reader', line 1, column 1:
    apiVersion: v1
    ^
Cannot create property=creationTimestamp for JavaBean=class V1ObjectMeta {
    annotations: {podpreset.admission.spotify.com/revision=6104d05}
    clusterName: null
    creationTimestamp: null
    deletionGracePeriodSeconds: null
    deletionTimestamp: null
    finalizers: null
    generateName: null
    generation: null
    initializers: null
    labels: null
    name: null
    namespace: null
    ownerReferences: null
    resourceVersion: null
    selfLink: null
    uid: null
}
 in 'reader', line 4, column 3:
      annotations:
      ^
Can't construct a java object for scalar tag:yaml.org,2002:timestamp; No String constructor found. Exception=org.joda.time.DateTime.<init>(java.lang.String)
 in 'reader', line 6, column 22:
      creationTimestamp: 2018-09-06T15:12:24Z
                         ^

 in 'reader', line 4, column 3:
      annotations:
      ^


	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:313)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:190)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:346)
	at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:184)
	at io.kubernetes.client.util.Yaml$CustomConstructor.constructObject(Yaml.java:272)
	at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:143)
	at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:129)
	at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:505)
	at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:465)
	at io.kubernetes.client.util.Yaml.loadAs(Yaml.java:207)
	at com.spotify.tugboat.apiservice.kubernetes.KubeInstallationStatusFetcherTest.testCrashLoopingPod(KubeInstallationStatusFetcherTest.java:158)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: Cannot create property=creationTimestamp for JavaBean=class V1ObjectMeta {
    annotations: {podpreset.admission.spotify.com/revision=6104d05}
    clusterName: null
    creationTimestamp: null
    deletionGracePeriodSeconds: null
    deletionTimestamp: null
    finalizers: null
    generateName: null
    generation: null
    initializers: null
    labels: null
    name: null
    namespace: null
    ownerReferences: null
    resourceVersion: null
    selfLink: null
    uid: null
}
 in 'reader', line 4, column 3:
      annotations:
      ^
Can't construct a java object for scalar tag:yaml.org,2002:timestamp; No String constructor found. Exception=org.joda.time.DateTime.<init>(java.lang.String)
 in 'reader', line 6, column 22:
      creationTimestamp: 2018-09-06T15:12:24Z
                         ^

	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:313)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:190)
	at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:184)
	at io.kubernetes.client.util.Yaml$CustomConstructor.constructObject(Yaml.java:272)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:298)
	... 32 more
Caused by: org.yaml.snakeyaml.error.YAMLException: Can't construct a java object for scalar tag:yaml.org,2002:timestamp; No String constructor found. Exception=org.joda.time.DateTime.<init>(java.lang.String)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructScalar.construct(Constructor.java:412)
	at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:184)
	at io.kubernetes.client.util.Yaml$CustomConstructor.constructObject(Yaml.java:272)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:298)
	... 36 more
Caused by: java.lang.NoSuchMethodException: org.joda.time.DateTime.<init>(java.lang.String)
	at java.lang.Class.getConstructor0(Class.java:3082)
	at java.lang.Class.getDeclaredConstructor(Class.java:2178)
	at org.yaml.snakeyaml.constructor.Constructor$ConstructScalar.construct(Constructor.java:410)
	... 39 more

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions