From e4146b19b5588eaec32526aedf5ac31a2e857cd0 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Tue, 19 Mar 2024 13:47:14 +0900 Subject: [PATCH 1/2] Added test for panic when path is missing --- paths/paths_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/paths/paths_test.go b/paths/paths_test.go index 4437223..5eff42f 100644 --- a/paths/paths_test.go +++ b/paths/paths_test.go @@ -634,3 +634,28 @@ paths: assert.Equal(t, "two", pathItem.Post.OperationId) } + +func TestNewValidator_FindPathMissingWithBaseURLInServer(t *testing.T) { + + spec := `openapi: 3.1.0 +servers: + - url: 'https://things.com/' +paths: + /dishy: + get: + operationId: one +` + + doc, err := libopenapi.NewDocument([]byte(spec)) + if err != nil { + t.Fatal(err) + } + m, _ := doc.BuildV3Model() + + request, _ := http.NewRequest(http.MethodGet, "https://things.com/not_here", nil) + + _, errs, _ := FindPath(request, &m.Model) + assert.Len(t, errs, 1) + assert.Equal(t, "GET Path '/not_here' not found", errs[0].Message) + +} From d929361b7baf259624afcad689d0df87407d037f Mon Sep 17 00:00:00 2001 From: k1LoW Date: Tue, 19 Mar 2024 14:04:54 +0900 Subject: [PATCH 2/2] Keep basePaths unmodified. --- paths/paths.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/paths/paths.go b/paths/paths.go index cf883c4..4888bdb 100644 --- a/paths/paths.go +++ b/paths/paths.go @@ -223,11 +223,11 @@ func checkPathAgainstBase(docPath, urlPath string, basePaths []string) bool { if docPath == urlPath { return true } - for i := range basePaths { - if basePaths[i][len(basePaths[i])-1] == '/' { - basePaths[i] = basePaths[i][:len(basePaths[i])-1] + for _, basePath := range basePaths { + if basePath[len(basePath)-1] == '/' { + basePath = basePath[:len(basePath)-1] } - merged := fmt.Sprintf("%s%s", basePaths[i], urlPath) + merged := fmt.Sprintf("%s%s", basePath, urlPath) if docPath == merged { return true }