diff --git a/README.md b/README.md index d272d0d64..4566c04f5 100644 --- a/README.md +++ b/README.md @@ -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] -- +go run github.com/getkin/kin-openapi/cmd/validate@latest [--circular] [--defaults] [--examples] [--ext] [--patterns] -- ``` ## Loading OpenAPI document diff --git a/cmd/validate/main.go b/cmd/validate/main.go index d8c0fe6ad..6301348a8 100644 --- a/cmd/validate/main.go +++ b/cmd/validate/main.go @@ -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") @@ -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] -- \nGot: %+v\n", os.Args) + log.Fatalf("Usage: go run github.com/getkin/kin-openapi/cmd/validate@latest [--circular] [--defaults] [--examples] [--ext] [--patterns] -- \nGot: %+v\n", os.Args) } data, err := os.ReadFile(filename) @@ -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 @@ -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") }