From cb60f8e2570db3ede63407dc47cdf95cce6d26de Mon Sep 17 00:00:00 2001 From: Calvin Lobo Date: Fri, 16 Aug 2024 12:51:38 -0400 Subject: [PATCH] - Renamed RuleSetExecution.SpecFileNam to SpecFilePath because it's actually set to the path, not just the filename - Recursively build reference maps in unused-components check --- cmd/lint.go | 2 +- functions/openapi/unused_component.go | 16 +++++++++++++--- motor/rule_applicator.go | 9 ++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/cmd/lint.go b/cmd/lint.go index d9960475..554138e5 100644 --- a/cmd/lint.go +++ b/cmd/lint.go @@ -335,7 +335,7 @@ func lintFile(req utils.LintFileRequest) (int64, int, error) { result := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{ RuleSet: req.SelectedRS, Spec: specBytes, - SpecFileName: req.FileName, + SpecFilePath: req.FileName, CustomFunctions: req.Functions, Base: req.BaseFlag, AllowLookup: req.Remote, diff --git a/functions/openapi/unused_component.go b/functions/openapi/unused_component.go index e7008631..c44eb276 100644 --- a/functions/openapi/unused_component.go +++ b/functions/openapi/unused_component.go @@ -10,6 +10,7 @@ import ( "github.com/pb33f/libopenapi/index" "github.com/pb33f/libopenapi/utils" "gopkg.in/yaml.v3" + "maps" "strings" ) @@ -38,8 +39,12 @@ func (uc UnusedComponent) RunRule(nodes []*yaml.Node, context model.RuleFunction var results []model.RuleFunctionResult - // extract all references, and every single component - allRefs := context.Index.GetAllReferences() + // extract all references, and every single component, recursively + var allRefs = make(map[string]*index.Reference) + for _, idx := range context.Document.GetRolodex().GetIndexes() { + refs := idx.GetAllReferences() + maps.Copy(allRefs, refs) + } schemas := context.Index.GetAllComponentSchemas() responses := context.Index.GetAllResponses() parameters := context.Index.GetAllParameters() @@ -49,7 +54,12 @@ func (uc UnusedComponent) RunRule(nodes []*yaml.Node, context model.RuleFunction securitySchemes := context.Index.GetAllSecuritySchemes() links := context.Index.GetAllLinks() callbacks := context.Index.GetAllCallbacks() - mappedRefs := context.Index.GetMappedReferences() + + var mappedRefs = make(map[string]*index.Reference) + for _, idx := range context.Document.GetRolodex().GetIndexes() { + refs := idx.GetMappedReferences() + maps.Copy(mappedRefs, refs) + } // extract securityRequirements from swagger. These are not mapped as they are not $refs // so, we need to map them as if they were. diff --git a/motor/rule_applicator.go b/motor/rule_applicator.go index 8ff2be1e..dd10ce51 100644 --- a/motor/rule_applicator.go +++ b/motor/rule_applicator.go @@ -54,7 +54,7 @@ type ruleContext struct { // of ApplyRulesToRuleSet to change, without a huge refactor. The ApplyRulesToRuleSet function only returns a single error also. type RuleSetExecution struct { RuleSet *rulesets.RuleSet // The RuleSet in which to apply - SpecFileName string // The name of the specification file, used to correctly label location + SpecFilePath string // The path of the specification file, used to correctly label location Spec []byte // The raw bytes of the OpenAPI specification. SpecInfo *datamodel.SpecInfo // Pre-parsed spec-info. CustomFunctions map[string]model.RuleFunction // custom functions loaded from plugin. @@ -109,13 +109,16 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult { // create new configurations indexConfig := index.CreateClosedAPIIndexConfig() + indexConfig.SpecFilePath = execution.SpecFilePath indexConfigUnresolved := index.CreateClosedAPIIndexConfig() + indexConfigUnresolved.SpecFilePath = execution.SpecFilePath // avoid building the index, we don't need it to run yet. indexConfig.AvoidBuildIndex = true //indexConfig.AvoidCircularReferenceCheck = true docConfig := datamodel.NewDocumentConfiguration() + docConfig.SpecFilePath = execution.SpecFilePath //docConfig.SkipCircularReferenceCheck = true if execution.IgnoreCircularArrayRef { @@ -883,7 +886,7 @@ func removeDuplicates(results *[]model.RuleFunctionResult, rse *RuleSetExecution origin := idx.FindNodeOrigin(result.StartNode) if origin != nil { if filepath.Base(origin.AbsoluteLocation) == "root.yaml" { - origin.AbsoluteLocation = rse.SpecFileName + origin.AbsoluteLocation = rse.SpecFilePath } result.Origin = origin } @@ -914,7 +917,7 @@ func removeDuplicates(results *[]model.RuleFunctionResult, rse *RuleSetExecution origin := idx.FindNodeOrigin(result.StartNode) if origin != nil { if filepath.Base(origin.AbsoluteLocation) == "root.yaml" { - origin.AbsoluteLocation = rse.SpecFileName + origin.AbsoluteLocation = rse.SpecFilePath } result.Origin = origin }