fix(api): keep milestone due date when it is omitted in edit request (#39869)

`PATCH /repos/{owner}/{repo}/milestones/{id}` always overwrote the due
date with `due_on`, so a request that only changed other fields, such as
`{"state": "closed"}`, silently removed it.

The due date is now kept when `due_on` is omitted, and `"due_on": null`
or `"due_on": ""` remove it.

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Shudhanshu Singhandwxiaoguang authored and GitHub committed 2026-10-21 18:52:33 +08:00
1 parent b5e53210c2
commit a8e1057f5e
15 files changed
+192 -74

No files matched your search

+13 -2
View File
@@ -4,17 +4,28 @@
package optional
import (
"reflect"
"gitea.dev/modules/json"
"go.yaml.in/yaml/v4"
)
func (o *Option[T]) assignUnmarshalledValue(v *T) {
if reflect.TypeFor[T]().Kind() == reflect.Pointer && v == nil {
var zero T
*o = Some[T](zero)
} else {
*o = FromPtr(v)
}
}
func (o *Option[T]) UnmarshalJSON(data []byte) error {
var v *T
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*o = FromPtr(v)
o.assignUnmarshalledValue(v)
return nil
}
@@ -31,7 +42,7 @@ func (o *Option[T]) UnmarshalYAML(value *yaml.Node) error {
if err := value.Decode(&v); err != nil {
return err
}
*o = FromPtr(v)
o.assignUnmarshalledValue(v)
return nil
}
+35
View File
@@ -6,6 +6,7 @@ package optional_test
import (
std_json "encoding/json" //nolint:depguard // for testing purpose
"testing"
"time"
"gitea.dev/modules/json"
"gitea.dev/modules/optional"
@@ -28,6 +29,14 @@ type testSerializationStruct struct {
OptTwoString optional.Option[string] `json:"optional_two_string" yaml:"optional_two_string"`
}
type testSerializationNullValue struct {
OptString optional.Option[string] `json:"opt_null,omitempty" yaml:"opt_null,omitempty"`
}
type testSerializationNullPointer struct {
OptNull optional.Option[*time.Time] `json:"opt_null,omitempty" yaml:"opt_null,omitempty"`
}
func TestOptionalToJson(t *testing.T) {
tests := []struct {
name string
@@ -63,6 +72,32 @@ func TestOptionalToJson(t *testing.T) {
assert.Equal(t, tc.want, string(b), "std json module returned unexpected")
})
}
t.Run("NullValue", func(t *testing.T) {
var v1 testSerializationNullValue
err := json.Unmarshal([]byte(`{"opt_null":null}`), &v1)
assert.NoError(t, err)
assert.False(t, v1.OptString.Has())
})
t.Run("NullPointer", func(t *testing.T) {
var v1 testSerializationNullPointer
err := json.Unmarshal([]byte(`{"opt_null":null}`), &v1)
assert.NoError(t, err)
assert.True(t, v1.OptNull.Has())
assert.Nil(t, v1.OptNull.Value())
var v2 testSerializationNullPointer
err = std_json.Unmarshal([]byte(`{}`), &v2)
assert.NoError(t, err)
assert.False(t, v2.OptNull.Has())
var v3 testSerializationNullPointer
err = std_json.Unmarshal([]byte(`{"opt_null":"2006-01-02T15:04:05Z"}`), &v3)
assert.NoError(t, err)
assert.True(t, v3.OptNull.Has())
assert.NotNil(t, v3.OptNull.Value())
})
}
func TestOptionalFromJson(t *testing.T) {
+10 -6
View File
@@ -10,6 +10,8 @@ import (
"strings"
"time"
"gitea.dev/modules/optional"
"go.yaml.in/yaml/v4"
)
@@ -96,7 +98,7 @@ type CreateIssueOption struct {
Assignee string `json:"assignee"`
Assignees []string `json:"assignees"`
// swagger:strfmt date-time
Deadline *time.Time `json:"due_date"`
Deadline optional.Option[*string] `json:"due_date,omitzero"`
// milestone id
Milestone int64 `json:"milestone"`
// list of label ids
@@ -118,9 +120,10 @@ type EditIssueOption struct {
// list of project ids to set (replaces existing projects)
Projects *[]int64 `json:"projects"`
State *string `json:"state"`
// Deadline is the updated due date for the issue, use null or empty string to remove the deadline
// swagger:strfmt date-time
Deadline *time.Time `json:"due_date"`
RemoveDeadline *bool `json:"unset_due_date"`
Deadline optional.Option[*string] `json:"due_date,omitzero"`
RemoveDeadline *bool `json:"unset_due_date"`
// The current version of the issue content to detect conflicts during editing
ContentVersion *int `json:"content_version"`
}
@@ -130,10 +133,11 @@ type IssueAssigneesOption struct {
Assignees []string `json:"assignees"`
}
// EditDeadlineOption options for creating a deadline
// EditDeadlineOption options for editing a deadline
type EditDeadlineOption struct {
// required:true
Deadline *time.Time `json:"due_date"`
// Deadline is the updated due date for the issue or pull request, use null or empty string to remove the deadline
// swagger:strfmt date-time
Deadline optional.Option[*string] `json:"due_date,omitzero"`
}
// IssueDeadline represents an issue deadline
+7 -4
View File
@@ -5,9 +5,11 @@ package structs
import (
"time"
"gitea.dev/modules/optional"
)
// Milestone milestone is a collection of issues on one repository
// Milestone is a collection of issues on one repository
type Milestone struct {
// ID is the unique identifier for the milestone
ID int64 `json:"id"`
@@ -39,7 +41,7 @@ type CreateMilestoneOption struct {
Description string `json:"description"`
// swagger:strfmt date-time
// Deadline is the due date for the milestone
Deadline *time.Time `json:"due_on"`
Deadline optional.Option[*string] `json:"due_on,omitzero"`
// enum: ["open","closed"]
// State indicates the initial state of the milestone
State string `json:"state"`
@@ -54,6 +56,7 @@ type EditMilestoneOption struct {
// State indicates the updated state of the milestone
// enum: ["open","closed"]
State *string `json:"state"`
// Deadline is the updated due date for the milestone
Deadline *time.Time `json:"due_on"`
// Deadline is the due date for the milestone, use null or empty string to remove the deadline
// swagger:strfmt date-time
Deadline optional.Option[*string] `json:"due_on,omitzero"`
}
+5 -2
View File
@@ -5,6 +5,8 @@ package structs
import (
"time"
"gitea.dev/modules/optional"
)
// PullRequest represents a pull request
@@ -137,7 +139,7 @@ type CreatePullRequestOption struct {
// The list of label IDs to assign to the pull request
Labels []int64 `json:"labels"`
// swagger:strfmt date-time
Deadline *time.Time `json:"due_date"`
Deadline optional.Option[*string] `json:"due_date,omitzero"`
// The list of reviewer usernames
Reviewers []string `json:"reviewers"`
// The list of team reviewer names
@@ -164,8 +166,9 @@ type EditPullRequestOption struct {
Labels []int64 `json:"labels"`
// The new state for the pull request
State *string `json:"state"`
// Deadline is the updated due date for the pull request, use null or empty string to remove the deadline
// swagger:strfmt date-time
Deadline *time.Time `json:"due_date"`
Deadline optional.Option[*string] `json:"due_date,omitzero"`
// Whether to remove the current deadline
RemoveDeadline *bool `json:"unset_due_date"`
// Whether to allow maintainer edits