mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-22 19:13:57 +09:00
Backport https://github.com/go-gitea/gitea/pull/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. Only the milestone fix is backported. The issue and pull request deadline changes of https://github.com/go-gitea/gitea/pull/39869 build on https://github.com/go-gitea/gitea/pull/38677, which isn't on v28.
231 lines
6.1 KiB
Go
231 lines
6.1 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package optional_test
|
|
|
|
import (
|
|
std_json "encoding/json" //nolint:depguard // for testing purpose
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.dev/modules/json"
|
|
"gitea.dev/modules/optional"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"go.yaml.in/yaml/v4"
|
|
)
|
|
|
|
type testSerializationStruct struct {
|
|
NormalString string `json:"normal_string" yaml:"normal_string"`
|
|
NormalBool bool `json:"normal_bool" yaml:"normal_bool"`
|
|
OptBool optional.Option[bool] `json:"optional_bool,omitempty" yaml:"optional_bool,omitempty"`
|
|
|
|
// It causes an undefined behavior: should the "omitempty" tag only omit "null", or also the empty string?
|
|
// The behavior is inconsistent between json and v2 packages, and there is no such use case in Gitea.
|
|
// If anyone really needs it, they can use json.MarshalKeepOptionalEmpty to revert the v1 behavior
|
|
OptString optional.Option[string] `json:"optional_string,omitempty" yaml:"optional_string,omitempty"`
|
|
|
|
OptTwoBool optional.Option[bool] `json:"optional_two_bool" yaml:"optional_two_bool"`
|
|
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
|
|
obj *testSerializationStruct
|
|
want string
|
|
}{
|
|
{
|
|
name: "empty",
|
|
obj: new(testSerializationStruct),
|
|
want: `{"normal_string":"","normal_bool":false,"optional_two_bool":null,"optional_two_string":null}`,
|
|
},
|
|
{
|
|
name: "some",
|
|
obj: &testSerializationStruct{
|
|
NormalString: "a string",
|
|
NormalBool: true,
|
|
OptBool: optional.Some(false),
|
|
OptString: optional.Some(""),
|
|
OptTwoBool: optional.None[bool](),
|
|
OptTwoString: optional.None[string](),
|
|
},
|
|
want: `{"normal_string":"a string","normal_bool":true,"optional_bool":false,"optional_string":"","optional_two_bool":null,"optional_two_string":null}`,
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
b, err := json.MarshalKeepOptionalEmpty(tc.obj)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tc.want, string(b), "gitea json module returned unexpected")
|
|
|
|
b, err = std_json.Marshal(tc.obj)
|
|
assert.NoError(t, err)
|
|
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) {
|
|
tests := []struct {
|
|
name string
|
|
data string
|
|
want testSerializationStruct
|
|
}{
|
|
{
|
|
name: "empty",
|
|
data: `{}`,
|
|
want: testSerializationStruct{
|
|
NormalString: "",
|
|
},
|
|
},
|
|
{
|
|
name: "some",
|
|
data: `{"normal_string":"a string","normal_bool":true,"optional_bool":false,"optional_string":"","optional_two_bool":null,"optional_two_string":null}`,
|
|
want: testSerializationStruct{
|
|
NormalString: "a string",
|
|
NormalBool: true,
|
|
OptBool: optional.Some(false),
|
|
OptString: optional.Some(""),
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var obj1 testSerializationStruct
|
|
err := json.Unmarshal([]byte(tc.data), &obj1)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tc.want, obj1, "gitea json module returned unexpected")
|
|
|
|
var obj2 testSerializationStruct
|
|
err = std_json.Unmarshal([]byte(tc.data), &obj2)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tc.want, obj2, "std json module returned unexpected")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOptionalToYaml(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
obj *testSerializationStruct
|
|
want string
|
|
}{
|
|
{
|
|
name: "empty",
|
|
obj: new(testSerializationStruct),
|
|
want: `normal_string: ""
|
|
normal_bool: false
|
|
optional_two_bool: null
|
|
optional_two_string: null
|
|
`,
|
|
},
|
|
{
|
|
name: "some",
|
|
obj: &testSerializationStruct{
|
|
NormalString: "a string",
|
|
NormalBool: true,
|
|
OptBool: optional.Some(false),
|
|
OptString: optional.Some(""),
|
|
},
|
|
want: `normal_string: a string
|
|
normal_bool: true
|
|
optional_bool: false
|
|
optional_string: ""
|
|
optional_two_bool: null
|
|
optional_two_string: null
|
|
`,
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
b, err := yaml.Marshal(tc.obj)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tc.want, string(b), "yaml module returned unexpected")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOptionalFromYaml(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
data string
|
|
want testSerializationStruct
|
|
}{
|
|
{
|
|
name: "empty",
|
|
data: ``,
|
|
want: testSerializationStruct{},
|
|
},
|
|
{
|
|
name: "empty but init",
|
|
data: `normal_string: ""
|
|
normal_bool: false
|
|
optional_bool:
|
|
optional_two_bool:
|
|
optional_two_string:
|
|
`,
|
|
want: testSerializationStruct{},
|
|
},
|
|
{
|
|
name: "some",
|
|
data: `
|
|
normal_string: a string
|
|
normal_bool: true
|
|
optional_bool: false
|
|
optional_string: ""
|
|
optional_two_bool: null
|
|
optional_two_string: null
|
|
`,
|
|
want: testSerializationStruct{
|
|
NormalString: "a string",
|
|
NormalBool: true,
|
|
OptBool: optional.Some(false),
|
|
OptString: optional.Some(""),
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var obj testSerializationStruct
|
|
err := yaml.Unmarshal([]byte(tc.data), &obj)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tc.want, obj, "yaml module returned unexpected")
|
|
})
|
|
}
|
|
}
|