Skip to content

Commit fbc3f25

Browse files
committed
use sigs.k8s.io/json to unmarshal in fakeclient
Signed-off-by: Troy Connor <[email protected]>
1 parent b6c5897 commit fbc3f25

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pkg/client/fake/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package fake
1919
import (
2020
"bytes"
2121
"context"
22-
"encoding/json"
2322
"errors"
2423
"fmt"
2524
"reflect"
@@ -58,6 +57,7 @@ import (
5857
"k8s.io/apimachinery/pkg/runtime"
5958
"k8s.io/apimachinery/pkg/runtime/schema"
6059
"k8s.io/apimachinery/pkg/types"
60+
"k8s.io/apimachinery/pkg/util/json"
6161
utilrand "k8s.io/apimachinery/pkg/util/rand"
6262
"k8s.io/apimachinery/pkg/util/sets"
6363
"k8s.io/apimachinery/pkg/util/strategicpatch"

pkg/client/fake/client_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,38 @@ var _ = Describe("Fake client", func() {
18101810
Expect(cmp.Diff(objOriginal, actual)).To(BeEmpty())
18111811
})
18121812

1813+
It("should Unmarshal the unstructured object with int64 to preserve ints", func() {
1814+
obj := &unstructured.Unstructured{}
1815+
obj.SetAPIVersion("foo/v1")
1816+
obj.SetKind("Foo")
1817+
obj.SetName("a-foo")
1818+
1819+
err := unstructured.SetNestedMap(obj.Object, map[string]any{"value": json.Number("999")}, "spec")
1820+
Expect(err).NotTo(HaveOccurred())
1821+
1822+
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
1823+
Expect(cl.Update(context.Background(), obj)).To(Succeed())
1824+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)).To(Succeed())
1825+
1826+
Expect(obj.Object["spec"]).To(BeEquivalentTo(map[string]any{"value": int64(999)}))
1827+
})
1828+
1829+
It("should Unmarshal the unstructured object with float64 to preserve ints", func() {
1830+
obj := &unstructured.Unstructured{}
1831+
obj.SetAPIVersion("foo/v1")
1832+
obj.SetKind("Foo")
1833+
obj.SetName("a-foo")
1834+
1835+
err := unstructured.SetNestedMap(obj.Object, map[string]any{"value": 99.9}, "spec")
1836+
Expect(err).NotTo(HaveOccurred())
1837+
1838+
cl := NewClientBuilder().WithStatusSubresource(obj).WithObjects(obj).Build()
1839+
Expect(cl.Update(context.Background(), obj)).To(Succeed())
1840+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(obj), obj)).To(Succeed())
1841+
1842+
Expect(obj.Object["spec"]).To(BeEquivalentTo(map[string]any{"value": float64(99.9)}))
1843+
})
1844+
18131845
It("should not change the status of unstructured objects that are configured to have a status subresource on update", func() {
18141846
obj := &unstructured.Unstructured{}
18151847
obj.SetAPIVersion("foo/v1")

0 commit comments

Comments
 (0)