Skip to content

Commit 688b7a2

Browse files
jacobm-splunkdaveshanley
authored andcommitted
Added secondary parsing if the initial base path parsing fails; the second attempt tries to remove the host
1 parent 2540a40 commit 688b7a2

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

paths/paths.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,21 @@ func getBasePaths(document *v3.Document) []string {
200200
// extract base path from document to check against paths.
201201
var basePaths []string
202202
for _, s := range document.Servers {
203-
u, _ := url.Parse(s.URL)
203+
var u *url.URL = nil
204+
u, err := url.Parse(s.URL)
205+
206+
// if the host contains special characters, we should attempt to split and parse only the relative path
207+
if err != nil {
208+
// split at first occurrence
209+
_, serverPath, _ := strings.Cut(strings.Replace(s.URL, "//", "", 1), "/")
210+
211+
if !strings.HasPrefix(serverPath, "/") {
212+
serverPath = "/" + serverPath
213+
}
214+
215+
u, _ = url.Parse(serverPath)
216+
}
217+
204218
if u != nil && u.Path != "" {
205219
basePaths = append(basePaths, u.Path)
206220
}

paths/paths_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,3 +659,41 @@ paths:
659659
assert.Equal(t, "GET Path '/not_here' not found", errs[0].Message)
660660

661661
}
662+
663+
func TestGetBasePaths(t *testing.T) {
664+
spec := `openapi: 3.1.0
665+
servers:
666+
- url: 'https://things.com/'
667+
- url: 'https://things.com/some/path'
668+
- url: 'https://things.com/more//paths//please'
669+
- url: 'https://{invalid}.com/'
670+
- url: 'https://{invalid}.com/some/path'
671+
- url: 'https://{invalid}.com/more//paths//please'
672+
- url: 'https://{invalid}.com//even//more//paths//please'
673+
paths:
674+
/dishy:
675+
get:
676+
operationId: one
677+
`
678+
679+
doc, err := libopenapi.NewDocument([]byte(spec))
680+
if err != nil {
681+
t.Fatal(err)
682+
}
683+
m, _ := doc.BuildV3Model()
684+
685+
basePaths := getBasePaths(&m.Model)
686+
687+
expectedPaths := []string{
688+
"/",
689+
"/some/path",
690+
"/more//paths//please",
691+
"/",
692+
"/some/path",
693+
"/more//paths//please",
694+
"/even//more//paths//please",
695+
}
696+
697+
assert.Equal(t, expectedPaths, basePaths)
698+
699+
}

0 commit comments

Comments
 (0)