Skip to content

Commit d9a349e

Browse files
committed
fix(parameters): return error on regex no-match for constrained template segments
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
1 parent 3c36636 commit d9a349e

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

parameters/path_parameters.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ func (v *paramValidator) ValidatePathParamsWithPathItem(request *http.Request, p
9191

9292
matches := rgx.FindStringSubmatch(submittedSegments[x])
9393
if matches == nil {
94-
continue
94+
validationErrors = append(validationErrors,
95+
errors.PathParameterMissing(p, pathValue, request.URL.Path))
96+
break
9597
}
9698
matches = matches[1:]
9799

parameters/path_parameters_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,7 +2395,7 @@ paths:
23952395
func TestValidatePathParamsWithPathItem_RegexNoMatchContinues(t *testing.T) {
23962396
// When a submitted path segment does not match the regex for a
23972397
// constrained path template segment (e.g. {id:[0-9]+} vs "abc"), the
2398-
// nil-match guard must skip the segment without panicking.
2398+
// guard must not panic and must return a validation error.
23992399
spec := `openapi: 3.1.0
24002400
paths:
24012401
/items/{id:[0-9]+}:
@@ -2415,13 +2415,12 @@ paths:
24152415
m, _ := doc.BuildV3Model()
24162416
v := NewParameterValidator(&m.Model)
24172417

2418-
// "abc" does not match ^([0-9]+)$; the nil-match guard should skip the
2419-
// segment cleanly rather than panicking on a nil FindStringSubmatch result.
2418+
// "abc" does not match ^([0-9]+)$; expect invalid + at least one error.
24202419
req, _ := http.NewRequest(http.MethodGet, "https://example.com/items/abc", nil)
24212420
pathItem := m.Model.Paths.PathItems.GetOrZero("/items/{id:[0-9]+}")
24222421
require.NotNil(t, pathItem)
24232422

2424-
assert.NotPanics(t, func() {
2425-
v.ValidatePathParamsWithPathItem(req, pathItem, "/items/{id:[0-9]+}")
2426-
})
2423+
valid, errs := v.ValidatePathParamsWithPathItem(req, pathItem, "/items/{id:[0-9]+}")
2424+
assert.False(t, valid)
2425+
assert.NotEmpty(t, errs)
24272426
}

0 commit comments

Comments
 (0)