Skip to content

Commit 48f320a

Browse files
committed
Path validation respects path parameters
Remove path uniqueness validation behavior where path parameters are ignored. Signed-off-by: Alex Dadgar <alex.dadgar@gmail.com>
1 parent 348543c commit 48f320a

File tree

4 files changed

+0
-67
lines changed

4 files changed

+0
-67
lines changed

helpers.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,6 @@ type pathHelper struct {
119119
// A collection of unexported helpers for path validation
120120
}
121121

122-
func (h *pathHelper) stripParametersInPath(path string) string {
123-
// Returns a path stripped from all path parameters, with multiple or trailing slashes removed.
124-
//
125-
// Stripping is performed on a slash-separated basis, e.g '/a{/b}' remains a{/b} and not /a.
126-
// - Trailing "/" make a difference, e.g. /a/ !~ /a (ex: canary/bitbucket.org/swagger.json)
127-
// - presence or absence of a parameter makes a difference, e.g. /a/{log} !~ /a/ (ex: canary/kubernetes/swagger.json)
128-
129-
// Regexp to extract parameters from path, with surrounding {}.
130-
// NOTE: important non-greedy modifier
131-
rexParsePathParam := mustCompileRegexp(`{[^{}]+?}`)
132-
strippedSegments := []string{}
133-
134-
for _, segment := range strings.Split(path, "/") {
135-
strippedSegments = append(strippedSegments, rexParsePathParam.ReplaceAllString(segment, "X"))
136-
}
137-
return strings.Join(strippedSegments, "/")
138-
}
139-
140122
func (h *pathHelper) extractPathParams(path string) (params []string) {
141123
// Extracts all params from a path, with surrounding "{}"
142124
rexParsePathParam := mustCompileRegexp(`{[^{}]+?}`)

spec.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -612,44 +612,15 @@ func (s *SpecValidator) validateRequiredProperties(path, in string, v *spec.Sche
612612
}
613613

614614
func (s *SpecValidator) validateParameters() *Result {
615-
// - for each method, path is unique, regardless of path parameters
616-
// e.g. GET:/petstore/{id}, GET:/petstore/{pet}, GET:/petstore are
617-
// considered duplicate paths
618615
// - each parameter should have a unique `name` and `type` combination
619616
// - each operation should have only 1 parameter of type body
620617
// - there must be at most 1 parameter in body
621618
// - parameters with pattern property must specify valid patterns
622619
// - $ref in parameters must resolve
623620
// - path param must be required
624621
res := new(Result)
625-
rexGarbledPathSegment := mustCompileRegexp(`.*[{}\s]+.*`)
626622
for method, pi := range s.expandedAnalyzer().Operations() {
627-
methodPaths := make(map[string]map[string]string)
628623
for path, op := range pi {
629-
pathToAdd := pathHelp.stripParametersInPath(path)
630-
631-
// Warn on garbled path afer param stripping
632-
if rexGarbledPathSegment.MatchString(pathToAdd) {
633-
res.AddWarnings(pathStrippedParamGarbledMsg(pathToAdd))
634-
}
635-
636-
// Check uniqueness of stripped paths
637-
if _, found := methodPaths[method][pathToAdd]; found {
638-
639-
// Sort names for stable, testable output
640-
if strings.Compare(path, methodPaths[method][pathToAdd]) < 0 {
641-
res.AddErrors(pathOverlapMsg(path, methodPaths[method][pathToAdd]))
642-
} else {
643-
res.AddErrors(pathOverlapMsg(methodPaths[method][pathToAdd], path))
644-
}
645-
} else {
646-
if _, found := methodPaths[method]; !found {
647-
methodPaths[method] = map[string]string{}
648-
}
649-
methodPaths[method][pathToAdd] = path // Original non stripped path
650-
651-
}
652-
653624
var bodyParams []string
654625
var paramNames []string
655626
var hasForm, hasBody bool

spec_messages.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ const (
126126
// NoValidResponseError indicates that no valid response description could be found for an operation
127127
NoValidResponseError = "operation %q has no valid response"
128128

129-
// PathOverlapError ...
130-
PathOverlapError = "path %s overlaps with %s"
131-
132129
// PathParamNotInPathError indicates that a parameter specified with in: path was not found in the path specification
133130
PathParamNotInPathError = "path param %q is not present in path %q"
134131

@@ -166,9 +163,6 @@ const (
166163
// ParamValidationTypeMismatch indicates that parameter has validation which does not match its type
167164
ParamValidationTypeMismatch = "validation keywords of parameter %q in path %q don't match its type %s"
168165

169-
// PathStrippedParamGarbledWarning ...
170-
PathStrippedParamGarbledWarning = "path stripped from path parameters %s contains {,} or white space. This is probably no what you want."
171-
172166
// ReadOnlyAndRequiredWarning ...
173167
ReadOnlyAndRequiredWarning = "Required property %s in %q should not be marked as both required and readOnly"
174168

@@ -245,12 +239,6 @@ func requiredButNotDefinedMsg(path, definition string) errors.Error {
245239
func pathParamGarbledMsg(path, param string) errors.Error {
246240
return errors.New(errors.CompositeErrorCode, PathParamGarbledWarning, path, param)
247241
}
248-
func pathStrippedParamGarbledMsg(path string) errors.Error {
249-
return errors.New(errors.CompositeErrorCode, PathStrippedParamGarbledWarning, path)
250-
}
251-
func pathOverlapMsg(path, arg string) errors.Error {
252-
return errors.New(errors.CompositeErrorCode, PathOverlapError, path, arg)
253-
}
254242
func invalidPatternInParamMsg(operation, param, pattern string) errors.Error {
255243
return errors.New(errors.CompositeErrorCode, InvalidPatternInParamError, operation, param, pattern)
256244
}

spec_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,6 @@ func TestSpec_ValidateParameters(t *testing.T) {
443443
pp.Parameters = nameParams
444444
sw.Paths.Paths["/pets/{name}"] = pp
445445

446-
validator = NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default)
447-
validator.spec = doc
448-
validator.analyzer = analysis.New(doc.Spec())
449-
res = validator.validateParameters()
450-
assert.NotEmpty(t, res.Errors)
451-
assert.Len(t, res.Errors, 1)
452-
assert.Contains(t, res.Errors[0].Error(), "overlaps with")
453-
454446
doc, _ = loads.Analyzed(PetStoreJSONMessage, "")
455447
validator = NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default)
456448
validator.spec = doc

0 commit comments

Comments
 (0)