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

Add security requirement to root OpenAPI V3 #432

Merged
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
12 changes: 12 additions & 0 deletions pkg/spec3/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ var OpenAPIV3FuzzFuncs []interface{} = []interface{}{
func(o *OpenAPI, c fuzz.Continue) {
c.FuzzNoCustom(o)
o.Version = "3.0.0"
for i, val := range o.SecurityRequirement {
if val == nil {
o.SecurityRequirement[i] = make(map[string][]string)
}

for k, v := range val {
if v == nil {
val[k] = make([]string, 0)
}
}
}

},
func(r *interface{}, c fuzz.Continue) {
switch c.Intn(3) {
Expand Down
15 changes: 9 additions & 6 deletions pkg/spec3/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type OpenAPI struct {
Servers []*Server `json:"servers,omitempty"`
// Components hold various schemas for the specification
Components *Components `json:"components,omitempty"`
// SecurityRequirement holds a declaration of which security mechanisms can be used across the API
SecurityRequirement []map[string][]string `json:"security,omitempty"`
// ExternalDocs holds additional external documentation
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
}
Expand All @@ -60,12 +62,13 @@ func (o *OpenAPI) MarshalJSON() ([]byte, error) {

func (o *OpenAPI) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error {
type OpenAPIOmitZero struct {
Version string `json:"openapi"`
Info *spec.Info `json:"info"`
Paths *Paths `json:"paths,omitzero"`
Servers []*Server `json:"servers,omitempty"`
Components *Components `json:"components,omitzero"`
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitzero"`
Version string `json:"openapi"`
Info *spec.Info `json:"info"`
Paths *Paths `json:"paths,omitzero"`
Servers []*Server `json:"servers,omitempty"`
Components *Components `json:"components,omitzero"`
SecurityRequirement []map[string][]string `json:"security,omitempty"`
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitzero"`
}
x := (*OpenAPIOmitZero)(o)
return opts.MarshalNext(enc, x)
Expand Down