Skip to content

Commit

Permalink
Added various tuneups based on just using vacuum.
Browse files Browse the repository at this point in the history
It’s so nice to be able to use this tool in an IDE.

Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Feb 16, 2024
1 parent 49b6593 commit a9d5a5a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 37 deletions.
52 changes: 20 additions & 32 deletions functions/openapi/component_descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/daveshanley/vacuum/model"
vacuumUtils "github.com/daveshanley/vacuum/utils"
"github.com/pb33f/doctor/model/high/base"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
"strconv"
Expand Down Expand Up @@ -74,11 +75,12 @@ func (cd ComponentDescription) RunRule(_ []*yaml.Node, context model.RuleFunctio
schemaValue := schemaPairs.Value()
key := schemaPairs.Key()

k, _ := low.FindItemInOrderedMapWithKey(key, components.Value.GoLow().Schemas.Value)
checkDescription(schemaValue.Schema.Value.Description,
key,
"schemas",
schemaValue.GenerateJSONPath(),
schemaValue.Value.GetSchemaKeyNode(),
k.GetKeyNode(),
schemaValue)
}
}
Expand All @@ -87,12 +89,12 @@ func (cd ComponentDescription) RunRule(_ []*yaml.Node, context model.RuleFunctio
for paramPairs := components.Parameters.First(); paramPairs != nil; paramPairs = paramPairs.Next() {
paramValue := paramPairs.Value()
key := paramPairs.Key()

k, _ := low.FindItemInOrderedMapWithKey(key, components.Value.GoLow().Parameters.Value)
checkDescription(paramValue.Value.Description,
key,
"parameters",
paramValue.GenerateJSONPath(),
paramValue.Value.GoLow().RootNode,
k.GetKeyNode(),
paramValue)

}
Expand All @@ -102,12 +104,12 @@ func (cd ComponentDescription) RunRule(_ []*yaml.Node, context model.RuleFunctio
for requestBodyPairs := components.RequestBodies.First(); requestBodyPairs != nil; requestBodyPairs = requestBodyPairs.Next() {
rbValue := requestBodyPairs.Value()
key := requestBodyPairs.Key()

k, _ := low.FindItemInOrderedMapWithKey(key, components.Value.GoLow().RequestBodies.Value)
checkDescription(rbValue.Value.Description,
key,
"requestBodies",
rbValue.GenerateJSONPath(),
rbValue.Value.GoLow().RootNode,
k.GetKeyNode(),
rbValue)
}
}
Expand All @@ -116,12 +118,12 @@ func (cd ComponentDescription) RunRule(_ []*yaml.Node, context model.RuleFunctio
for responsePairs := components.Responses.First(); responsePairs != nil; responsePairs = responsePairs.Next() {
rValue := responsePairs.Value()
key := responsePairs.Key()

k, _ := low.FindItemInOrderedMapWithKey(key, components.Value.GoLow().Responses.Value)
checkDescription(rValue.Value.Description,
key,
"responses",
rValue.GenerateJSONPath(),
rValue.Value.GoLow().RootNode,
k.GetKeyNode(),
rValue)
}
}
Expand All @@ -130,69 +132,55 @@ func (cd ComponentDescription) RunRule(_ []*yaml.Node, context model.RuleFunctio
for examplePairs := components.Examples.First(); examplePairs != nil; examplePairs = examplePairs.Next() {
exampleValue := examplePairs.Value()
key := examplePairs.Key()

k, _ := low.FindItemInOrderedMapWithKey(key, components.Value.GoLow().Examples.Value)
checkDescription(exampleValue.Value.Description,
key,
"examples",
exampleValue.GenerateJSONPath(),
exampleValue.Value.GoLow().RootNode,
k.GetKeyNode(),
exampleValue)
}
}

if components != nil && components.Callbacks != nil {
for callbackPairs := components.Examples.First(); callbackPairs != nil; callbackPairs = callbackPairs.Next() {
callbackValue := callbackPairs.Value()
key := callbackPairs.Key()

checkDescription(callbackValue.Value.Description,
key,
"callbacks",
callbackValue.GenerateJSONPath(),
callbackValue.Value.GoLow().RootNode,
callbackValue)
}
}

if components != nil && components.Headers != nil {
for headerPair := components.Examples.First(); headerPair != nil; headerPair = headerPair.Next() {
for headerPair := components.Headers.First(); headerPair != nil; headerPair = headerPair.Next() {
headerValue := headerPair.Value()
key := headerPair.Key()

k, _ := low.FindItemInOrderedMapWithKey(key, components.Value.GoLow().Headers.Value)
checkDescription(headerValue.Value.Description,
key,
"headers",
headerValue.GenerateJSONPath(),
headerValue.Value.GoLow().RootNode,
k.GetKeyNode(),
headerValue)
}
}

if components != nil && components.Links != nil {
for linkPair := components.Examples.First(); linkPair != nil; linkPair = linkPair.Next() {
for linkPair := components.Links.First(); linkPair != nil; linkPair = linkPair.Next() {
linkValue := linkPair.Value()
key := linkPair.Key()

k, _ := low.FindItemInOrderedMapWithKey(key, components.Value.GoLow().Links.Value)
checkDescription(linkValue.Value.Description,
key,
"links",
linkValue.GenerateJSONPath(),
linkValue.Value.GoLow().RootNode,
k.GetKeyNode(),
linkValue)

}
}

if components != nil && components.SecuritySchemes != nil {
for securitySchemePair := components.Examples.First(); securitySchemePair != nil; securitySchemePair = securitySchemePair.Next() {
for securitySchemePair := components.SecuritySchemes.First(); securitySchemePair != nil; securitySchemePair = securitySchemePair.Next() {
ssValue := securitySchemePair.Value()
key := securitySchemePair.Key()

k, _ := low.FindItemInOrderedMapWithKey(key, components.Value.GoLow().SecuritySchemes.Value)
checkDescription(ssValue.Value.Description,
key,
"securitySchemes",
ssValue.GenerateJSONPath(),
ssValue.Value.GoLow().RootNode,
k.GetKeyNode(),
ssValue)

}
Expand Down
28 changes: 27 additions & 1 deletion functions/openapi/examples_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/pb33f/doctor/model/high/base"
"github.com/pb33f/libopenapi-validator/schema_validation"
v3Base "github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils"
"github.com/sourcegraph/conc"
"gopkg.in/yaml.v3"
"strings"
Expand Down Expand Up @@ -101,6 +103,18 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, context model.RuleFunctionConte
if s.Value.Examples != nil {
for x, ex := range s.Value.Examples {

isRef, _, _ := utils.IsNodeRefValue(ex)
if isRef {
// extract node
fNode, _, _, _ := low.LocateRefNodeWithContext(s.Value.ParentProxy.GoLow().GetContext(),
ex, context.Index)
if fNode != nil {
ex = fNode
} else {
continue
}
}

var example map[string]interface{}
_ = ex.Decode(&example)

Expand All @@ -115,8 +129,20 @@ func (es ExamplesSchema) RunRule(_ []*yaml.Node, context model.RuleFunctionConte
}

if s.Value.Example != nil {

isRef, _, _ := utils.IsNodeRefValue(s.Value.Example)
ref := s.Value.Example
if isRef {
// extract node
fNode, _, _, _ := low.LocateRefNodeWithContext(s.Value.ParentProxy.GoLow().GetContext(),
s.Value.Example, context.Index)
if fNode != nil {
ref = fNode
}
}

var example interface{}
_ = s.Value.Example.Decode(&example)
_ = ref.Decode(&example)

result := validateSchema(nil, "", "example", s, s, s.Value.Example,
s.Value.GoLow().Example.GetKeyNode(), example)
Expand Down
8 changes: 4 additions & 4 deletions functions/owasp/integer_limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ components:
res := def.RunRule(nil, ctx)

assert.Len(t, res, 1)
assert.Equal(t, "schema of type `string` must specify `minimum` and `maximum` or `exclusiveMinimum` "+
assert.Equal(t, "schema of type `integer` must specify `minimum` and `maximum` or `exclusiveMinimum` "+
"and `exclusiveMaximum`", res[0].Message)
assert.Equal(t, "$.components.schemas['thing']", res[0].Path)
}
Expand Down Expand Up @@ -71,7 +71,7 @@ components:
res := def.RunRule(nil, ctx)

assert.Len(t, res, 1)
assert.Equal(t, "schema of type `string` must specify `minimum` and `maximum` or `exclusiveMinimum` "+
assert.Equal(t, "schema of type `integer` must specify `minimum` and `maximum` or `exclusiveMinimum` "+
"and `exclusiveMaximum`", res[0].Message)
assert.Equal(t, "$.components.schemas['thing']", res[0].Path)
}
Expand Down Expand Up @@ -132,7 +132,7 @@ components:
res := def.RunRule(nil, ctx)

assert.Len(t, res, 1)
assert.Equal(t, "schema of type `string` must specify `minimum` and `maximum` or `exclusiveMinimum` "+
assert.Equal(t, "schema of type `integer` must specify `minimum` and `maximum` or `exclusiveMinimum` "+
"and `exclusiveMaximum`", res[0].Message)
assert.Equal(t, "$.components.schemas['thing']", res[0].Path)
}
Expand Down Expand Up @@ -165,7 +165,7 @@ components:
res := def.RunRule(nil, ctx)

assert.Len(t, res, 1)
assert.Equal(t, "schema of type `string` must specify `minimum` and `maximum` or `exclusiveMinimum` "+
assert.Equal(t, "schema of type `integer` must specify `minimum` and `maximum` or `exclusiveMinimum` "+
"and `exclusiveMaximum`", res[0].Message)
assert.Equal(t, "$.components.schemas['thing']", res[0].Path)
}
Expand Down

0 comments on commit a9d5a5a

Please sign in to comment.