Skip to content

Commit

Permalink
Allow API endpoints to determine default boolean option values
Browse files Browse the repository at this point in the history
On Jira API endpoints that accept boolean parameters, the value of the
parameter may default to either true or false if it isn't supplied in
the request, depending on the endpoint. In options structs that have
them, change the type of fields representing these parameters from
`bool` to `*bool`, so that their value isn't explicitly set to false in
the request if a value isn't given in the struct - in order words, let
the API endpoint decide what the value should be, not go-jira.
  • Loading branch information
chrisnovakovic committed Feb 23, 2024
1 parent c91ee0d commit 7ffee35
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Filter struct {

// GetMyFiltersQueryOptions specifies the optional parameters for the Get My Filters method
type GetMyFiltersQueryOptions struct {
IncludeFavourites bool `url:"includeFavourites,omitempty"`
IncludeFavourites *bool `url:"includeFavourites,omitempty"`
Expand string `url:"expand,omitempty"`
}

Expand Down
14 changes: 7 additions & 7 deletions issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ type IssueService struct {

// UpdateQueryOptions specifies the optional parameters to the Edit issue
type UpdateQueryOptions struct {
NotifyUsers bool `url:"notifyUsers,omitempty"`
OverrideScreenSecurity bool `url:"overrideScreenSecurity,omitempty"`
OverrideEditableFlag bool `url:"overrideEditableFlag,omitempty"`
NotifyUsers *bool `url:"notifyUsers,omitempty"`
OverrideScreenSecurity *bool `url:"overrideScreenSecurity,omitempty"`
OverrideEditableFlag *bool `url:"overrideEditableFlag,omitempty"`
}

// Issue represents a Jira issue.
Expand Down Expand Up @@ -544,8 +544,8 @@ type GetQueryOptions struct {
// Properties is the list of properties to return for the issue. By default no properties are returned.
Properties string `url:"properties,omitempty"`
// FieldsByKeys if true then fields in issues will be referenced by keys instead of ids
FieldsByKeys bool `url:"fieldsByKeys,omitempty"`
UpdateHistory bool `url:"updateHistory,omitempty"`
FieldsByKeys *bool `url:"fieldsByKeys,omitempty"`
UpdateHistory *bool `url:"updateHistory,omitempty"`
ProjectKeys string `url:"projectKeys,omitempty"`
}

Expand All @@ -558,12 +558,12 @@ type GetWorklogsQueryOptions struct {
}

type AddWorklogQueryOptions struct {
NotifyUsers bool `url:"notifyUsers,omitempty"`
NotifyUsers *bool `url:"notifyUsers,omitempty"`
AdjustEstimate string `url:"adjustEstimate,omitempty"`
NewEstimate string `url:"newEstimate,omitempty"`
ReduceBy string `url:"reduceBy,omitempty"`
Expand string `url:"expand,omitempty"`
OverrideEditableFlag bool `url:"overrideEditableFlag,omitempty"`
OverrideEditableFlag *bool `url:"overrideEditableFlag,omitempty"`
}

// CustomFields represents custom fields of Jira
Expand Down
45 changes: 45 additions & 0 deletions jira_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,51 @@ func TestClient_NewRequest_EmptyBody(t *testing.T) {
}
}

// Tests that addOptions correctly serialises *bools, which are used to convey the desired value of
// options that default to true on the API endpoint if a value is not specified in the request:
//
// | Option value | Expected outcome |
// | ------------ | ------------------------------ |
// | *true | opt=true in request |
// | *false | opt=false in request |
// | nil | Determined by the API endpoint |
func Test_addOptions_Bool_Pointer(t *testing.T) {
apiEndpoint := "rest/api/2/issue/123"

for _, test := range []struct {
Description string
Opts *UpdateQueryOptions
Expected string
}{
{
Description: "*bool implicitly nil",
Opts: &UpdateQueryOptions{},
Expected: "",
},
{
Description: "*bool explicitly nil",
Opts: &UpdateQueryOptions{NotifyUsers: nil},
Expected: "",
},
{
Description: "*bool explicitly true",
Opts: &UpdateQueryOptions{NotifyUsers: Bool(true)},
Expected: "?notifyUsers=true",
},
{
Description: "*bool explicitly false",
Opts: &UpdateQueryOptions{NotifyUsers: Bool(false)},
Expected: "?notifyUsers=false",
},
} {
t.Run(test.Description, func(t *testing.T) {
actual, err := addOptions(apiEndpoint, test.Opts)
assert.NoError(t, err)
assert.Equal(t, apiEndpoint + test.Expected, actual)
})
}
}

func TestClient_NewMultiPartRequest(t *testing.T) {
c, err := NewClient(nil, testJiraInstanceURL)
if err != nil {
Expand Down

0 comments on commit 7ffee35

Please sign in to comment.