-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
validate_body.go
159 lines (137 loc) · 5.43 KB
/
validate_body.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package responses
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/k1LoW/runn/tmpmod/github.com/pb33f/libopenapi-validator/errors"
"github.com/k1LoW/runn/tmpmod/github.com/pb33f/libopenapi-validator/helpers"
"github.com/k1LoW/runn/tmpmod/github.com/pb33f/libopenapi-validator/paths"
"github.com/pb33f/libopenapi/datamodel/high/base"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils"
)
func (v *responseBodyValidator) ValidateResponseBody(
request *http.Request,
response *http.Response,
) (bool, []*errors.ValidationError) {
// find path
var pathItem *v3.PathItem
var pathFound string
var errs []*errors.ValidationError
if v.pathItem == nil {
pathItem, errs, pathFound = paths.FindPath(request, v.document)
if pathItem == nil || errs != nil {
v.errors = errs
return false, errs
}
} else {
pathItem = v.pathItem
pathFound = v.pathValue
}
var validationErrors []*errors.ValidationError
operation := helpers.ExtractOperation(request, pathItem)
// extract the response code from the response
httpCode := response.StatusCode
contentType := response.Header.Get(helpers.ContentTypeHeader)
codeStr := strconv.Itoa(httpCode)
// extract the media type from the content type header.
mediaTypeSting, _, _ := helpers.ExtractContentType(contentType)
// check if the response code is in the contract
foundResponse := operation.Responses.Codes.GetOrZero(codeStr)
if foundResponse == nil {
// check range definition for response codes
foundResponse = operation.Responses.Codes.GetOrZero(fmt.Sprintf("%dXX", httpCode/100))
if foundResponse != nil {
codeStr = fmt.Sprintf("%dXX", httpCode/100)
}
}
if foundResponse != nil {
if foundResponse.Content != nil { // only validate if we have content types.
// check content type has been defined in the contract
if mediaType, ok := foundResponse.Content.Get(mediaTypeSting); ok {
validationErrors = append(validationErrors,
v.checkResponseSchema(request, response, mediaTypeSting, mediaType)...)
} else {
// check that the operation *actually* returns a body. (i.e. a 204 response)
if foundResponse.Content != nil && orderedmap.Len(foundResponse.Content) > 0 {
// content type not found in the contract
validationErrors = append(validationErrors,
errors.ResponseContentTypeNotFound(operation, request, response, codeStr, false))
}
}
}
} else {
// no code match, check for default response
if operation.Responses.Default != nil && operation.Responses.Default.Content != nil {
// check content type has been defined in the contract
if mediaType, ok := operation.Responses.Default.Content.Get(mediaTypeSting); ok {
validationErrors = append(validationErrors,
v.checkResponseSchema(request, response, contentType, mediaType)...)
} else {
// check that the operation *actually* returns a body. (i.e. a 204 response)
if operation.Responses.Default.Content != nil && orderedmap.Len(operation.Responses.Default.Content) > 0 {
// content type not found in the contract
validationErrors = append(validationErrors,
errors.ResponseContentTypeNotFound(operation, request, response, codeStr, true))
}
}
} else {
// TODO: add support for '2XX' and '3XX' responses in the contract
// no default, no code match, nothing!
validationErrors = append(validationErrors,
errors.ResponseCodeNotFound(operation, request, httpCode))
}
}
errors.PopulateValidationErrors(validationErrors, request, pathFound)
if len(validationErrors) > 0 {
return false, validationErrors
}
return true, nil
}
func (v *responseBodyValidator) checkResponseSchema(
request *http.Request,
response *http.Response,
contentType string,
mediaType *v3.MediaType,
) []*errors.ValidationError {
var validationErrors []*errors.ValidationError
// currently, we can only validate JSON based responses, so check for the presence
// of 'json' in the content type (what ever it may be) so we can perform a schema check on it.
// anything other than JSON, will be ignored.
if strings.Contains(strings.ToLower(contentType), helpers.JSONType) {
// extract schema from media type
if mediaType.Schema != nil {
var schema *base.Schema
var renderedInline, renderedJSON []byte
// have we seen this schema before? let's hash it and check the cache.
hash := mediaType.GoLow().Schema.Value.Hash()
if cacheHit, ch := v.schemaCache.Load(hash); ch {
// got a hit, use cached values
schema = cacheHit.(*schemaCache).schema
renderedInline = cacheHit.(*schemaCache).renderedInline
renderedJSON = cacheHit.(*schemaCache).renderedJSON
} else {
// render the schema inline and perform the intensive work of rendering and converting
// this is only performed once per schema and cached in the validator.
schema = mediaType.Schema.Schema()
renderedInline, _ = schema.RenderInline()
renderedJSON, _ = utils.ConvertYAMLtoJSON(renderedInline)
v.schemaCache.Store(hash, &schemaCache{
schema: schema,
renderedInline: renderedInline,
renderedJSON: renderedJSON,
})
}
// render the schema, to be used for validation
valid, vErrs := ValidateResponseSchema(request, response, schema, renderedInline, renderedJSON)
if !valid {
validationErrors = append(validationErrors, vErrs...)
}
}
}
return validationErrors
}