@@ -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.
7879func 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}
0 commit comments