MCPcopy Create free account
hub / github.com/cli/cli / restProblems

Function restProblems

script/ghmock/restcheck.go:237–275  ·  view source on GitHub ↗

restProblems turns schema validation errors into short lines that use the same dotted paths as ghmock set --at. An allOf failure is replaced by the failures inside it. oneOf and anyOf failures aren't, because those list every alternative that didn't match.

(err error)

Source from the content-addressed store, hash-verified

235// failures inside it. oneOf and anyOf failures aren't, because those list
236// every alternative that didn't match.
237func restProblems(err error) []string {
238 var out []string
239 var walk func(e error, prefix []string)
240 walk = func(e error, prefix []string) {
241 switch e := e.(type) {
242 case nil:
243 case openapi3.MultiError:
244 for _, inner := range e {
245 walk(inner, prefix)
246 }
247 case *openapi3.SchemaError:
248 // GitHub's descriptions use oneOf where the alternatives overlap,
249 // so real responses can match more than one. That's fine.
250 if e.SchemaField == "oneOf" && strings.HasPrefix(e.Reason, "value matches more than one schema") {
251 return
252 }
253 pointer := append(append([]string(nil), prefix...), e.JSONPointer()...)
254 var inner openapi3.MultiError
255 if e.SchemaField == "allOf" && errors.As(e.Origin, &inner) {
256 walk(inner, pointer)
257 return
258 }
259 path := strings.Join(pointer, ".")
260 if path == "" {
261 path = "body"
262 }
263 reason := e.Reason
264 if strings.HasPrefix(reason, "property ") && strings.HasSuffix(reason, " is missing") {
265 reason = "missing, but GitHub's description requires it"
266 }
267 out = append(out, path+": "+reason)
268 default:
269 line, _, _ := strings.Cut(e.Error(), "\n")
270 out = append(out, "body: "+line)
271 }
272 }
273 walk(err, nil)
274 return dedupe(out)
275}

Callers 2

matchesSchemaFunction · 0.85
checkMethod · 0.85

Calls 3

dedupeFunction · 0.85
JoinMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected