Repository: xanzy/go-gitlab (Go)

Issue:
New Project Value causes issue when null
In #1661 , a bunch of new properties were added to the `Project` object, which is great! Unfortunately, one of the tags (`only_allow_merge_if_all_status_checks_passed`) is missing the `omitempty` json tag, which causes an API error if the value is set to `nil`, which is allowed due to it accepting a pointer.



Discussion:

Patch:
diff --git a/projects.go b/projects.go
index 011434309..e85b19224 100644
--- a/projects.go
+++ b/projects.go
@@ -633,7 +633,7 @@ func (s *ProjectsService) GetProjectEvents(pid interface{}, opt *GetProjectEvent
 // GitLab API docs: https://docs.gitlab.com/ee/api/projects.html#create-project
 type CreateProjectOptions struct {
 	AllowMergeOnSkippedPipeline               *bool                                `url:"allow_merge_on_skipped_pipeline,omitempty" json:"allow_merge_on_skipped_pipeline,omitempty"`
-	OnlyAllowMergeIfAllStatusChecksPassed     *bool                                `url:"only_allow_merge_if_all_status_checks_passed" json:"only_allow_merge_if_all_status_checks_passed"`
+	OnlyAllowMergeIfAllStatusChecksPassed     *bool                                `url:"only_allow_merge_if_all_status_checks_passed,omitempty" json:"only_allow_merge_if_all_status_checks_passed,omitempty"`
 	AnalyticsAccessLevel                      *AccessControlValue                  `url:"analytics_access_level,omitempty" json:"analytics_access_level,omitempty"`
 	ApprovalsBeforeMerge                      *int                                 `url:"approvals_before_merge,omitempty" json:"approvals_before_merge,omitempty"`
 	AutoCancelPendingPipelines                *string                              `url:"auto_cancel_pending_pipelines,omitempty" json:"auto_cancel_pending_pipelines,omitempty"`
@@ -853,7 +853,7 @@ func (s *ProjectsService) CreateProjectForUser(user int, opt *CreateProjectForUs
 // GitLab API docs: https://docs.gitlab.com/ee/api/projects.html#edit-project
 type EditProjectOptions struct {
 	AllowMergeOnSkippedPipeline               *bool                                `url:"allow_merge_on_skipped_pipeline,omitempty" json:"allow_merge_on_skipped_pipeline,omitempty"`
-	OnlyAllowMergeIfAllStatusChecksPassed     *bool                                `url:"only_allow_merge_if_all_status_checks_passed" json:"only_allow_merge_if_all_status_checks_passed"`
+	OnlyAllowMergeIfAllStatusChecksPassed     *bool                                `url:"only_allow_merge_if_all_status_checks_passed,omitempty" json:"only_allow_merge_if_all_status_checks_passed,omitempty"`
 	AnalyticsAccessLevel                      *AccessControlValue                  `url:"analytics_access_level,omitempty" json:"analytics_access_level,omitempty"`
 	ApprovalsBeforeMerge                      *int                                 `url:"approvals_before_merge,omitempty" json:"approvals_before_merge,omitempty"`
 	AutoCancelPendingPipelines                *string                              `url:"auto_cancel_pending_pipelines,omitempty" json:"auto_cancel_pending_pipelines,omitempty"`


Test patch:
diff --git a/projects_test.go b/projects_test.go
index d3a1b9780..7500357a4 100644
--- a/projects_test.go
+++ b/projects_test.go
@@ -18,6 +18,7 @@ package gitlab
 
 import (
 	"bytes"
+	"encoding/json"
 	"fmt"
 	"net/http"
 	"os"
@@ -25,6 +26,8 @@ import (
 	"strings"
 	"testing"
 	"time"
+
+	"github.com/stretchr/testify/assert"
 )
 
 func TestListProjects(t *testing.T) {
@@ -1361,3 +1364,23 @@ func TestCreateProjectApprovalRuleEligibleApprovers(t *testing.T) {
 		t.Errorf("Projects.CreateProjectApprovalRule returned %+v, want %+v", rule, want)
 	}
 }
+
+func TestProjectModelsOptionalMergeAttribute(t *testing.T) {
+	// Create a `CreateProjectOptions` struct, ensure that merge attribute doesn't serialize
+	jsonString, err := json.Marshal(&CreateProjectOptions{
+		Name: String("testProject"),
+	})
+	if err != nil {
+		t.Fatal("Failed to marshal object", err)
+	}
+	assert.False(t, strings.Contains(string(jsonString), "only_allow_merge_if_all_status_checks_passed"))
+
+	// Test the same thing but for `EditProjectOptions` struct
+	jsonString, err = json.Marshal(&EditProjectOptions{
+		Name: String("testProject"),
+	})
+	if err != nil {
+		t.Fatal("Failed to marshal object", err)
+	}
+	assert.False(t, strings.Contains(string(jsonString), "only_allow_merge_if_all_status_checks_passed"))
+}