From ade6c5fea647e1139a7efe7dd665d62510d4940d Mon Sep 17 00:00:00 2001 From: Adar Maor Date: Tue, 8 Nov 2022 15:56:31 +0200 Subject: [PATCH] made boolean omitempty values references --- pkg/notifications/channels_test.go | 3 +- pkg/notifications/types.go | 10 +- pkg/notifications/types_unit_test.go | 136 +++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 6 deletions(-) create mode 100644 pkg/notifications/types_unit_test.go diff --git a/pkg/notifications/channels_test.go b/pkg/notifications/channels_test.go index a7158d13c..7fd892610 100644 --- a/pkg/notifications/channels_test.go +++ b/pkg/notifications/channels_test.go @@ -229,6 +229,7 @@ func TestUpdateChannel(t *testing.T) { t.Parallel() respJSON := fmt.Sprintf(`{ "data":%s }`, testUpdateChannelResponseJSON) notifications := newMockResponse(t, respJSON, http.StatusCreated) + var falseValue = false updateChannelInput := AiNotificationsChannelUpdate{ Name: "test-notification-channel-1-update", @@ -239,7 +240,7 @@ func TestUpdateChannel(t *testing.T) { Value: "{\\n\\t\\\"id\\\": \\\"test-update\\\"\\n}", }, }, - Active: false, + Active: &falseValue, } expected := &AiNotificationsChannelResponse{ diff --git a/pkg/notifications/types.go b/pkg/notifications/types.go index 241409d4d..e3b64e767 100644 --- a/pkg/notifications/types.go +++ b/pkg/notifications/types.go @@ -589,7 +589,7 @@ type AiNotificationsChannel struct { // AiNotificationsChannelFilter - Filter channel object type AiNotificationsChannelFilter struct { // active - Active bool `json:"active,omitempty"` + Active *bool `json:"active,omitempty"` // destinationId DestinationId string `json:"destinationId,omitempty"` // id @@ -639,7 +639,7 @@ type AiNotificationsChannelSorter struct { // AiNotificationsChannelUpdate - Channel update object type AiNotificationsChannelUpdate struct { // active - Active bool `json:"active,omitempty"` + Active *bool `json:"active,omitempty"` // name Name string `json:"name,omitempty"` // properties @@ -715,7 +715,7 @@ type AiNotificationsDestination struct { // AiNotificationsDestinationFilter - Filter destination object type AiNotificationsDestinationFilter struct { // active - Active bool `json:"active,omitempty"` + Active *bool `json:"active,omitempty"` // authType AuthType AiNotificationsAuthType `json:"authType,omitempty"` // id @@ -763,11 +763,11 @@ type AiNotificationsDestinationSorter struct { // AiNotificationsDestinationUpdate - Destination update object type AiNotificationsDestinationUpdate struct { // active - Active bool `json:"active,omitempty"` + Active *bool `json:"active,omitempty"` // auth Auth *AiNotificationsCredentialsInput `json:"auth,omitempty"` // disableAuth - DisableAuth bool `json:"disableAuth,omitempty"` + DisableAuth *bool `json:"disableAuth,omitempty"` // name Name string `json:"name,omitempty"` // properties diff --git a/pkg/notifications/types_unit_test.go b/pkg/notifications/types_unit_test.go new file mode 100644 index 000000000..3b494b881 --- /dev/null +++ b/pkg/notifications/types_unit_test.go @@ -0,0 +1,136 @@ +//go:build unit +// +build unit + +package notifications + +import ( +"encoding/json" +"testing" + +"github.com/stretchr/testify/assert" +) + + +// Verify that boolean values with "empty" values (== false) are not ignored by json marshaller +// If `false` is not included into input, it would be impossible to change any boolean flag to `false` in an update +func TestAiNotificationsChannelFilter_OptionalBooleans_JsonFormat(t *testing.T) { + t.Parallel() + var falseValue = false + var input = AiNotificationsChannelFilter{ + Name: "test-notification-channel-1-update", + Active: &falseValue, + } + var serialized, err = json.Marshal(input) + + assert.NoError(t, err) + assert.Equal( + t, + "{\"active\":false,\"name\":\"test-notification-channel-1-update\",\"property\":{\"key\":\"\",\"value\":\"\"}}", + string(serialized), + ) +} + +// Verify that an empty update input is serialized into an empty json +func TestAiNotificationsChannelFilter_EmptyInput_JsonFormat(t *testing.T) { + t.Parallel() + var input = AiNotificationsChannelFilter{ + } + + var serialized, err = json.Marshal(input) + + assert.NoError(t, err) + assert.Equal(t, "{\"property\":{\"key\":\"\",\"value\":\"\"}}", string(serialized)) +} + +// Verify that boolean values with "empty" values (== false) are not ignored by json marshaller +// If `false` is not included into input, it would be impossible to change any boolean flag to `false` in an update +func TestAiNotificationsChannelUpdate_OptionalBooleans_JsonFormat(t *testing.T) { + t.Parallel() + var falseValue = false + var input = AiNotificationsChannelUpdate{ + Name: "test-notification-channel-1-update", + Properties: []AiNotificationsPropertyInput{}, + Active: &falseValue, + } + var serialized, err = json.Marshal(input) + + assert.NoError(t, err) + assert.Equal( + t, + "{\"active\":false,\"name\":\"test-notification-channel-1-update\"}", + string(serialized), + ) +} + +// Verify that an empty update input is serialized into an empty json +func TestAiNotificationsChannelUpdate_EmptyInput_JsonFormat(t *testing.T) { + t.Parallel() + var input = AiNotificationsChannelUpdate{} + + var serialized, err = json.Marshal(input) + + assert.NoError(t, err) + assert.Equal(t, "{}", string(serialized)) +} + +// Verify that boolean values with "empty" values (== false) are not ignored by json marshaller +// If `false` is not included into input, it would be impossible to change any boolean flag to `false` in an update +func TestAiNotificationsDestinationFilter_OptionalBooleans_JsonFormat(t *testing.T) { + t.Parallel() + var falseValue = false + var input = AiNotificationsDestinationFilter{ + Name: "test-notification-channel-1-update", + Active: &falseValue, + } + var serialized, err = json.Marshal(input) + + assert.NoError(t, err) + assert.Equal( + t, + "{\"active\":false,\"name\":\"test-notification-channel-1-update\",\"property\":{\"key\":\"\",\"value\":\"\"}}", + string(serialized), + ) +} + +// Verify that an empty update input is serialized into an empty json +func TestAiNotificationsDestinationFilter_EmptyInput_JsonFormat(t *testing.T) { + t.Parallel() + var input = AiNotificationsDestinationFilter{} + + var serialized, err = json.Marshal(input) + + assert.NoError(t, err) + assert.Equal(t, "{\"property\":{\"key\":\"\",\"value\":\"\"}}", string(serialized)) +} + +// Verify that boolean values with "empty" values (== false) are not ignored by json marshaller +// If `false` is not included into input, it would be impossible to change any boolean flag to `false` in an update +func TestAiNotificationsDestinationUpdate_OptionalBooleans_JsonFormat(t *testing.T) { + t.Parallel() + var falseValue = false + var input = AiNotificationsDestinationUpdate{ + Name: "test-notification-channel-1-update", + Properties: []AiNotificationsPropertyInput{}, + Active: &falseValue, + DisableAuth: &falseValue, + } + var serialized, err = json.Marshal(input) + + assert.NoError(t, err) + assert.Equal( + t, + "{\"active\":false,\"disableAuth\":false,\"name\":\"test-notification-channel-1-update\"}", + string(serialized), + ) +} + +// Verify that an empty update input is serialized into an empty json +func TestAiNotificationsDestinationUpdate_EmptyInput_JsonFormat(t *testing.T) { + t.Parallel() + var input = AiNotificationsDestinationUpdate{} + + var serialized, err = json.Marshal(input) + + assert.NoError(t, err) + assert.Equal(t, "{}", string(serialized)) +}