Debian Codegraph @

errors/response_errors.go

raw
1 // Copyright 2023-2026 Princess Beef Heavy Industries, LLC / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package errors
5
6 import (
7 "fmt"
8 "net/http"
9 "strings"
10
11 "github.com/pb33f/libopenapi/orderedmap"
12
13 v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
14
15 "github.com/pb33f/libopenapi-validator/helpers"
16 )
17
18 func ResponseContentTypeNotFound(op *v3.Operation,
19 request *http.Request,
20 response *http.Response,
21 code string,
22 isDefault bool,
23 ) *ValidationError {
24 ct := response.Header.Get(helpers.ContentTypeHeader)
25 mediaTypeString, _, _ := helpers.ExtractContentType(ct)
26 var ctypes []string
27 specLine, specCol := 1, 0
28 var contentMap *orderedmap.Map[string, *v3.MediaType]
29
30 // check for a default type (applies to all codes without a match)
31 if !isDefault {
32 resp := op.Responses.Codes.GetOrZero(code)
33 if resp != nil {
34 for pair := orderedmap.First(resp.Content); pair != nil; pair = pair.Next() {
35 ctypes = append(ctypes, pair.Key())
36 }
37 contentMap = resp.Content
38 if low := resp.GoLow(); low != nil && low.Content.KeyNode != nil {
39 specLine = low.Content.KeyNode.Line
40 specCol = low.Content.KeyNode.Column
41 }
42 }
43 } else {
44 if op.Responses.Default != nil {
45 for pair := orderedmap.First(op.Responses.Default.Content); pair != nil; pair = pair.Next() {
46 ctypes = append(ctypes, pair.Key())
47 }
48 contentMap = op.Responses.Default.Content
49 if low := op.Responses.Default.GoLow(); low != nil && low.Content.KeyNode != nil {
50 specLine = low.Content.KeyNode.Line
51 specCol = low.Content.KeyNode.Column
52 }
53 }
54 }
55 return &ValidationError{
56 ValidationType: helpers.ResponseBodyValidation,
57 ValidationSubType: helpers.RequestBodyContentType,
58 Message: fmt.Sprintf("%s / %s operation response content type '%s' does not exist",
59 request.Method, code, mediaTypeString),
60 Reason: fmt.Sprintf("The content type '%s' of the %s response received has not "+
61 "been defined, it's an unknown type", mediaTypeString, request.Method),
62 SpecLine: specLine,
63 SpecCol: specCol,
64 Context: op,
65 HowToFix: fmt.Sprintf(HowToFixInvalidContentType,
66 orderedmap.Len(contentMap), strings.Join(ctypes, ", ")),
67 }
68 }
69
70 func ResponseCodeNotFound(op *v3.Operation, request *http.Request, code int) *ValidationError {
71 specLine, specCol := 1, 0
72 if low := op.GoLow(); low != nil && low.Responses.KeyNode != nil {
73 specLine = low.Responses.KeyNode.Line
74 specCol = low.Responses.KeyNode.Column
75 }
76 return &ValidationError{
77 ValidationType: helpers.ResponseBodyValidation,
78 ValidationSubType: helpers.ResponseBodyResponseCode,
79 Message: fmt.Sprintf("%s operation request response code '%d' does not exist",
80 request.Method, code),
81 Reason: fmt.Sprintf("The response code '%d' of the %s request submitted has not "+
82 "been defined, it's an unknown type", code, request.Method),
83 SpecLine: specLine,
84 SpecCol: specCol,
85 Context: op,
86 HowToFix: HowToFixInvalidResponseCode,
87 }
88 }