Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to libopenapi #1211

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions cmd/oapi-codegen/oapi-codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,14 @@

// Ensure default values are set if user hasn't specified some needed
// fields.
opts.Configuration = opts.UpdateDefaults()

Check failure on line 240 in cmd/oapi-codegen/oapi-codegen.go

View workflow job for this annotation

GitHub Actions / Build

opts.UpdateDefaults undefined (type configuration has no field or method UpdateDefaults) (typecheck)

if err := detectPackageName(&opts); err != nil {
errExit("%s\n", err)
}

// Now, ensure that the config options are valid.
if err := opts.Validate(); err != nil {

Check failure on line 247 in cmd/oapi-codegen/oapi-codegen.go

View workflow job for this annotation

GitHub Actions / Build

opts.Validate undefined (type configuration has no field or method Validate) (typecheck)
errExit("configuration error: %v\n", err)
}

Expand All @@ -258,12 +258,12 @@
return
}

swagger, err := util.LoadSwagger(flag.Arg(0))
openapi, err := util.ParseOpenAPI(flag.Arg(0))
if err != nil {
errExit("error loading swagger spec in %s\n: %s", flag.Arg(0), err)
}

code, err := codegen.Generate(swagger, opts.Configuration)
code, err := codegen.Generate(openapi, opts.Configuration)
if err != nil {
errExit("error generating code: %s\n", err)
}
Expand Down Expand Up @@ -316,13 +316,13 @@

// detectPackageName detects and sets PackageName if not already set.
func detectPackageName(cfg *configuration) error {
if cfg.PackageName != "" {

Check failure on line 319 in cmd/oapi-codegen/oapi-codegen.go

View workflow job for this annotation

GitHub Actions / Build

cfg.PackageName undefined (type *configuration has no field or method PackageName) (typecheck)
return nil
}

if cfg.OutputFile != "" {
// Determine from the package name of the output file.
dir := filepath.Dir(cfg.PackageName)

Check failure on line 325 in cmd/oapi-codegen/oapi-codegen.go

View workflow job for this annotation

GitHub Actions / Build

cfg.PackageName undefined (type *configuration has no field or method PackageName) (typecheck)
cmd := exec.Command("go", "list", "-f", "{{.Name}}", dir)
out, err := cmd.CombinedOutput()
if err != nil {
Expand All @@ -338,7 +338,7 @@
return fmt.Errorf("detect package name for %q output: %q: %w", dir, string(out), err)
}
} else {
cfg.PackageName = string(out)

Check failure on line 341 in cmd/oapi-codegen/oapi-codegen.go

View workflow job for this annotation

GitHub Actions / Build

cfg.PackageName undefined (type *configuration has no field or method PackageName) (typecheck)
return nil
}
}
Expand All @@ -365,21 +365,21 @@
}
}
if flagIncludeTags != "" {
cfg.OutputOptions.IncludeTags = util.ParseCommandLineList(flagIncludeTags)

Check failure on line 368 in cmd/oapi-codegen/oapi-codegen.go

View workflow job for this annotation

GitHub Actions / Build

cfg.OutputOptions undefined (type *configuration has no field or method OutputOptions) (typecheck)
}
if flagExcludeTags != "" {
cfg.OutputOptions.ExcludeTags = util.ParseCommandLineList(flagExcludeTags)

Check failure on line 371 in cmd/oapi-codegen/oapi-codegen.go

View workflow job for this annotation

GitHub Actions / Build

cfg.OutputOptions undefined (type *configuration has no field or method OutputOptions) (typecheck)
}
if flagTemplatesDir != "" {
templates, err := loadTemplateOverrides(flagTemplatesDir)
if err != nil {
return fmt.Errorf("load templates from %q: %w", flagTemplatesDir, err)
}
cfg.OutputOptions.UserTemplates = templates

Check failure on line 378 in cmd/oapi-codegen/oapi-codegen.go

View workflow job for this annotation

GitHub Actions / Build

cfg.OutputOptions undefined (type *configuration has no field or method OutputOptions) (typecheck)
}
if flagImportMapping != "" {
var err error
cfg.ImportMapping, err = util.ParseCommandlineMap(flagImportMapping)

Check failure on line 382 in cmd/oapi-codegen/oapi-codegen.go

View workflow job for this annotation

GitHub Actions / Build

cfg.ImportMapping undefined (type *configuration has no field or method ImportMapping) (typecheck)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/labstack/echo/v4 v4.10.2
github.com/lestrrat-go/jwx v1.2.25
github.com/matryer/moq v0.3.1
github.com/pb33f/libopenapi v0.8.5
github.com/stretchr/testify v1.8.3
golang.org/x/text v0.9.0
golang.org/x/tools v0.9.2
Expand All @@ -22,6 +23,7 @@ require (
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
Expand Down Expand Up @@ -56,10 +58,12 @@ require (
github.com/ugorji/go/codec v1.2.9 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
Expand Down
97 changes: 97 additions & 0 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/chi-middleware/oapi_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@

// OapiRequestValidator Creates middleware to validate request by swagger spec.
// This middleware is good for net/http either since go-chi is 100% compatible with net/http.
func OapiRequestValidator(swagger *openapi3.T) func(next http.Handler) http.Handler {
func OapiRequestValidator(swagger *libopenapi.DocumentModel[v3.Document]) func(next http.Handler) http.Handler {

Check failure on line 37 in pkg/chi-middleware/oapi_validate.go

View workflow job for this annotation

GitHub Actions / Build

undeclared name: `libopenapi` (typecheck)

Check failure on line 37 in pkg/chi-middleware/oapi_validate.go

View workflow job for this annotation

GitHub Actions / Build

undefined: libopenapi
return OapiRequestValidatorWithOptions(swagger, nil)
}

// OapiRequestValidatorWithOptions Creates middleware to validate request by swagger spec.
// This middleware is good for net/http either since go-chi is 100% compatible with net/http.
func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) func(next http.Handler) http.Handler {
func OapiRequestValidatorWithOptions(swagger *libopenapi.DocumentModel[v3.Document], options *Options) func(next http.Handler) http.Handler {

Check failure on line 43 in pkg/chi-middleware/oapi_validate.go

View workflow job for this annotation

GitHub Actions / Build

undefined: libopenapi
if swagger.Servers != nil && (options == nil || options.SilenceServersWarning) {
log.Println("WARN: OapiRequestValidatorWithOptions called with an OpenAPI spec that has `Servers` set. This may lead to an HTTP 400 with `no matching operation was found` when sending a valid request, as the validator performs `Host` header validation. If you're expecting `Host` header validation, you can silence this warning by setting `Options.SilenceServersWarning = true`. See https://github.com/deepmap/oapi-codegen/issues/882 for more information.")
}
Expand Down