Skip to content

Commit e882dce

Browse files
committed
Added more coverage for deep base paths
multible path args with deep base paths now also supported. Signed-off-by: Dave Shanley <[email protected]>
1 parent 3b6e7bc commit e882dce

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

paths/paths.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@ func FindPath(request *http.Request, document *v3.Document) (*v3.PathItem, []*er
2424

2525
var validationErrors []*errors.ValidationError
2626

27-
reqPathSegments := strings.Split(request.URL.Path, "/")
28-
if reqPathSegments[0] == "" {
29-
reqPathSegments = reqPathSegments[1:]
30-
}
31-
3227
// extract base path from document to check against paths.
3328
basePaths := make([]string, len(document.Servers))
3429
for i, s := range document.Servers {
3530
u, _ := url.Parse(s.URL)
3631
basePaths[i] = u.Path
3732
}
3833

34+
// strip any base path
35+
stripped := stripBaseFromPath(request.URL.Path, basePaths)
36+
37+
reqPathSegments := strings.Split(stripped, "/")
38+
if reqPathSegments[0] == "" {
39+
reqPathSegments = reqPathSegments[1:]
40+
}
41+
3942
var pItem *v3.PathItem
4043
var foundPath string
4144
pathFound:
@@ -225,6 +228,15 @@ func checkPathAgainstBase(docPath, urlPath string, basePaths []string) bool {
225228
return false
226229
}
227230

231+
func stripBaseFromPath(path string, basePaths []string) string {
232+
for i := range basePaths {
233+
if strings.HasPrefix(path, basePaths[i]) {
234+
return path[len(basePaths[i]):]
235+
}
236+
}
237+
return path
238+
}
239+
228240
func comparePaths(mapped, requested []string,
229241
params []*v3.Parameter, basePaths []string) (bool, []*errors.ValidationError) {
230242

paths/paths_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,28 @@ paths:
276276

277277
}
278278

279+
func TestNewValidator_FindPathWithBaseURLInServer_Args(t *testing.T) {
280+
281+
spec := `openapi: 3.1.0
282+
servers:
283+
- url: https://things.com/base3/base4/base5/base6/
284+
paths:
285+
/user/{userId}/thing/{thingId}:
286+
post:
287+
operationId: addUser
288+
`
289+
290+
doc, _ := libopenapi.NewDocument([]byte(spec))
291+
m, _ := doc.BuildV3Model()
292+
293+
// check against a deeper base
294+
request, _ := http.NewRequest(http.MethodPost, "https://things.com/base3/base4/base5/base6/user/1234/thing/abcd", nil)
295+
pathItem, _, _ := FindPath(request, &m.Model)
296+
assert.NotNil(t, pathItem)
297+
assert.Equal(t, "addUser", pathItem.Post.OperationId)
298+
299+
}
300+
279301
func TestNewValidator_FindPathMissing(t *testing.T) {
280302

281303
spec := `openapi: 3.1.0

0 commit comments

Comments
 (0)