Skip to content

Commit b0eb17d

Browse files
authored
Fix path item resolution for unclean path (#50)
1 parent e237298 commit b0eb17d

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.17
44

55
require (
66
github.com/bool64/dev v0.2.22
7-
github.com/stretchr/testify v1.8.0
7+
github.com/stretchr/testify v1.8.1
88
github.com/swaggest/assertjson v1.7.0
99
github.com/swaggest/jsonschema-go v0.3.40
1010
github.com/swaggest/refl v1.1.0

go.sum

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
5656
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
5757
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5858
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
59+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
5960
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
6061
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
61-
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
6262
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
63+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
64+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
6365
github.com/swaggest/assertjson v1.7.0 h1:SKw5Rn0LQs6UvmGrIdaKQbMR1R3ncXm5KNon+QJ7jtw=
6466
github.com/swaggest/assertjson v1.7.0/go.mod h1:vxMJMehbSVJd+dDWFCKv3QRZKNTpy/ktZKTz9LOEDng=
6567
github.com/swaggest/jsonschema-go v0.3.40 h1:9EqQ9RvtdW69xfYODmyEKWOSZ12x5eiK+wGD2EVh/L4=

openapi3/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ func (s *Spec) SetupOperation(method, path string, setup ...func(*Operation) err
3737
return fmt.Errorf("unexpected http method: %s", method)
3838
}
3939

40-
pathItem := s.Paths.MapOfPathItemValues[path]
4140
pathParams := map[string]bool{}
4241

4342
if len(pathParametersSubmatches) > 0 {
@@ -52,6 +51,7 @@ func (s *Spec) SetupOperation(method, path string, setup ...func(*Operation) err
5251

5352
var errs []string
5453

54+
pathItem := s.Paths.MapOfPathItemValues[path]
5555
operation := pathItem.MapOfOperationValues[method]
5656

5757
for _, f := range setup {

openapi3/helper_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/assert"
8+
"github.com/swaggest/assertjson"
89
"github.com/swaggest/openapi-go/openapi3"
910
)
1011

@@ -77,3 +78,25 @@ func TestSpec_SetupOperation_pathRegex(t *testing.T) {
7778
})
7879
}
7980
}
81+
82+
func TestSpec_SetupOperation_uncleanPath(t *testing.T) {
83+
s := openapi3.Spec{}
84+
f := func(operation *openapi3.Operation) error {
85+
operation.WithParameters(openapi3.Parameter{In: openapi3.ParameterInPath, Name: "userID"}.ToParameterOrRef())
86+
87+
return nil
88+
}
89+
90+
assert.NoError(t, s.SetupOperation(http.MethodGet, "/users/{userID:[^/]+}", f))
91+
assert.NoError(t, s.SetupOperation(http.MethodPost, "/users/{userID:[^/]+}", f))
92+
93+
assertjson.EqualMarshal(t, []byte(`{
94+
"openapi":"","info":{"title":"","version":""},
95+
"paths":{
96+
"/users/{userID}":{
97+
"get":{"parameters":[{"name":"userID","in":"path"}],"responses":{}},
98+
"post":{"parameters":[{"name":"userID","in":"path"}],"responses":{}}
99+
}
100+
}
101+
}`), s)
102+
}

0 commit comments

Comments
 (0)