Skip to content

Commit c16b8e7

Browse files
committed
Added support for paths with fragments.
pb33f/wiretap#78 Signed-off-by: Dave Shanley <[email protected]>
1 parent 892369d commit c16b8e7

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/pb33f/libopenapi-validator
33
go 1.21
44

55
require (
6-
github.com/pb33f/libopenapi v0.15.0
6+
github.com/pb33f/libopenapi v0.15.2
77
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
88
github.com/stretchr/testify v1.8.4
99
github.com/vmware-labs/yaml-jsonpath v0.3.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
5454
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
5555
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
5656
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
57-
github.com/pb33f/libopenapi v0.15.0 h1:AoBYIY3HXqDDF8O9kcudlqWaRFZZJmgtueE649oHzIw=
58-
github.com/pb33f/libopenapi v0.15.0/go.mod h1:m+4Pwri31UvcnZjuP8M7TlbR906DXJmMvYsbis234xg=
57+
github.com/pb33f/libopenapi v0.15.2 h1:Jit99QiDVgrYgTbEeBmzlUnrR/oewH6K24OTVPRbYaU=
58+
github.com/pb33f/libopenapi v0.15.2/go.mod h1:m+4Pwri31UvcnZjuP8M7TlbR906DXJmMvYsbis234xg=
5959
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6060
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6161
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=

paths/paths.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func FindPath(request *http.Request, document *v3.Document) (*v3.PathItem, []*er
3636

3737
// strip any base path
3838
stripped := stripBaseFromPath(request.URL.Path, basePaths)
39+
if request.URL.Fragment != "" {
40+
stripped = fmt.Sprintf("%s#%s", stripped, request.URL.Fragment)
41+
}
3942

4043
reqPathSegments := strings.Split(stripped, "/")
4144
if reqPathSegments[0] == "" {
@@ -48,6 +51,15 @@ pathFound:
4851
for pair := orderedmap.First(document.Paths.PathItems); pair != nil; pair = pair.Next() {
4952
path := pair.Key()
5053
pathItem := pair.Value()
54+
55+
// if the stripped path has a fragment, then use that as part of the lookup
56+
// if not, then strip off any fragments from the pathItem
57+
if !strings.Contains(stripped, "#") {
58+
if strings.Contains(path, "#") {
59+
path = strings.Split(path, "#")[0]
60+
}
61+
}
62+
5163
segs := strings.Split(path, "/")
5264
if segs[0] == "" {
5365
segs = segs[1:]
@@ -256,11 +268,8 @@ func comparePaths(mapped, requested []string,
256268
var imploded []string
257269
for i, seg := range mapped {
258270
s := seg
259-
// sOrig := seg
260-
// check for braces
261271
if strings.Contains(seg, "{") {
262272
s = requested[i]
263-
// sOrig = s
264273
}
265274
// check param against type, check if it's a number or not, and if it validates.
266275
for p := range params {

paths/paths_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,33 @@ paths:
601601

602602
assert.Len(t, errs, 1)
603603
}
604+
605+
func TestNewValidator_FindPathWithFragment(t *testing.T) {
606+
607+
spec := `openapi: 3.1.0
608+
paths:
609+
/hashy#one:
610+
post:
611+
operationId: one
612+
/hashy#two:
613+
post:
614+
operationId: two
615+
`
616+
617+
doc, _ := libopenapi.NewDocument([]byte(spec))
618+
m, _ := doc.BuildV3Model()
619+
620+
request, _ := http.NewRequest(http.MethodPost, "https://things.com/hashy#one", nil)
621+
622+
pathItem, errs, _ := FindPath(request, &m.Model)
623+
assert.Len(t, errs, 0)
624+
assert.NotNil(t, pathItem)
625+
assert.Equal(t, "one", pathItem.Post.OperationId)
626+
627+
request, _ = http.NewRequest(http.MethodPost, "https://things.com/hashy#two", nil)
628+
pathItem, errs, _ = FindPath(request, &m.Model)
629+
assert.Len(t, errs, 0)
630+
assert.NotNil(t, pathItem)
631+
assert.Equal(t, "two", pathItem.Post.OperationId)
632+
633+
}

requests/validate_request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
var instanceLocationRegex = regexp.MustCompile(`^/(\d+)`)
2525

26-
// ValidateRequestSchema will validate an http.Request pointer against a schema.
26+
// ValidateRequestSchema will validate a http.Request pointer against a schema.
2727
// If validation fails, it will return a list of validation errors as the second return value.
2828
func ValidateRequestSchema(
2929
request *http.Request,

0 commit comments

Comments
 (0)