Skip to content

Commit

Permalink
Added safety check to prevent NPE
Browse files Browse the repository at this point in the history
Signed-off-by: quobix <[email protected]>
  • Loading branch information
daveshanley committed Feb 9, 2024
1 parent 6ba5579 commit 889f7d5
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions functions/openapi/examples_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,42 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, context model.RuleFunctionConte
example any) []model.RuleFunctionResult {

var rx []model.RuleFunctionResult
valid, validationErrors := validator.ValidateSchemaObject(s.Value, example)
if !valid {
var path string
if iKey == nil && sKey == "" {
path = fmt.Sprintf("%s.%s", obj.(base.Foundational).GenerateJSONPath(), label)
}
if iKey != nil && sKey == "" {
path = fmt.Sprintf("%s.%s[%d]", obj.(base.Foundational).GenerateJSONPath(), label, *iKey)
}
if iKey == nil && sKey != "" {
path = fmt.Sprintf("%s.%s['%s']", obj.(base.Foundational).GenerateJSONPath(), label, sKey)
}
for _, r := range validationErrors {
for _, err := range r.SchemaValidationErrors {
result := buildResult(vacuumUtils.SuppliedOrDefault(context.Rule.Message, err.Reason),
path, node, obj)

banned := false
for g := range bannedErrors {
if strings.Contains(err.Reason, bannedErrors[g]) {
banned = true
continue
if s != nil && s.Value != nil {
valid, validationErrors := validator.ValidateSchemaObject(s.Value, example)
if !valid {
var path string
if iKey == nil && sKey == "" {
path = fmt.Sprintf("%s.%s", obj.(base.Foundational).GenerateJSONPath(), label)
}
if iKey != nil && sKey == "" {
path = fmt.Sprintf("%s.%s[%d]", obj.(base.Foundational).GenerateJSONPath(), label, *iKey)
}
if iKey == nil && sKey != "" {
path = fmt.Sprintf("%s.%s['%s']", obj.(base.Foundational).GenerateJSONPath(), label, sKey)
}
for _, r := range validationErrors {
for _, err := range r.SchemaValidationErrors {
result := buildResult(vacuumUtils.SuppliedOrDefault(context.Rule.Message, err.Reason),
path, node, obj)

banned := false
for g := range bannedErrors {
if strings.Contains(err.Reason, bannedErrors[g]) {
banned = true
continue
}
}
if !banned {
rx = append(rx, result)
}
}
if !banned {
rx = append(rx, result)
}
}
}
}
return rx
}

if context.DrDocument.Schemas != nil {
if context.DrDocument != nil && context.DrDocument.Schemas != nil {
for i := range context.DrDocument.Schemas {
s := context.DrDocument.Schemas[i]
wg.Go(func() {
Expand Down Expand Up @@ -159,7 +161,7 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, context model.RuleFunctionConte
return rx
}

if context.DrDocument.Parameters != nil {
if context.DrDocument != nil && context.DrDocument.Parameters != nil {
for i := range context.DrDocument.Parameters {
p := context.DrDocument.Parameters[i]
wg.Go(func() {
Expand All @@ -174,7 +176,7 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, context model.RuleFunctionConte
}
}

if context.DrDocument.Headers != nil {
if context.DrDocument != nil && context.DrDocument.Headers != nil {
for i := range context.DrDocument.Headers {
h := context.DrDocument.Headers[i]
wg.Go(func() {
Expand All @@ -189,7 +191,7 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, context model.RuleFunctionConte
}
}

if context.DrDocument.MediaTypes != nil {
if context.DrDocument != nil && context.DrDocument.MediaTypes != nil {
for i := range context.DrDocument.MediaTypes {
mt := context.DrDocument.MediaTypes[i]
wg.Go(func() {
Expand Down

0 comments on commit 889f7d5

Please sign in to comment.