Skip to content

Commit 669b86e

Browse files
fix: Stop test/fields from reporting null-valued fields as missing
The test/fields utility decoded the API response into a struct and then re-marshaled the struct to build the set of known JSON keys. Because every go-github field uses ",omitempty", a field whose API value is null decodes to a nil pointer and disappears on re-marshal, so mapped fields such as Issue.Milestone were incorrectly reported as missing. Derive the expected top-level JSON keys directly from the struct type via reflection instead, which is value-independent and never drops ",omitempty" fields. Extract the comparison into a missingFields function and cover it with a network-free unit test, including a regression test for the null-milestone case and a check that genuinely unmapped keys are still reported. Fixes google#576.
1 parent 728cdb1 commit 669b86e

2 files changed

Lines changed: 173 additions & 36 deletions

File tree

‎test/fields/fields.go‎

Lines changed: 99 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"log"
2525
"os"
2626
"reflect"
27+
"slices"
2728
"strings"
2829

2930
"github.com/google/go-github/v88/github"
@@ -77,72 +78,134 @@ func main() {
7778
// struct fields of typ.
7879
func testType(urlStr string, typ any) error {
7980
ctx := context.Background()
80-
slice := reflect.Indirect(reflect.ValueOf(typ)).Kind() == reflect.Slice
8181

8282
req, err := client.NewRequest(ctx, "GET", urlStr, nil)
8383
if err != nil {
8484
return err
8585
}
8686

87-
// start with a json.RawMessage so we can decode multiple ways below
8887
raw := new(json.RawMessage)
8988
_, err = client.Do(req, raw)
9089
if err != nil {
9190
return err
9291
}
9392

94-
// unmarshal directly to a map
93+
missing, err := missingFields(*raw, typ, *skipURLs)
94+
if err != nil {
95+
return err
96+
}
97+
for _, m := range missing {
98+
fmt.Printf("%v missing field for key: %v (example value: %v)\n", reflect.TypeOf(typ), m.key, m.value)
99+
}
100+
101+
return nil
102+
}
103+
104+
// missingField is a JSON key returned by the API that has no corresponding
105+
// struct field in the relevant go-github type.
106+
type missingField struct {
107+
key string
108+
value any
109+
}
110+
111+
// missingFields returns, sorted by key, the JSON keys present in the API
112+
// response raw that have no corresponding field in typ. typ may be a pointer to
113+
// a struct or a pointer to a slice of structs (the element type is used, and
114+
// the first element of the response array is inspected).
115+
//
116+
// The expected set of struct keys is derived from the type via reflection
117+
// rather than by re-marshaling a decoded value. Re-marshaling drops keys for
118+
// any field whose value is the zero value because go-github fields use
119+
// ",omitempty"; a field that the API returns as null therefore decodes to a nil
120+
// pointer and disappears on re-marshal, which previously caused those fields to
121+
// be reported as missing even though they are mapped (see issue #576).
122+
func missingFields(raw json.RawMessage, typ any, skipURLs bool) ([]*missingField, error) {
123+
v := reflect.Indirect(reflect.ValueOf(typ))
124+
125+
// Decode the raw response into a map of its top-level keys. For a slice
126+
// response, inspect the first element.
95127
var m1 map[string]any
96-
if slice {
128+
if v.Kind() == reflect.Slice {
97129
var s []map[string]any
98-
err = json.Unmarshal(*raw, &s)
99-
if err != nil {
100-
return err
130+
if err := json.Unmarshal(raw, &s); err != nil {
131+
return nil, err
132+
}
133+
if len(s) > 0 {
134+
m1 = s[0]
101135
}
102-
m1 = s[0]
103136
} else {
104-
err = json.Unmarshal(*raw, &m1)
105-
if err != nil {
106-
return err
137+
if err := json.Unmarshal(raw, &m1); err != nil {
138+
return nil, err
107139
}
108140
}
109141

110-
// unmarshal to typ first, then re-marshal and unmarshal to a map
111-
err = json.Unmarshal(*raw, typ)
112-
if err != nil {
113-
return err
142+
t := v.Type()
143+
if t.Kind() == reflect.Slice {
144+
t = t.Elem()
114145
}
146+
want := jsonFieldNames(t)
115147

116-
var byt []byte
117-
if slice {
118-
// use first item in slice
119-
v := reflect.Indirect(reflect.ValueOf(typ))
120-
byt, err = json.Marshal(v.Index(0).Interface())
121-
if err != nil {
122-
return err
148+
var missing []*missingField
149+
for k, val := range m1 {
150+
if skipURLs && strings.HasSuffix(k, "_url") {
151+
continue
123152
}
124-
} else {
125-
byt, err = json.Marshal(typ)
126-
if err != nil {
127-
return err
153+
if _, ok := want[k]; !ok {
154+
missing = append(missing, &missingField{key: k, value: val})
128155
}
129156
}
157+
slices.SortFunc(missing, func(a, b *missingField) int {
158+
return strings.Compare(a.key, b.key)
159+
})
160+
return missing, nil
161+
}
130162

131-
var m2 map[string]any
132-
err = json.Unmarshal(byt, &m2)
133-
if err != nil {
134-
return err
163+
// jsonFieldNames returns the set of top-level JSON object keys that a value of
164+
// type t marshals to, following encoding/json's rules: it skips unexported
165+
// fields and fields tagged `json:"-"`, uses the tag name (the part before the
166+
// first comma) when present and otherwise the field name, and promotes the
167+
// fields of anonymous (embedded) structs. Unlike re-marshaling a value, this is
168+
// value-independent, so a ",omitempty" field is never dropped.
169+
func jsonFieldNames(t reflect.Type) map[string]struct{} {
170+
for t.Kind() == reflect.Pointer {
171+
t = t.Elem()
135172
}
136173

137-
// now compare the two maps
138-
for k, v := range m1 {
139-
if *skipURLs && strings.HasSuffix(k, "_url") {
174+
names := map[string]struct{}{}
175+
if t.Kind() != reflect.Struct {
176+
return names
177+
}
178+
179+
for i := 0; i < t.NumField(); i++ {
180+
f := t.Field(i)
181+
name, _, _ := strings.Cut(f.Tag.Get("json"), ",")
182+
if name == "-" {
140183
continue
141184
}
142-
if _, ok := m2[k]; !ok {
143-
fmt.Printf("%v missing field for key: %v (example value: %v)\n", reflect.TypeOf(typ), k, v)
185+
186+
// An embedded struct without an explicit JSON name has its fields
187+
// promoted to the parent's top-level keys.
188+
if f.Anonymous && name == "" {
189+
ft := f.Type
190+
for ft.Kind() == reflect.Pointer {
191+
ft = ft.Elem()
192+
}
193+
if ft.Kind() == reflect.Struct {
194+
for k := range jsonFieldNames(ft) {
195+
names[k] = struct{}{}
196+
}
197+
continue
198+
}
144199
}
200+
201+
if !f.IsExported() {
202+
continue
203+
}
204+
if name == "" {
205+
name = f.Name
206+
}
207+
names[name] = struct{}{}
145208
}
146209

147-
return nil
210+
return names
148211
}

‎test/fields/fields_test.go‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2026 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package main
7+
8+
import (
9+
"encoding/json"
10+
"slices"
11+
"testing"
12+
13+
"github.com/google/go-github/v88/github"
14+
)
15+
16+
func TestMissingFields(t *testing.T) {
17+
t.Parallel()
18+
19+
tests := []struct {
20+
name string
21+
raw string
22+
typ any
23+
skipURLs bool
24+
want []string
25+
}{
26+
{
27+
// Regression test for #576: a mapped field whose API value is null
28+
// must not be reported as missing. Previously the struct was
29+
// re-marshaled and ",omitempty" dropped the nil milestone key.
30+
name: "null-valued mapped field is not reported",
31+
raw: `{"number": 1, "milestone": null, "comments": 2}`,
32+
typ: &github.Issue{},
33+
want: nil,
34+
},
35+
{
36+
name: "genuinely unmapped field is reported",
37+
raw: `{"milestone": null, "this_field_does_not_exist": 123}`,
38+
typ: &github.Issue{},
39+
want: []string{"this_field_does_not_exist"},
40+
},
41+
{
42+
name: "url fields are skipped when requested",
43+
raw: `{"bogus_url": "x", "another_bogus": 1}`,
44+
typ: &github.Issue{},
45+
skipURLs: true,
46+
want: []string{"another_bogus"},
47+
},
48+
{
49+
name: "slice response inspects the first element",
50+
raw: `[{"totally_bogus": true}]`,
51+
typ: &[]github.Key{},
52+
want: []string{"totally_bogus"},
53+
},
54+
}
55+
56+
for _, tt := range tests {
57+
t.Run(tt.name, func(t *testing.T) {
58+
t.Parallel()
59+
60+
got, err := missingFields(json.RawMessage(tt.raw), tt.typ, tt.skipURLs)
61+
if err != nil {
62+
t.Fatalf("missingFields returned error: %v", err)
63+
}
64+
65+
var keys []string
66+
for _, m := range got {
67+
keys = append(keys, m.key)
68+
}
69+
if !slices.Equal(keys, tt.want) {
70+
t.Errorf("missingFields keys = %v, want %v", keys, tt.want)
71+
}
72+
})
73+
}
74+
}

0 commit comments

Comments
 (0)