Skip to content
Open
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
12 changes: 5 additions & 7 deletions openapi3/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/url"
"strings"

"github.com/go-openapi/jsonpointer"
)
Expand Down Expand Up @@ -215,13 +216,10 @@ func (doc *T) Validate(ctx context.Context, opts ...ValidationOption) error {
return wrap(err)
}
} else {
return wrap(errors.New("must be an object"))
}

wrap = func(e error) error { return fmt.Errorf("invalid security: %w", e) }
if v := doc.Security; v != nil {
if err := v.Validate(ctx); err != nil {
return wrap(err)
if strings.HasPrefix(doc.OpenAPI, "3.1") && (doc.Components != nil || len(doc.Webhooks) > 0) {
// paths is optional in OpenAPI 3.1 if components or webhooks is present
} else {
return wrap(errors.New("must be an object"))
}
}

Expand Down
8 changes: 8 additions & 0 deletions openapi3/openapi3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,14 @@ components:
spec: strings.Replace(spec, paths, ``, 1),
expectedErr: "invalid paths: must be an object",
},
{
name: "OAS 3.1 missing paths but has components",
spec: strings.Replace(strings.Replace(spec, version, "\nopenapi: 3.1.0\n", 1), paths, "", 1),
},
{
name: "OAS 3.1 missing paths but has webhooks",
spec: strings.Replace(strings.Replace(spec, version, "\nopenapi: 3.1.0\n", 1), paths, "webhooks:\n myWebhook:\n post:\n responses:\n '200':\n description: OK\n", 1),
},
{
name: "externalDocs section is invalid",
spec: strings.Replace(spec, externalDocs,
Expand Down
Loading