Skip to content

Commit

Permalink
Fix path item resolution for unclean path (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Nov 3, 2022
1 parent e237298 commit b0eb17d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/bool64/dev v0.2.22
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.1
github.com/swaggest/assertjson v1.7.0
github.com/swaggest/jsonschema-go v0.3.40
github.com/swaggest/refl v1.1.0
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/swaggest/assertjson v1.7.0 h1:SKw5Rn0LQs6UvmGrIdaKQbMR1R3ncXm5KNon+QJ7jtw=
github.com/swaggest/assertjson v1.7.0/go.mod h1:vxMJMehbSVJd+dDWFCKv3QRZKNTpy/ktZKTz9LOEDng=
github.com/swaggest/jsonschema-go v0.3.40 h1:9EqQ9RvtdW69xfYODmyEKWOSZ12x5eiK+wGD2EVh/L4=
Expand Down
2 changes: 1 addition & 1 deletion openapi3/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func (s *Spec) SetupOperation(method, path string, setup ...func(*Operation) err
return fmt.Errorf("unexpected http method: %s", method)
}

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

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

var errs []string

pathItem := s.Paths.MapOfPathItemValues[path]
operation := pathItem.MapOfOperationValues[method]

for _, f := range setup {
Expand Down
23 changes: 23 additions & 0 deletions openapi3/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/swaggest/assertjson"
"github.com/swaggest/openapi-go/openapi3"
)

Expand Down Expand Up @@ -77,3 +78,25 @@ func TestSpec_SetupOperation_pathRegex(t *testing.T) {
})
}
}

func TestSpec_SetupOperation_uncleanPath(t *testing.T) {
s := openapi3.Spec{}
f := func(operation *openapi3.Operation) error {
operation.WithParameters(openapi3.Parameter{In: openapi3.ParameterInPath, Name: "userID"}.ToParameterOrRef())

return nil
}

assert.NoError(t, s.SetupOperation(http.MethodGet, "/users/{userID:[^/]+}", f))
assert.NoError(t, s.SetupOperation(http.MethodPost, "/users/{userID:[^/]+}", f))

assertjson.EqualMarshal(t, []byte(`{
"openapi":"","info":{"title":"","version":""},
"paths":{
"/users/{userID}":{
"get":{"parameters":[{"name":"userID","in":"path"}],"responses":{}},
"post":{"parameters":[{"name":"userID","in":"path"}],"responses":{}}
}
}
}`), s)
}

0 comments on commit b0eb17d

Please sign in to comment.