|
4 | 4 | package responses |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "github.com/pb33f/libopenapi-validator/errors" |
8 | | - "github.com/pb33f/libopenapi-validator/helpers" |
9 | | - "github.com/pb33f/libopenapi-validator/paths" |
10 | | - "github.com/pb33f/libopenapi/datamodel/high/base" |
11 | | - "github.com/pb33f/libopenapi/datamodel/high/v3" |
12 | | - "github.com/pb33f/libopenapi/utils" |
13 | | - "net/http" |
14 | | - "strconv" |
15 | | - "strings" |
| 7 | + "github.com/pb33f/libopenapi-validator/errors" |
| 8 | + "github.com/pb33f/libopenapi-validator/helpers" |
| 9 | + "github.com/pb33f/libopenapi-validator/paths" |
| 10 | + "github.com/pb33f/libopenapi/datamodel/high/base" |
| 11 | + "github.com/pb33f/libopenapi/datamodel/high/v3" |
| 12 | + "github.com/pb33f/libopenapi/utils" |
| 13 | + "net/http" |
| 14 | + "strconv" |
| 15 | + "strings" |
16 | 16 | ) |
17 | 17 |
|
18 | 18 | func (v *responseBodyValidator) ValidateResponseBody( |
19 | | - request *http.Request, |
20 | | - response *http.Response) (bool, []*errors.ValidationError) { |
21 | | - |
22 | | - // find path |
23 | | - var pathItem *v3.PathItem |
24 | | - var errs []*errors.ValidationError |
25 | | - if v.pathItem == nil { |
26 | | - pathItem, errs, _ = paths.FindPath(request, v.document) |
27 | | - if pathItem == nil || errs != nil { |
28 | | - v.errors = errs |
29 | | - return false, errs |
30 | | - } |
31 | | - } else { |
32 | | - pathItem = v.pathItem |
33 | | - } |
34 | | - |
35 | | - var validationErrors []*errors.ValidationError |
36 | | - operation := helpers.ExtractOperation(request, pathItem) |
37 | | - |
38 | | - // extract the response code from the response |
39 | | - httpCode := response.StatusCode |
40 | | - contentType := response.Header.Get(helpers.ContentTypeHeader) |
41 | | - |
42 | | - // extract the media type from the content type header. |
43 | | - mediaTypeSting, _, _ := helpers.ExtractContentType(contentType) |
44 | | - |
45 | | - // check if the response code is in the contract |
46 | | - foundResponse := operation.Responses.FindResponseByCode(httpCode) |
47 | | - if foundResponse != nil { |
48 | | - |
49 | | - // check content type has been defined in the contract |
50 | | - if mediaType, ok := foundResponse.Content[mediaTypeSting]; ok { |
51 | | - |
52 | | - validationErrors = append(validationErrors, |
53 | | - v.checkResponseSchema(request, response, mediaTypeSting, mediaType)...) |
54 | | - |
55 | | - } else { |
56 | | - |
57 | | - // check that the operation *actually* returns a body. (i.e. a 204 response) |
58 | | - if foundResponse.Content != nil { |
59 | | - |
60 | | - // content type not found in the contract |
61 | | - codeStr := strconv.Itoa(httpCode) |
62 | | - validationErrors = append(validationErrors, |
63 | | - errors.ResponseContentTypeNotFound(operation, request, response, codeStr, false)) |
64 | | - |
65 | | - } |
66 | | - } |
67 | | - } else { |
68 | | - |
69 | | - // no code match, check for default response |
70 | | - if operation.Responses.Default != nil { |
71 | | - |
72 | | - // check content type has been defined in the contract |
73 | | - if mediaType, ok := operation.Responses.Default.Content[mediaTypeSting]; ok { |
74 | | - |
75 | | - validationErrors = append(validationErrors, |
76 | | - v.checkResponseSchema(request, response, contentType, mediaType)...) |
77 | | - |
78 | | - } else { |
79 | | - |
80 | | - // check that the operation *actually* returns a body. (i.e. a 204 response) |
81 | | - if operation.Responses.Default.Content != nil { |
82 | | - |
83 | | - // content type not found in the contract |
84 | | - codeStr := strconv.Itoa(httpCode) |
85 | | - validationErrors = append(validationErrors, |
86 | | - errors.ResponseContentTypeNotFound(operation, request, response, codeStr, true)) |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - } else { |
91 | | - // TODO: add support for '2XX' and '3XX' responses in the contract |
92 | | - // no default, no code match, nothing! |
93 | | - validationErrors = append(validationErrors, |
94 | | - errors.ResponseCodeNotFound(operation, request, httpCode)) |
95 | | - } |
96 | | - } |
97 | | - if len(validationErrors) > 0 { |
98 | | - return false, validationErrors |
99 | | - } |
100 | | - return true, nil |
| 19 | + request *http.Request, |
| 20 | + response *http.Response) (bool, []*errors.ValidationError) { |
| 21 | + |
| 22 | + // find path |
| 23 | + var pathItem *v3.PathItem |
| 24 | + var errs []*errors.ValidationError |
| 25 | + if v.pathItem == nil { |
| 26 | + pathItem, errs, _ = paths.FindPath(request, v.document) |
| 27 | + if pathItem == nil || errs != nil { |
| 28 | + v.errors = errs |
| 29 | + return false, errs |
| 30 | + } |
| 31 | + } else { |
| 32 | + pathItem = v.pathItem |
| 33 | + } |
| 34 | + |
| 35 | + var validationErrors []*errors.ValidationError |
| 36 | + operation := helpers.ExtractOperation(request, pathItem) |
| 37 | + |
| 38 | + // extract the response code from the response |
| 39 | + httpCode := response.StatusCode |
| 40 | + contentType := response.Header.Get(helpers.ContentTypeHeader) |
| 41 | + |
| 42 | + // extract the media type from the content type header. |
| 43 | + mediaTypeSting, _, _ := helpers.ExtractContentType(contentType) |
| 44 | + |
| 45 | + // check if the response code is in the contract |
| 46 | + foundResponse := operation.Responses.FindResponseByCode(httpCode) |
| 47 | + if foundResponse != nil { |
| 48 | + |
| 49 | + // check content type has been defined in the contract |
| 50 | + if mediaType, ok := foundResponse.Content[mediaTypeSting]; ok { |
| 51 | + |
| 52 | + validationErrors = append(validationErrors, |
| 53 | + v.checkResponseSchema(request, response, mediaTypeSting, mediaType)...) |
| 54 | + |
| 55 | + } else { |
| 56 | + |
| 57 | + // check that the operation *actually* returns a body. (i.e. a 204 response) |
| 58 | + if foundResponse.Content != nil && len(foundResponse.Content) > 0 { |
| 59 | + |
| 60 | + // content type not found in the contract |
| 61 | + codeStr := strconv.Itoa(httpCode) |
| 62 | + validationErrors = append(validationErrors, |
| 63 | + errors.ResponseContentTypeNotFound(operation, request, response, codeStr, false)) |
| 64 | + |
| 65 | + } |
| 66 | + } |
| 67 | + } else { |
| 68 | + |
| 69 | + // no code match, check for default response |
| 70 | + if operation.Responses.Default != nil { |
| 71 | + |
| 72 | + // check content type has been defined in the contract |
| 73 | + if mediaType, ok := operation.Responses.Default.Content[mediaTypeSting]; ok { |
| 74 | + |
| 75 | + validationErrors = append(validationErrors, |
| 76 | + v.checkResponseSchema(request, response, contentType, mediaType)...) |
| 77 | + |
| 78 | + } else { |
| 79 | + |
| 80 | + // check that the operation *actually* returns a body. (i.e. a 204 response) |
| 81 | + if operation.Responses.Default.Content != nil && len(operation.Responses.Default.Content) > 0 { |
| 82 | + |
| 83 | + // content type not found in the contract |
| 84 | + codeStr := strconv.Itoa(httpCode) |
| 85 | + validationErrors = append(validationErrors, |
| 86 | + errors.ResponseContentTypeNotFound(operation, request, response, codeStr, true)) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + } else { |
| 91 | + // TODO: add support for '2XX' and '3XX' responses in the contract |
| 92 | + // no default, no code match, nothing! |
| 93 | + validationErrors = append(validationErrors, |
| 94 | + errors.ResponseCodeNotFound(operation, request, httpCode)) |
| 95 | + } |
| 96 | + } |
| 97 | + if len(validationErrors) > 0 { |
| 98 | + return false, validationErrors |
| 99 | + } |
| 100 | + return true, nil |
101 | 101 | } |
102 | 102 |
|
103 | 103 | func (v *responseBodyValidator) checkResponseSchema( |
104 | | - request *http.Request, |
105 | | - response *http.Response, |
106 | | - contentType string, |
107 | | - mediaType *v3.MediaType) []*errors.ValidationError { |
108 | | - |
109 | | - var validationErrors []*errors.ValidationError |
110 | | - |
111 | | - // currently, we can only validate JSON based responses, so check for the presence |
112 | | - // of 'json' in the content type (what ever it may be) so we can perform a schema check on it. |
113 | | - // anything other than JSON, will be ignored. |
114 | | - if strings.Contains(strings.ToLower(contentType), helpers.JSONType) { |
115 | | - |
116 | | - // extract schema from media type |
117 | | - if mediaType.Schema != nil { |
118 | | - |
119 | | - var schema *base.Schema |
120 | | - var renderedInline, renderedJSON []byte |
121 | | - |
122 | | - // have we seen this schema before? let's hash it and check the cache. |
123 | | - hash := mediaType.GoLow().Schema.Value.Hash() |
124 | | - |
125 | | - if cacheHit, ch := v.schemaCache[hash]; ch { |
126 | | - |
127 | | - // got a hit, use cached values |
128 | | - schema = cacheHit.schema |
129 | | - renderedInline = cacheHit.renderedInline |
130 | | - renderedJSON = cacheHit.renderedJSON |
131 | | - |
132 | | - } else { |
133 | | - |
134 | | - // render the schema inline and perform the intensive work of rendering and converting |
135 | | - // this is only performed once per schema and cached in the validator. |
136 | | - schema = mediaType.Schema.Schema() |
137 | | - renderedInline, _ = schema.RenderInline() |
138 | | - renderedJSON, _ = utils.ConvertYAMLtoJSON(renderedInline) |
139 | | - v.schemaCache[hash] = &schemaCache{ |
140 | | - schema: schema, |
141 | | - renderedInline: renderedInline, |
142 | | - renderedJSON: renderedJSON, |
143 | | - } |
144 | | - } |
145 | | - |
146 | | - // render the schema, to be used for validation |
147 | | - valid, vErrs := ValidateResponseSchema(request, response, schema, renderedInline, renderedJSON) |
148 | | - if !valid { |
149 | | - validationErrors = append(validationErrors, vErrs...) |
150 | | - } |
151 | | - } |
152 | | - } |
153 | | - return validationErrors |
| 104 | + request *http.Request, |
| 105 | + response *http.Response, |
| 106 | + contentType string, |
| 107 | + mediaType *v3.MediaType) []*errors.ValidationError { |
| 108 | + |
| 109 | + var validationErrors []*errors.ValidationError |
| 110 | + |
| 111 | + // currently, we can only validate JSON based responses, so check for the presence |
| 112 | + // of 'json' in the content type (what ever it may be) so we can perform a schema check on it. |
| 113 | + // anything other than JSON, will be ignored. |
| 114 | + if strings.Contains(strings.ToLower(contentType), helpers.JSONType) { |
| 115 | + |
| 116 | + // extract schema from media type |
| 117 | + if mediaType.Schema != nil { |
| 118 | + |
| 119 | + var schema *base.Schema |
| 120 | + var renderedInline, renderedJSON []byte |
| 121 | + |
| 122 | + // have we seen this schema before? let's hash it and check the cache. |
| 123 | + hash := mediaType.GoLow().Schema.Value.Hash() |
| 124 | + |
| 125 | + if cacheHit, ch := v.schemaCache[hash]; ch { |
| 126 | + |
| 127 | + // got a hit, use cached values |
| 128 | + schema = cacheHit.schema |
| 129 | + renderedInline = cacheHit.renderedInline |
| 130 | + renderedJSON = cacheHit.renderedJSON |
| 131 | + |
| 132 | + } else { |
| 133 | + |
| 134 | + // render the schema inline and perform the intensive work of rendering and converting |
| 135 | + // this is only performed once per schema and cached in the validator. |
| 136 | + schema = mediaType.Schema.Schema() |
| 137 | + renderedInline, _ = schema.RenderInline() |
| 138 | + renderedJSON, _ = utils.ConvertYAMLtoJSON(renderedInline) |
| 139 | + v.schemaCache[hash] = &schemaCache{ |
| 140 | + schema: schema, |
| 141 | + renderedInline: renderedInline, |
| 142 | + renderedJSON: renderedJSON, |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // render the schema, to be used for validation |
| 147 | + valid, vErrs := ValidateResponseSchema(request, response, schema, renderedInline, renderedJSON) |
| 148 | + if !valid { |
| 149 | + validationErrors = append(validationErrors, vErrs...) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + return validationErrors |
154 | 154 | } |
0 commit comments