Repository: xanzy/go-gitlab
Pull request #919 (2020-08-18 07:41:50)

Patch boolean json unmarshall error on slack service
This PR addresses these issues:
- GitLab Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/233941
- Terraform Issue: https://github.com/gitlabhq/terraform-provider-gitlab/issues/348
- Move `TestBoolValue` func to `types_test.go` file and add test two new cases.


Patch:
diff --git a/types.go b/types.go
index a78b80f..bebb980 100644
--- a/types.go
+++ b/types.go
@@ -402,8 +402,11 @@ func Time(v time.Time) *time.Time {
 // BoolValue is a boolean value with advanced json unmarshaling features.
 type BoolValue bool
 
-// UnmarshalJSON allows 1 and 0 to be considered as boolean values
-// Needed for https://gitlab.com/gitlab-org/gitlab-ce/issues/50122
+// UnmarshalJSON allows 1, 0, "true", and "false" to be considered as boolean values
+// Needed for:
+// https://gitlab.com/gitlab-org/gitlab-ce/issues/50122
+// https://gitlab.com/gitlab-org/gitlab/-/issues/233941
+// https://github.com/gitlabhq/terraform-provider-gitlab/issues/348
 func (t *BoolValue) UnmarshalJSON(b []byte) error {
 	switch string(b) {
 	case `"1"`:
@@ -412,6 +415,12 @@ func (t *BoolValue) UnmarshalJSON(b []byte) error {
 	case `"0"`:
 		*t = false
 		return nil
+	case `"true"`:
+		*t = true
+		return nil
+	case `"false"`:
+		*t = false
+		return nil
 	default:
 		var v bool
 		err := json.Unmarshal(b, &v)


Test patch:
diff --git a/gitlab_test.go b/gitlab_test.go
index c522d56..ee96093 100644
--- a/gitlab_test.go
+++ b/gitlab_test.go
@@ -3,7 +3,6 @@ package gitlab
 import (
 	"bytes"
 	"context"
-	"encoding/json"
 	"errors"
 	"io"
 	"io/ioutil"
@@ -167,43 +166,6 @@ func TestRequestWithContext(t *testing.T) {
 	}
 }
 
-func TestBoolValue(t *testing.T) {
-	testCases := map[string]struct {
-		data     []byte
-		expected bool
-	}{
-		"should unmarshal true as true": {
-			data:     []byte("true"),
-			expected: true,
-		},
-		"should unmarshal false as true": {
-			data:     []byte("false"),
-			expected: false,
-		},
-		"should unmarshal \"1\" as true": {
-			data:     []byte(`"1"`),
-			expected: true,
-		},
-		"should unmarshal \"0\" as false": {
-			data:     []byte(`"0"`),
-			expected: false,
-		},
-	}
-
-	for name, testCase := range testCases {
-		t.Run(name, func(t *testing.T) {
-			var b BoolValue
-			if err := json.Unmarshal(testCase.data, &b); err != nil {
-				t.Fatalf("Unexpected error: %v", err)
-			}
-
-			if bool(b) != testCase.expected {
-				t.Fatalf("Expected %v but got %v", testCase.expected, b)
-			}
-		})
-	}
-}
-
 func loadFixture(filePath string) []byte {
 	content, err := ioutil.ReadFile(filePath)
 	if err != nil {
diff --git a/types_test.go b/types_test.go
new file mode 100644
index 0000000..6e14e7c
--- /dev/null
+++ b/types_test.go
@@ -0,0 +1,58 @@
+package gitlab
+
+import (
+	"encoding/json"
+	"testing"
+)
+
+func TestBoolValue(t *testing.T) {
+	testCases := []struct {
+		name     string
+		data     []byte
+		expected bool
+	}{
+		{
+			name:     "should unmarshal true as true",
+			data:     []byte("true"),
+			expected: true,
+		},
+		{
+			name:     "should unmarshal false as false",
+			data:     []byte("false"),
+			expected: false,
+		},
+		{
+			name:     "should unmarshal true as true",
+			data:     []byte(`"true"`),
+			expected: true,
+		},
+		{
+			name:     "should unmarshal false as false",
+			data:     []byte(`"false"`),
+			expected: false,
+		},
+		{
+			name:     "should unmarshal \"1\" as true",
+			data:     []byte(`"1"`),
+			expected: true,
+		},
+		{
+			name:     "should unmarshal \"0\" as false",
+			data:     []byte(`"0"`),
+			expected: false,
+		},
+	}
+
+	for _, testCase := range testCases {
+		t.Run(testCase.name, func(t *testing.T) {
+			var b BoolValue
+			if err := json.Unmarshal(testCase.data, &b); err != nil {
+				t.Fatalf("Unexpected error: %v", err)
+			}
+
+			if bool(b) != testCase.expected {
+				t.Fatalf("Expected %v but got %v", testCase.expected, b)
+			}
+		})
+	}
+}