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

cmd/validate: support setting openapi3.CircularReferenceCounter #815

Merged
merged 2 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Be sure to check [OpenAPI Initiative](https://github.com/OAI)'s [great tooling l
# Some recipes
## Validating an OpenAPI document
```shell
go run github.com/getkin/kin-openapi/cmd/validate@latest [--defaults] [--examples] [--ext] [--patterns] -- <local YAML or JSON file>
go run github.com/getkin/kin-openapi/cmd/validate@latest [--circular] [--defaults] [--examples] [--ext] [--patterns] -- <local YAML or JSON file>
```

## Loading OpenAPI document
Expand Down
11 changes: 10 additions & 1 deletion cmd/validate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
"github.com/getkin/kin-openapi/openapi3"
)

var (
defaultCircular = openapi3.CircularReferenceCounter
circular = flag.Int("circular", defaultCircular, "bump this (upper) limit when there's trouble with cyclic schema references")
)

var (
defaultDefaults = true
defaults = flag.Bool("defaults", defaultDefaults, "when false, disables schemas' default field validation")
Expand All @@ -36,7 +41,7 @@ func main() {
flag.Parse()
filename := flag.Arg(0)
if len(flag.Args()) != 1 || filename == "" {
log.Fatalf("Usage: go run github.com/getkin/kin-openapi/cmd/validate@latest [--defaults] [--examples] [--ext] [--patterns] -- <local YAML or JSON file>\nGot: %+v\n", os.Args)
log.Fatalf("Usage: go run github.com/getkin/kin-openapi/cmd/validate@latest [--circular] [--defaults] [--examples] [--ext] [--patterns] -- <local YAML or JSON file>\nGot: %+v\n", os.Args)
}

data, err := os.ReadFile(filename)
Expand All @@ -54,6 +59,7 @@ func main() {

switch {
case vd.OpenAPI == "3" || strings.HasPrefix(vd.OpenAPI, "3."):
openapi3.CircularReferenceCounter = *circular
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = *ext

Expand All @@ -78,6 +84,9 @@ func main() {
}

case vd.Swagger == "2" || strings.HasPrefix(vd.Swagger, "2."):
if *circular != defaultCircular {
log.Fatal("Flag --circular is only for OpenAPIv3")
}
if *defaults != defaultDefaults {
log.Fatal("Flag --defaults is only for OpenAPIv3")
}
Expand Down