diff --git a/generator/match_rule.go b/generator/match_rule.go index 196c7f7..81a2382 100644 --- a/generator/match_rule.go +++ b/generator/match_rule.go @@ -56,16 +56,16 @@ func createServerUrlMatchingGroup(serverUrls []string) string { return encapsulateRegexToken(rex.Group.Composite(serverUrlsTokens...)) } -func getPathParamType(name string, params *openapi3.Parameters) string { +func getPathParamType(name string, params *openapi3.Parameters) *openapi3.Types { if params == nil { log.Default().Print("no path parameters has been defined") - return "" + return nil } p := params.GetByInAndName(openapi3.ParameterInPath, name) if p == nil { log.Default().Printf("path param %s is not defined", name) - return "" + return nil } return p.Schema.Value.Type @@ -73,14 +73,16 @@ func getPathParamType(name string, params *openapi3.Parameters) string { func createParamsMatchingGroup(name string, params *openapi3.Parameters) string { var t dialect.Token - switch getPathParamType(name, params) { - case "string": + paramType := getPathParamType(name, params) + if paramType == nil { + t = defaultToken + } else if paramType.Is("string") { t = stringToken - case "number": + } else if paramType.Is("number") { t = numberToken - case "integer": + } else if paramType.Is("integer") { t = integerToken - default: + } else { t = defaultToken } diff --git a/generator/match_rule_test.go b/generator/match_rule_test.go index 17a874b..18b8d9d 100644 --- a/generator/match_rule_test.go +++ b/generator/match_rule_test.go @@ -69,7 +69,8 @@ func TestGenerateMatchRuleWithStringPathParams(t *testing.T) { URL: "/resource/<.+>", Methods: []string{"GET"}, } - param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: "string"}) + types := &openapi3.Types{"string"} + param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: types}) matchRule, err := createMatchRule([]string{}, "GET", "/resource/{param}", &openapi3.Parameters{&openapi3.ParameterRef{ Ref: "param", Value: param, @@ -84,7 +85,8 @@ func TestGenerateMatchRuleWithNumberPathParams(t *testing.T) { URL: "/resource/<((\\x2D|\\+)?\\d+(?:\\.\\d+)?)>", Methods: []string{"GET"}, } - param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: "number"}) + types := &openapi3.Types{"number"} + param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: types}) matchRule, err := createMatchRule([]string{}, "GET", "/resource/{param}", &openapi3.Parameters{&openapi3.ParameterRef{ Ref: "param", Value: param, @@ -99,7 +101,8 @@ func TestGenerateMatchRuleWithIntegerPathParams(t *testing.T) { URL: "/resource/<\\d+>", Methods: []string{"GET"}, } - param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: "integer"}) + types := &openapi3.Types{"integer"} + param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: types}) matchRule, err := createMatchRule([]string{}, "GET", "/resource/{param}", &openapi3.Parameters{&openapi3.ParameterRef{ Ref: "param", Value: param, @@ -114,7 +117,8 @@ func TestGenerateMatchRuleWithBooleanPathParams(t *testing.T) { URL: "/resource/<.+>", Methods: []string{"GET"}, } - param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: "boolean"}) + types := &openapi3.Types{"boolean"} + param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: types}) matchRule, err := createMatchRule([]string{}, "GET", "/resource/{param}", &openapi3.Parameters{&openapi3.ParameterRef{ Ref: "param", Value: param, @@ -129,7 +133,8 @@ func TestGenerateMatchRuleWithArrayPathParams(t *testing.T) { URL: "/resource/<.+>", Methods: []string{"GET"}, } - param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: "array"}) + types := &openapi3.Types{"array"} + param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: types}) matchRule, err := createMatchRule([]string{}, "GET", "/resource/{param}", &openapi3.Parameters{&openapi3.ParameterRef{ Ref: "param", Value: param, @@ -144,7 +149,8 @@ func TestGenerateMatchRuleWithObjectPathParams(t *testing.T) { URL: "/resource/<.+>", Methods: []string{"GET"}, } - param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: "object"}) + types := &openapi3.Types{"object"} + param := openapi3.NewPathParameter("param").WithSchema(&openapi3.Schema{Type: types}) matchRule, err := createMatchRule([]string{}, "GET", "/resource/{param}", &openapi3.Parameters{&openapi3.ParameterRef{ Ref: "param", Value: param, diff --git a/go.mod b/go.mod index f573d2f..dd1851c 100644 --- a/go.mod +++ b/go.mod @@ -5,14 +5,14 @@ go 1.22 require ( github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 github.com/bradleyjkemp/cupaloy/v2 v2.8.0 - github.com/getkin/kin-openapi v0.123.0 + github.com/getkin/kin-openapi v0.124.0 github.com/hedhyw/rex v0.6.0 github.com/jarcoal/httpmock v1.3.1 github.com/knadh/koanf/maps v0.1.1 github.com/knadh/koanf/parsers/yaml v0.1.0 github.com/knadh/koanf/providers/confmap v0.1.0 github.com/knadh/koanf/providers/file v0.1.0 - github.com/knadh/koanf/v2 v2.1.0 + github.com/knadh/koanf/v2 v2.1.1 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.9.0 diff --git a/go.sum b/go.sum index f0a8e74..bfa4e26 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= -github.com/getkin/kin-openapi v0.123.0 h1:zIik0mRwFNLyvtXK274Q6ut+dPh6nlxBp0x7mNrPhs8= -github.com/getkin/kin-openapi v0.123.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM= +github.com/getkin/kin-openapi v0.124.0 h1:VSFNMB9C9rTKBnQ/fpyDU8ytMTr4dWI9QovSKj9kz/M= +github.com/getkin/kin-openapi v0.124.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM= github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q= github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs= github.com/go-openapi/swag v0.22.8 h1:/9RjDSQ0vbFR+NyjGMkFTsA1IA0fmhKSThmfGZjicbw= @@ -38,8 +38,8 @@ github.com/knadh/koanf/providers/confmap v0.1.0 h1:gOkxhHkemwG4LezxxN8DMOFopOPgh github.com/knadh/koanf/providers/confmap v0.1.0/go.mod h1:2uLhxQzJnyHKfxG927awZC7+fyHFdQkd697K4MdLnIU= github.com/knadh/koanf/providers/file v0.1.0 h1:fs6U7nrV58d3CFAFh8VTde8TM262ObYf3ODrc//Lp+c= github.com/knadh/koanf/providers/file v0.1.0/go.mod h1:rjJ/nHQl64iYCtAW2QQnF0eSmDEX/YZ/eNFj5yR6BvA= -github.com/knadh/koanf/v2 v2.1.0 h1:eh4QmHHBuU8BybfIJ8mB8K8gsGCD/AUQTdwGq/GzId8= -github.com/knadh/koanf/v2 v2.1.0/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es= +github.com/knadh/koanf/v2 v2.1.1 h1:/R8eXqasSTsmDCsAyYj+81Wteg8AqrV9CP6gvsTsOmM= +github.com/knadh/koanf/v2 v2.1.1/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -73,8 +73,6 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=