Skip to content

Commit

Permalink
Added secondary parsing if the initial base path parsing fails; the s…
Browse files Browse the repository at this point in the history
…econd attempt tries to remove the host
  • Loading branch information
jacobm-splunk authored and daveshanley committed Apr 25, 2024
1 parent 2540a40 commit 688b7a2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
16 changes: 15 additions & 1 deletion paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,21 @@ func getBasePaths(document *v3.Document) []string {
// extract base path from document to check against paths.
var basePaths []string
for _, s := range document.Servers {
u, _ := url.Parse(s.URL)
var u *url.URL = nil
u, err := url.Parse(s.URL)

// if the host contains special characters, we should attempt to split and parse only the relative path
if err != nil {
// split at first occurrence
_, serverPath, _ := strings.Cut(strings.Replace(s.URL, "//", "", 1), "/")

if !strings.HasPrefix(serverPath, "/") {
serverPath = "/" + serverPath
}

u, _ = url.Parse(serverPath)
}

if u != nil && u.Path != "" {
basePaths = append(basePaths, u.Path)
}
Expand Down
38 changes: 38 additions & 0 deletions paths/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,41 @@ paths:
assert.Equal(t, "GET Path '/not_here' not found", errs[0].Message)

}

func TestGetBasePaths(t *testing.T) {
spec := `openapi: 3.1.0
servers:
- url: 'https://things.com/'
- url: 'https://things.com/some/path'
- url: 'https://things.com/more//paths//please'
- url: 'https://{invalid}.com/'
- url: 'https://{invalid}.com/some/path'
- url: 'https://{invalid}.com/more//paths//please'
- url: 'https://{invalid}.com//even//more//paths//please'
paths:
/dishy:
get:
operationId: one
`

doc, err := libopenapi.NewDocument([]byte(spec))
if err != nil {
t.Fatal(err)
}
m, _ := doc.BuildV3Model()

basePaths := getBasePaths(&m.Model)

expectedPaths := []string{
"/",
"/some/path",
"/more//paths//please",
"/",
"/some/path",
"/more//paths//please",
"/even//more//paths//please",
}

assert.Equal(t, expectedPaths, basePaths)

}

0 comments on commit 688b7a2

Please sign in to comment.