check compares a response with GitHub's description of it. It returns the problems it found, or a note saying why it couldn't check.
(method, path string, resp *response)
| 160 | // check compares a response with GitHub's description of it. It returns the |
| 161 | // problems it found, or a note saying why it couldn't check. |
| 162 | func (idx *restIndex) check(method, path string, resp *response) ([]string, string) { |
| 163 | status, body := resp.status(), resp.bodyBytes() |
| 164 | route := idx.match(method, path) |
| 165 | if route == nil { |
| 166 | return nil, "not in GitHub's REST description" |
| 167 | } |
| 168 | op := route.item.GetOperation(method) |
| 169 | if op == nil { |
| 170 | return nil, fmt.Sprintf("GitHub documents no %s for %s", method, route.template) |
| 171 | } |
| 172 | ref := op.Responses.Status(status) |
| 173 | if ref == nil || ref.Value == nil { |
| 174 | return nil, fmt.Sprintf("GitHub doesn't document status %d for %s %s", status, method, route.template) |
| 175 | } |
| 176 | |
| 177 | trimmed := bytes.TrimSpace(body) |
| 178 | media := jsonMedia(ref.Value.Content) |
| 179 | if resp.RawBody != nil { |
| 180 | contentType, _, err := mime.ParseMediaType(resp.contentType()) |
| 181 | if err != nil { |
| 182 | return []string{fmt.Sprintf("Content-Type: %v", err)}, "" |
| 183 | } |
| 184 | media = ref.Value.Content.Get(contentType) |
| 185 | if media == nil { |
| 186 | return []string{fmt.Sprintf("Content-Type: %s is not documented for status %d", contentType, status)}, "" |
| 187 | } |
| 188 | if !strings.Contains(contentType, "json") { |
| 189 | if media.Schema == nil || media.Schema.Value == nil { |
| 190 | return nil, "no schema for the raw response body" |
| 191 | } |
| 192 | if !media.Schema.Value.Type.Is("string") { |
| 193 | return nil, fmt.Sprintf("body validation for %s is not supported", contentType) |
| 194 | } |
| 195 | return restProblems(media.Schema.Value.VisitJSON(string(body), openapi3.MultiErrors(), openapi3.VisitAsResponse())), "" |
| 196 | } |
| 197 | } |
| 198 | if media == nil || media.Schema == nil || media.Schema.Value == nil { |
| 199 | if len(trimmed) > 0 { |
| 200 | return nil, fmt.Sprintf("GitHub documents no JSON body for status %d", status) |
| 201 | } |
| 202 | return nil, "" |
| 203 | } |
| 204 | if len(trimmed) == 0 { |
| 205 | return []string{fmt.Sprintf("body: missing, but GitHub documents a JSON body for status %d", status)}, "" |
| 206 | } |
| 207 | var v any |
| 208 | if err := json.Unmarshal(trimmed, &v); err != nil { |
| 209 | return []string{fmt.Sprintf("body: not valid JSON: %v", err)}, "" |
| 210 | } |
| 211 | err := media.Schema.Value.VisitJSON(v, openapi3.MultiErrors(), openapi3.VisitAsResponse()) |
| 212 | return restProblems(err), "" |
| 213 | } |
| 214 | |
| 215 | // jsonMedia picks the JSON content of a documented response. |
| 216 | func jsonMedia(content openapi3.Content) *openapi3.MediaType { |
no test coverage detected