Skip to content

Commit ba4394c

Browse files
authored
Update path param regex to have better tolerance and accuracy (#46)
1 parent 6d8b47c commit ba4394c

File tree

9 files changed

+55
-10
lines changed

9 files changed

+55
-10
lines changed

.github/workflows/cloc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ jobs:
2424
- name: Count Lines Of Code
2525
id: loc
2626
run: |
27-
curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.2/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && echo "b17e76bede22af0206b4918d3b3c4e7357f2a21b57f8de9e7c9dc0eb56b676c0 sccdiff" | shasum -c
27+
curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.3/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
28+
sccdiff_hash=$(git hash-object ./sccdiff)
29+
[ "$sccdiff_hash" == "ae8a07b687bd3dba60861584efe724351aa7ff63" ] || (echo "::error::unexpected hash for sccdiff, possible tampering: $sccdiff_hash" && exit 1)
2830
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
2931
echo "${OUTPUT}"
3032
OUTPUT="${OUTPUT//$'\n'/%0A}"

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
uses: golangci/golangci-lint-action@v3.1.0
2828
with:
2929
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
30-
version: v1.48.0
30+
version: v1.49.0
3131

3232
# Optional: working directory, useful for monorepos
3333
# working-directory: somedir

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ linters:
4444
- ireturn
4545
- exhaustruct
4646
- nonamedreturns
47+
- nosnakecase
48+
- structcheck
49+
- varcheck
50+
- deadcode
4751

4852
issues:
4953
exclude:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#GOLANGCI_LINT_VERSION := "v1.48.0" # Optional configuration to pinpoint golangci-lint version.
1+
#GOLANGCI_LINT_VERSION := "v1.49.0" # Optional configuration to pinpoint golangci-lint version.
22

33
# The head of Makefile determines location of dev-go to include standard targets.
44
GO ?= go

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ module github.com/swaggest/openapi-go
33
go 1.17
44

55
require (
6-
github.com/bool64/dev v0.2.19
6+
github.com/bool64/dev v0.2.20
77
github.com/stretchr/testify v1.8.0
88
github.com/swaggest/assertjson v1.7.0
9-
github.com/swaggest/jsonschema-go v0.3.36
9+
github.com/swaggest/jsonschema-go v0.3.40
1010
github.com/swaggest/refl v1.1.0
1111
gopkg.in/yaml.v2 v2.4.0
1212
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ github.com/bool64/dev v0.2.5/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zR
22
github.com/bool64/dev v0.2.10/go.mod h1:/csLrm+4oDSsKJRIVS0mrywAonLnYKFG8RvGT7Jh9b8=
33
github.com/bool64/dev v0.2.16/go.mod h1:/csLrm+4oDSsKJRIVS0mrywAonLnYKFG8RvGT7Jh9b8=
44
github.com/bool64/dev v0.2.17/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
5-
github.com/bool64/dev v0.2.18/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
6-
github.com/bool64/dev v0.2.19 h1:s++kaqTDpAJ53JJuCZr0up64tpjiMJFDJYRWZEYaIxc=
75
github.com/bool64/dev v0.2.19/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
6+
github.com/bool64/dev v0.2.20 h1:9eIRGdcg2kQW2NGza++QbOKidNNaK+KfWuUXcZFDejE=
7+
github.com/bool64/dev v0.2.20/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
88
github.com/bool64/shared v0.1.4/go.mod h1:ryGjsnQFh6BnEXClfVlEJrzjwzat7CmA8PNS5E+jPp0=
99
github.com/bool64/shared v0.1.5 h1:fp3eUhBsrSjNCQPcSdQqZxxh9bBwrYiZ+zOKFkM0/2E=
1010
github.com/bool64/shared v0.1.5/go.mod h1:081yz68YC9jeFB3+Bbmno2RFWvGKv1lPKkMP6MHJlPs=
@@ -62,8 +62,8 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK
6262
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
6363
github.com/swaggest/assertjson v1.7.0 h1:SKw5Rn0LQs6UvmGrIdaKQbMR1R3ncXm5KNon+QJ7jtw=
6464
github.com/swaggest/assertjson v1.7.0/go.mod h1:vxMJMehbSVJd+dDWFCKv3QRZKNTpy/ktZKTz9LOEDng=
65-
github.com/swaggest/jsonschema-go v0.3.36 h1:S1LHKKtBpoyvlJYDKXT4U7+9n8PryAdbIEpBc561LzI=
66-
github.com/swaggest/jsonschema-go v0.3.36/go.mod h1:vOJ5t/RW4tbLttUFGiegE+jlsKj8+iQT4MVDXhWSuw0=
65+
github.com/swaggest/jsonschema-go v0.3.40 h1:9EqQ9RvtdW69xfYODmyEKWOSZ12x5eiK+wGD2EVh/L4=
66+
github.com/swaggest/jsonschema-go v0.3.40/go.mod h1:ipIOmoFP64QyRUgyPyU/P9tayq2m2TlvUhyZHrhe3S4=
6767
github.com/swaggest/refl v1.1.0 h1:a+9a75Kv6ciMozPjVbOfcVTEQe81t2R3emvaD9oGQGc=
6868
github.com/swaggest/refl v1.1.0/go.mod h1:g3Qa6ki0A/L2yxiuUpT+cuBURuRaltF5SDQpg1kMZSY=
6969
github.com/yosuke-furukawa/json5 v0.1.2-0.20201207051438-cf7bb3f354ff/go.mod h1:sw49aWDqNdRJ6DYUtIQiaA3xyj2IL9tjeNYmX2ixwcU=

openapi3/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package openapi3 provides entities and helpers to manage OpenAPI 3.0.x schema.
2+
package openapi3

openapi3/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (p *PathItem) WithOperation(method string, operation Operation) *PathItem {
2323
return p.WithMapOfOperationValuesItem(strings.ToLower(method), operation)
2424
}
2525

26-
var regexFindPathParameter = regexp.MustCompile(`{([^}:]+)(:[^/]+)?(?:})`)
26+
var regexFindPathParameter = regexp.MustCompile(`{([^}:]+)(:[^}]+)?(?:})`)
2727

2828
// SetupOperation creates operation if it is not present and applies setup functions.
2929
func (s *Spec) SetupOperation(method, path string, setup ...func(*Operation) error) error {

openapi3/helper_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,40 @@ func TestSpec_SetOperation(t *testing.T) {
4040
assert.EqualError(t, s.AddOperation(http.MethodGet, "/another/{foo}", op),
4141
"duplicate parameter in path: foo, duplicate parameter in query: bar")
4242
}
43+
44+
func TestSpec_SetupOperation_pathRegex(t *testing.T) {
45+
s := openapi3.Spec{}
46+
47+
for _, tc := range []struct {
48+
path string
49+
params []string
50+
}{
51+
{`/{month}-{day}-{year}`, []string{"month", "day", "year"}},
52+
{`/{month}/{day}/{year}`, []string{"month", "day", "year"}},
53+
{`/{month:[\d]+}-{day:[\d]+}-{year:[\d]+}`, []string{"month", "day", "year"}},
54+
{`/{articleSlug:[a-z-]+}`, []string{"articleSlug"}},
55+
{"/articles/{rid:^[0-9]{5,6}}", []string{"rid"}},
56+
{"/articles/{rid:^[0-9]{5,6}}/{zid:^[0-9]{5,6}}", []string{"rid", "zid"}},
57+
{"/articles/{zid:^0[0-9]+}", []string{"zid"}},
58+
{"/articles/{name:^@[a-z]+}/posts", []string{"name"}},
59+
{"/articles/{op:^[0-9]+}/run", []string{"op"}},
60+
{"/users/{userID:[^/]+}", []string{"userID"}},
61+
{"/users/{userID:[^/]+}/books/{bookID:.+}", []string{"userID", "bookID"}},
62+
} {
63+
t.Run(tc.path, func(t *testing.T) {
64+
assert.NoError(t, s.SetupOperation(http.MethodGet, tc.path,
65+
func(operation *openapi3.Operation) error {
66+
var pp []openapi3.ParameterOrRef
67+
68+
for _, p := range tc.params {
69+
pp = append(pp, openapi3.Parameter{In: openapi3.ParameterInPath, Name: p}.ToParameterOrRef())
70+
}
71+
72+
operation.WithParameters(pp...)
73+
74+
return nil
75+
},
76+
))
77+
})
78+
}
79+
}

0 commit comments

Comments
 (0)