Skip to content

Commit c4ddd55

Browse files
authored
set unmarshalling JSON with DiscardUnknown option (#7339)
1 parent ef5e03c commit c4ddd55

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

aks-node-controller/parser/testdata/test_aksnodeconfig_fields_unexpected.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"bootstrapping_config": {
44
"bootstrapping_auth_method": "BOOTSTRAPPING_AUTH_METHOD_BOOTSTRAP_TOKEN"
55
},
6+
"unexpected_new_field1": "unexpected_new_value1",
67
"cluster_config": {
78
"vm_type": 2,
89
"cluster_network_config": {
@@ -19,8 +20,7 @@
1920
"gpu_config": {
2021
"config_gpu_driver": true
2122
},
22-
"unexpected_new_field1": "unexpected_new_value1",
2323
"unexpected_new_config2": {
2424
"unexpected_new_field2": "unexpected_new_value2"
2525
}
26-
}
26+
}

aks-node-controller/pkg/nodeconfigutils/utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ func MarshalConfigurationV1(cfg *aksnodeconfigv1.Configuration) ([]byte, error)
4040

4141
func UnmarshalConfigurationV1(data []byte) (*aksnodeconfigv1.Configuration, error) {
4242
cfg := &aksnodeconfigv1.Configuration{}
43-
options := protojson.UnmarshalOptions{}
43+
options := protojson.UnmarshalOptions{
44+
DiscardUnknown: true, // ignore unknown fields to allow forward compatibility
45+
}
4446
err := options.Unmarshal(data, cfg)
4547
return cfg, err
4648
}

aks-node-controller/pkg/nodeconfigutils/utils_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,24 @@ func TestUnmarshalConfigurationV1(t *testing.T) {
6060
wantErr: true,
6161
},
6262
{
63-
name: "unknown field should error",
63+
name: "string is assigned with a boolean value",
64+
data: []byte(`{
65+
"version": "v1",
66+
"LinuxAdminUsername": true,
67+
"authConfig": {
68+
"subscriptionId": "test-subscription"
69+
}
70+
}`),
71+
want: &aksnodeconfigv1.Configuration{
72+
Version: "v1",
73+
AuthConfig: &aksnodeconfigv1.AuthConfig{
74+
SubscriptionId: "test-subscription",
75+
},
76+
},
77+
wantErr: false,
78+
},
79+
{
80+
name: "unknown field should be ignored",
6481
data: []byte(`{
6582
"version": "v1",
6683
"unknownField": "should be ignored",
@@ -74,7 +91,7 @@ func TestUnmarshalConfigurationV1(t *testing.T) {
7491
SubscriptionId: "test-subscription",
7592
},
7693
},
77-
wantErr: true,
94+
wantErr: false,
7895
},
7996
}
8097

0 commit comments

Comments
 (0)