Serge Bazanski | cc25bdf | 2018-10-25 14:02:58 +0200 | [diff] [blame] | 1 | // Copyright 2015 go-swagger maintainers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package validate |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | "github.com/go-openapi/spec" |
| 22 | ) |
| 23 | |
| 24 | // defaultValidator validates default values in a spec. |
| 25 | // According to Swagger spec, default values MUST validate their schema. |
| 26 | type defaultValidator struct { |
| 27 | SpecValidator *SpecValidator |
| 28 | visitedSchemas map[string]bool |
| 29 | } |
| 30 | |
| 31 | // resetVisited resets the internal state of visited schemas |
| 32 | func (d *defaultValidator) resetVisited() { |
| 33 | d.visitedSchemas = map[string]bool{} |
| 34 | } |
| 35 | |
| 36 | // beingVisited asserts a schema is being visited |
| 37 | func (d *defaultValidator) beingVisited(path string) { |
| 38 | d.visitedSchemas[path] = true |
| 39 | } |
| 40 | |
| 41 | // isVisited tells if a path has already been visited |
| 42 | func (d *defaultValidator) isVisited(path string) bool { |
| 43 | found := d.visitedSchemas[path] |
| 44 | if !found { |
| 45 | // search for overlapping paths |
| 46 | frags := strings.Split(path, ".") |
| 47 | if len(frags) < 2 { |
| 48 | // shortcut exit on smaller paths |
| 49 | return found |
| 50 | } |
| 51 | last := len(frags) - 1 |
| 52 | var currentFragStr, parent string |
| 53 | for i := range frags { |
| 54 | if i == 0 { |
| 55 | currentFragStr = frags[last] |
| 56 | } else { |
| 57 | currentFragStr = strings.Join([]string{frags[last-i], currentFragStr}, ".") |
| 58 | } |
| 59 | if i < last { |
| 60 | parent = strings.Join(frags[0:last-i], ".") |
| 61 | } else { |
| 62 | parent = "" |
| 63 | } |
| 64 | if strings.HasSuffix(parent, currentFragStr) { |
| 65 | found = true |
| 66 | break |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | return found |
| 71 | } |
| 72 | |
| 73 | // Validate validates the default values declared in the swagger spec |
| 74 | func (d *defaultValidator) Validate() (errs *Result) { |
| 75 | errs = new(Result) |
| 76 | if d == nil || d.SpecValidator == nil { |
| 77 | return errs |
| 78 | } |
| 79 | d.resetVisited() |
| 80 | errs.Merge(d.validateDefaultValueValidAgainstSchema()) // error - |
| 81 | return errs |
| 82 | } |
| 83 | |
| 84 | func (d *defaultValidator) validateDefaultValueValidAgainstSchema() *Result { |
| 85 | // every default value that is specified must validate against the schema for that property |
| 86 | // headers, items, parameters, schema |
| 87 | |
| 88 | res := new(Result) |
| 89 | s := d.SpecValidator |
| 90 | |
| 91 | for method, pathItem := range s.analyzer.Operations() { |
| 92 | if pathItem != nil { // Safeguard |
| 93 | for path, op := range pathItem { |
| 94 | // parameters |
| 95 | for _, param := range paramHelp.safeExpandedParamsFor(path, method, op.ID, res, s) { |
| 96 | if param.Default != nil && param.Required { |
| 97 | res.AddWarnings(requiredHasDefaultMsg(param.Name, param.In)) |
| 98 | } |
| 99 | |
| 100 | // reset explored schemas to get depth-first recursive-proof exploration |
| 101 | d.resetVisited() |
| 102 | |
| 103 | // Check simple parameters first |
| 104 | // default values provided must validate against their inline definition (no explicit schema) |
| 105 | if param.Default != nil && param.Schema == nil { |
| 106 | // check param default value is valid |
| 107 | red := NewParamValidator(¶m, s.KnownFormats).Validate(param.Default) |
| 108 | if red.HasErrorsOrWarnings() { |
| 109 | res.AddErrors(defaultValueDoesNotValidateMsg(param.Name, param.In)) |
| 110 | res.Merge(red) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Recursively follows Items and Schemas |
| 115 | if param.Items != nil { |
| 116 | red := d.validateDefaultValueItemsAgainstSchema(param.Name, param.In, ¶m, param.Items) |
| 117 | if red.HasErrorsOrWarnings() { |
| 118 | res.AddErrors(defaultValueItemsDoesNotValidateMsg(param.Name, param.In)) |
| 119 | res.Merge(red) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if param.Schema != nil { |
| 124 | // Validate default value against schema |
| 125 | red := d.validateDefaultValueSchemaAgainstSchema(param.Name, param.In, param.Schema) |
| 126 | if red.HasErrorsOrWarnings() { |
| 127 | res.AddErrors(defaultValueDoesNotValidateMsg(param.Name, param.In)) |
| 128 | res.Merge(red) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if op.Responses != nil { |
| 134 | if op.Responses.Default != nil { |
| 135 | // Same constraint on default Response |
| 136 | res.Merge(d.validateDefaultInResponse(op.Responses.Default, "default", path, 0, op.ID)) |
| 137 | } |
| 138 | // Same constraint on regular Responses |
| 139 | if op.Responses.StatusCodeResponses != nil { // Safeguard |
| 140 | for code, r := range op.Responses.StatusCodeResponses { |
| 141 | res.Merge(d.validateDefaultInResponse(&r, "response", path, code, op.ID)) |
| 142 | } |
| 143 | } |
| 144 | } else { |
| 145 | // Empty op.ID means there is no meaningful operation: no need to report a specific message |
| 146 | if op.ID != "" { |
| 147 | res.AddErrors(noValidResponseMsg(op.ID)) |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | if s.spec.Spec().Definitions != nil { // Safeguard |
| 154 | // reset explored schemas to get depth-first recursive-proof exploration |
| 155 | d.resetVisited() |
| 156 | for nm, sch := range s.spec.Spec().Definitions { |
| 157 | res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("definitions.%s", nm), "body", &sch)) |
| 158 | } |
| 159 | } |
| 160 | return res |
| 161 | } |
| 162 | |
| 163 | func (d *defaultValidator) validateDefaultInResponse(resp *spec.Response, responseType, path string, responseCode int, operationID string) *Result { |
| 164 | s := d.SpecValidator |
| 165 | |
| 166 | response, res := responseHelp.expandResponseRef(resp, path, s) |
| 167 | if !res.IsValid() { |
| 168 | return res |
| 169 | } |
| 170 | |
| 171 | responseName, responseCodeAsStr := responseHelp.responseMsgVariants(responseType, responseCode) |
| 172 | |
| 173 | if response.Headers != nil { // Safeguard |
| 174 | for nm, h := range response.Headers { |
| 175 | // reset explored schemas to get depth-first recursive-proof exploration |
| 176 | d.resetVisited() |
| 177 | |
| 178 | if h.Default != nil { |
| 179 | red := NewHeaderValidator(nm, &h, s.KnownFormats).Validate(h.Default) |
| 180 | if red.HasErrorsOrWarnings() { |
| 181 | res.AddErrors(defaultValueHeaderDoesNotValidateMsg(operationID, nm, responseName)) |
| 182 | res.Merge(red) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Headers have inline definition, like params |
| 187 | if h.Items != nil { |
| 188 | red := d.validateDefaultValueItemsAgainstSchema(nm, "header", &h, h.Items) |
| 189 | if red.HasErrorsOrWarnings() { |
| 190 | res.AddErrors(defaultValueHeaderItemsDoesNotValidateMsg(operationID, nm, responseName)) |
| 191 | res.Merge(red) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if _, err := compileRegexp(h.Pattern); err != nil { |
| 196 | res.AddErrors(invalidPatternInHeaderMsg(operationID, nm, responseName, h.Pattern, err)) |
| 197 | } |
| 198 | |
| 199 | // Headers don't have schema |
| 200 | } |
| 201 | } |
| 202 | if response.Schema != nil { |
| 203 | // reset explored schemas to get depth-first recursive-proof exploration |
| 204 | d.resetVisited() |
| 205 | |
| 206 | red := d.validateDefaultValueSchemaAgainstSchema(responseCodeAsStr, "response", response.Schema) |
| 207 | if red.HasErrorsOrWarnings() { |
| 208 | // Additional message to make sure the context of the error is not lost |
| 209 | res.AddErrors(defaultValueInDoesNotValidateMsg(operationID, responseName)) |
| 210 | res.Merge(red) |
| 211 | } |
| 212 | } |
| 213 | return res |
| 214 | } |
| 215 | |
| 216 | func (d *defaultValidator) validateDefaultValueSchemaAgainstSchema(path, in string, schema *spec.Schema) *Result { |
| 217 | if schema == nil || d.isVisited(path) { |
| 218 | // Avoids recursing if we are already done with that check |
| 219 | return nil |
| 220 | } |
| 221 | d.beingVisited(path) |
| 222 | res := new(Result) |
| 223 | s := d.SpecValidator |
| 224 | |
| 225 | if schema.Default != nil { |
| 226 | res.Merge(NewSchemaValidator(schema, s.spec.Spec(), path+".default", s.KnownFormats).Validate(schema.Default)) |
| 227 | } |
| 228 | if schema.Items != nil { |
| 229 | if schema.Items.Schema != nil { |
| 230 | res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+".items.default", in, schema.Items.Schema)) |
| 231 | } |
| 232 | // Multiple schemas in items |
| 233 | if schema.Items.Schemas != nil { // Safeguard |
| 234 | for i, sch := range schema.Items.Schemas { |
| 235 | res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.items[%d].default", path, i), in, &sch)) |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | if _, err := compileRegexp(schema.Pattern); err != nil { |
| 240 | res.AddErrors(invalidPatternInMsg(path, in, schema.Pattern)) |
| 241 | } |
| 242 | if schema.AdditionalItems != nil && schema.AdditionalItems.Schema != nil { |
| 243 | // NOTE: we keep validating values, even though additionalItems is not supported by Swagger 2.0 (and 3.0 as well) |
| 244 | res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalItems", path), in, schema.AdditionalItems.Schema)) |
| 245 | } |
| 246 | for propName, prop := range schema.Properties { |
| 247 | res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+"."+propName, in, &prop)) |
| 248 | } |
| 249 | for propName, prop := range schema.PatternProperties { |
| 250 | res.Merge(d.validateDefaultValueSchemaAgainstSchema(path+"."+propName, in, &prop)) |
| 251 | } |
| 252 | if schema.AdditionalProperties != nil && schema.AdditionalProperties.Schema != nil { |
| 253 | res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.additionalProperties", path), in, schema.AdditionalProperties.Schema)) |
| 254 | } |
| 255 | if schema.AllOf != nil { |
| 256 | for i, aoSch := range schema.AllOf { |
| 257 | res.Merge(d.validateDefaultValueSchemaAgainstSchema(fmt.Sprintf("%s.allOf[%d]", path, i), in, &aoSch)) |
| 258 | } |
| 259 | } |
| 260 | return res |
| 261 | } |
| 262 | |
| 263 | func (d *defaultValidator) validateDefaultValueItemsAgainstSchema(path, in string, root interface{}, items *spec.Items) *Result { |
| 264 | res := new(Result) |
| 265 | s := d.SpecValidator |
| 266 | if items != nil { |
| 267 | if items.Default != nil { |
| 268 | res.Merge(newItemsValidator(path, in, items, root, s.KnownFormats).Validate(0, items.Default)) |
| 269 | } |
| 270 | if items.Items != nil { |
| 271 | res.Merge(d.validateDefaultValueItemsAgainstSchema(path+"[0].default", in, root, items.Items)) |
| 272 | } |
| 273 | if _, err := compileRegexp(items.Pattern); err != nil { |
| 274 | res.AddErrors(invalidPatternInMsg(path, in, items.Pattern)) |
| 275 | } |
| 276 | } |
| 277 | return res |
| 278 | } |