diff --git a/go.mod b/go.mod index 151be0f18d..95d6630ace 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/deepmap/oapi-codegen require ( github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c github.com/getkin/kin-openapi v0.61.0 - github.com/gin-gonic/gin v1.7.2 github.com/go-chi/chi/v5 v5.0.0 github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219 github.com/labstack/echo/v4 v4.2.1 @@ -11,7 +10,7 @@ require ( github.com/mattn/go-colorable v0.1.8 // indirect github.com/mattn/go-isatty v0.0.13 // indirect github.com/pkg/errors v0.8.1 - github.com/stretchr/testify v1.5.1 + github.com/stretchr/testify v1.8.0 golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect diff --git a/go.sum b/go.sum index 4019452789..7e724721f4 100644 --- a/go.sum +++ b/go.sum @@ -45,13 +45,15 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +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/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= @@ -99,5 +101,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/codegen/codegen.go b/pkg/codegen/codegen.go index f994084c09..13d026015a 100644 --- a/pkg/codegen/codegen.go +++ b/pkg/codegen/codegen.go @@ -282,7 +282,7 @@ func Generate(swagger *openapi3.T, packageName string, opts Options) (string, er fmt.Println(goCode) return "", errors.Wrap(err, "error formatting Go code") } - return string(outBytes), nil + return strings.TrimSuffix(string(outBytes), "\n"), nil } func GenerateTypeDefinitions(t *template.Template, swagger *openapi3.T, ops []OperationDefinition, excludeSchemas []string) (string, error) { @@ -575,8 +575,8 @@ func GenerateImports(t *template.Template, externalImports []string, packageName var modulePath string var moduleVersion string if bi, ok := debug.ReadBuildInfo(); ok { - modulePath = bi.Main.Path - moduleVersion = bi.Main.Version + modulePath = "github.com/opalsecurity/oapi-codegen" + moduleVersion = bi.GoVersion } else { // Unit tests have ok=false, so we'll just use "unknown" for the // version if we can't read this. diff --git a/pkg/codegen/operations.go b/pkg/codegen/operations.go index 96345e9383..2d5c2c50a6 100644 --- a/pkg/codegen/operations.go +++ b/pkg/codegen/operations.go @@ -402,7 +402,6 @@ func OperationDefinitions(swagger *openapi3.T) ([]OperationDefinition, error) { return nil, fmt.Errorf("error generating default OperationID for %s/%s: %s", opName, requestPath, err) } - op.OperationID = op.OperationID } else { op.OperationID = ToCamelCase(op.OperationID) } diff --git a/pkg/codegen/templates/templates.gen.go b/pkg/codegen/templates/templates.gen.go index 2e68514627..2191362cad 100644 --- a/pkg/codegen/templates/templates.gen.go +++ b/pkg/codegen/templates/templates.gen.go @@ -808,7 +808,7 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { } {{end}} {{if .IsStyled}} - err = runtime.BindStyledParameter("{{.Style}}",{{.Explode}}, "{{.ParamName}}", c.Query("{{.ParamName}}"), &{{$varName}}) + err = runtime.BindStyledParameter("{{.Style}}",{{.Explode}}, "{{.ParamName}}", c.Param("{{.ParamName}}"), &{{$varName}}) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)}) return @@ -944,10 +944,26 @@ func (siw *ServerInterfaceWrapper) {{$opid}}(c *gin.Context) { {{end}} {{end}} + // ------------- Reject unknown query parameters ------------- + acceptableQueryParams := map[string]bool { + {{range $paramIdx, $param := .QueryParams}}"{{.ParamName}}": true, + {{end}} + } + for queryParam := range c.Request.URL.Query() { + if !acceptableQueryParams[queryParam] { + c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid query parameter: %s", queryParam)}) + return + } + } + for _, middleware := range siw.HandlerMiddlewares { middleware(c) } + if c.IsAborted() { + return + } + siw.Handler.{{.OperationId}}(c{{genParamNames .PathParams}}{{if .RequiresParamObject}}, params{{end}}) } {{end}} @@ -1288,4 +1304,3 @@ func Parse(t *template.Template) (*template.Template, error) { } return t, nil } -