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

v0.4.3 Fixes a small regression when using vacuum as an API #360

Merged
merged 6 commits into from
Nov 5, 2023
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
9 changes: 6 additions & 3 deletions functions/core/undefined.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ func (u Undefined) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext

fieldNode, _ := utils.FindKeyNode(context.RuleAction.Field, node.Content)
if fieldNode != nil {

var val = ""
if context.RuleAction.Field != "" {
val = fmt.Sprintf("'%s' ", context.RuleAction.Field)
}
results = append(results, model.RuleFunctionResult{
Message: fmt.Sprintf("%s: '%s' must be undefined",
context.Rule.Description, context.RuleAction.Field),
Message: fmt.Sprintf("%s %smust be undefined",
context.Rule.Description, val),
StartNode: fieldNode,
EndNode: fieldNode,
Path: pathValue,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/gizak/termui/v3 v3.1.0
github.com/json-iterator/go v1.1.12
github.com/mitchellh/mapstructure v1.5.0
github.com/pb33f/libopenapi v0.13.5
github.com/pb33f/libopenapi v0.13.6
github.com/pb33f/libopenapi-validator v0.0.26
github.com/pterm/pterm v0.12.69
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/libopenapi v0.13.5 h1:jXmEKAAU6hyE9uPOSI43T2KVutMscEL4czoh7q0wcFw=
github.com/pb33f/libopenapi v0.13.5/go.mod h1:Lv2eEtsAtbRFlF8hjH82L8SIGoUNgemMVoKoB6A9THk=
github.com/pb33f/libopenapi v0.13.6 h1:FjPbc2jI+B3Z+vKk1x+8I4laJ4vdlwFWmUxaPwNdV/8=
github.com/pb33f/libopenapi v0.13.6/go.mod h1:Lv2eEtsAtbRFlF8hjH82L8SIGoUNgemMVoKoB6A9THk=
github.com/pb33f/libopenapi-validator v0.0.26 h1:sDJ7xXX2C+p9TVaujcV7HaSVpULlr99dVyhZBbSTwoM=
github.com/pb33f/libopenapi-validator v0.0.26/go.mod h1:QChIoPXJX8FkAQ9ufF7YX3KBaeUXyMucb/uajw0MaBs=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
Expand Down
35 changes: 27 additions & 8 deletions motor/rule_applicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {

// create new configurations
indexConfig := index.CreateClosedAPIIndexConfig()
indexConfigUnresolved := index.CreateClosedAPIIndexConfig()

// avoid building the index, we don't need it to run yet.
indexConfig.AvoidBuildIndex = true

docConfig := datamodel.NewDocumentConfiguration()

if execution.Base != "" {
Expand All @@ -98,40 +98,44 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {
if e == nil && u.Scheme != "" && u.Host != "" {
indexConfig.BaseURL = u
indexConfig.BasePath = ""
indexConfigUnresolved.BaseURL = u
indexConfigUnresolved.BasePath = ""
docConfig.BaseURL = u
docConfig.BasePath = ""
indexConfig.AllowRemoteLookup = true
indexConfigUnresolved.AllowRemoteLookup = true

} else {
indexConfig.AllowFileLookup = true
indexConfig.BasePath = execution.Base
indexConfigUnresolved.AllowFileLookup = true
indexConfigUnresolved.BasePath = execution.Base
docConfig.BasePath = execution.Base
}
}

if execution.AllowLookup {
if indexConfig.BasePath != "" {
indexConfig.AllowFileLookup = true
indexConfigUnresolved.AllowFileLookup = true
}
indexConfig.AllowRemoteLookup = true
indexConfigUnresolved.AllowRemoteLookup = true
docConfig.AllowRemoteReferences = true
//docConfig.AllowFileReferences = true
}

if execution.SkipDocumentCheck {
docConfig.BypassDocumentCheck = true
}

docResolved := execution.Document
docUnresolved := execution.Document
var docUnresolved libopenapi.Document

// If no docResolved is supplied (default) then create a new one.
// otherwise update the configuration with the supplied document.
// and build it.
specInfo := execution.SpecInfo
if execution.Document != nil && execution.Document.GetSpecInfo() != nil {
specInfo = execution.Document.GetSpecInfo()
}

var specInfo, specInfoUnresolved *datamodel.SpecInfo
if docResolved == nil {
var err error
// create a new document.
Expand All @@ -147,16 +151,31 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {
indexConfig.SpecInfo = specInfo

} else {

var uErr error
docUnresolved, uErr = libopenapi.NewDocumentWithConfiguration(*docResolved.GetSpecInfo().SpecBytes, docConfig)
if uErr != nil {
// Done here, we can't do anything else.
return &RuleSetExecutionResult{Errors: []error{uErr}}
}

specInfo = docResolved.GetSpecInfo()
specInfoUnresolved = docUnresolved.GetSpecInfo()

suppliedDocConfig := docResolved.GetConfiguration()
docConfig.BaseURL = suppliedDocConfig.BaseURL
docConfig.BasePath = suppliedDocConfig.BasePath
docConfig.IgnorePolymorphicCircularReferences = suppliedDocConfig.IgnorePolymorphicCircularReferences
docConfig.IgnoreArrayCircularReferences = suppliedDocConfig.IgnoreArrayCircularReferences
docConfig.AvoidIndexBuild = suppliedDocConfig.AvoidIndexBuild
indexConfig.SpecInfo = specInfo
indexConfig.AvoidBuildIndex = suppliedDocConfig.AvoidIndexBuild
indexConfig.IgnorePolymorphicCircularReferences = suppliedDocConfig.IgnorePolymorphicCircularReferences
indexConfig.IgnoreArrayCircularReferences = suppliedDocConfig.IgnoreArrayCircularReferences
indexConfig.SpecInfo = specInfo
indexConfigUnresolved.SpecInfo = specInfoUnresolved
indexConfigUnresolved.AvoidBuildIndex = suppliedDocConfig.AvoidIndexBuild
indexConfigUnresolved.IgnorePolymorphicCircularReferences = suppliedDocConfig.IgnorePolymorphicCircularReferences
indexConfigUnresolved.IgnoreArrayCircularReferences = suppliedDocConfig.IgnoreArrayCircularReferences
}

// build model
Expand Down
148 changes: 148 additions & 0 deletions motor/rule_applicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,154 @@ components:
assert.Equal(t, "resolving-references", results.Results[0].RuleId)
}

type testRuleNotResolved struct{}

func (r *testRuleNotResolved) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) []model.RuleFunctionResult {
paths := context.Index.GetAllPaths()
oneRef := paths["/one"]["get"].Node.Content[1].Content[1].Content[1].Content[1].Content[1].Content[0].Value
one := paths["/one"]["get"].Node.Content[1].Content[1].Content[1].Content[1].Content[1].Content[1].Value

if oneRef != "$ref" && one != "#/components/schemas/one" {
return []model.RuleFunctionResult{
{
Message: "the reference was resolved when it should not be.",
},
}
} else {
return []model.RuleFunctionResult{}
}
}
func (r *testRuleNotResolved) GetSchema() model.RuleFunctionSchema {
return model.RuleFunctionSchema{
Name: "notResolved",
}
}

type testRuleResolved struct{}

func (r *testRuleResolved) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) []model.RuleFunctionResult {
paths := context.Index.GetAllPaths()
oneRef := paths["/one"]["get"].Node.Content[1].Content[1].Content[1].Content[1].Content[1].Content[0].Value
one := paths["/one"]["get"].Node.Content[1].Content[1].Content[1].Content[1].Content[1].Content[1].Value

if oneRef == "$ref" && one == "#/components/schemas/one" {
return []model.RuleFunctionResult{
{
Message: "the reference was not resolved when it should not be.",
},
}
} else {
return []model.RuleFunctionResult{}
}
}
func (r *testRuleResolved) GetSchema() model.RuleFunctionSchema {
return model.RuleFunctionSchema{
Name: "resolved",
}
}

func TestRuleSet_TestDocumentNotResolved(t *testing.T) {

yml := `openapi: 3.1.0
paths:
/one:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/one'
components:
schemas:
one:
type: string`

config := datamodel.NewDocumentConfiguration()

doc, err := libopenapi.NewDocumentWithConfiguration([]byte(yml), config)
if err != nil {
panic(err)
}

ex := &RuleSetExecution{
RuleSet: &rulesets.RuleSet{
Rules: map[string]*model.Rule{
"test": {
Id: "test",
Resolved: false,
Given: "$",
RuleCategory: model.RuleCategories[model.CategoryValidation],
Type: rulesets.Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "notResolved",
},
},
},
},
Document: doc,
CustomFunctions: map[string]model.RuleFunction{
"notResolved": &testRuleNotResolved{},
},
}

results := ApplyRulesToRuleSet(ex)
assert.Len(t, results.Errors, 0)
assert.Nil(t, results.Results)
}

func TestRuleSet_TestDocumentResolved(t *testing.T) {

yml := `openapi: 3.1.0
paths:
/one:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/one'
components:
schemas:
one:
type: string`

config := datamodel.NewDocumentConfiguration()

doc, err := libopenapi.NewDocumentWithConfiguration([]byte(yml), config)
if err != nil {
panic(err)
}

ex := &RuleSetExecution{
RuleSet: &rulesets.RuleSet{
Rules: map[string]*model.Rule{
"test": {
Id: "test",
Resolved: true,
Given: "$",
RuleCategory: model.RuleCategories[model.CategoryValidation],
Type: rulesets.Validation,
Severity: model.SeverityError,
Then: model.RuleAction{
Function: "resolved",
},
},
},
},
Document: doc,
CustomFunctions: map[string]model.RuleFunction{
"resolved": &testRuleResolved{},
},
}

results := ApplyRulesToRuleSet(ex)
assert.Len(t, results.Errors, 0)
assert.Nil(t, results.Results)
}

func TestRuleSet_InfiniteCircularLoop(t *testing.T) {

yml := `openapi: 3.1.0
Expand Down
Loading