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