Skip to content

Commit

Permalink
- Renamed RuleSetExecution.SpecFileNam to SpecFilePath because it's a…
Browse files Browse the repository at this point in the history
…ctually set to the path, not just the filename

- Recursively build reference maps in unused-components check
  • Loading branch information
Calvin Lobo committed Aug 16, 2024
1 parent ca4b96e commit cb60f8e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 13 additions & 3 deletions functions/openapi/unused_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
"maps"
"strings"
)

Expand Down Expand Up @@ -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()
Expand All @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions motor/rule_applicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -109,13 +109,16 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {

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

Check failure on line 112 in motor/rule_applicator.go

View workflow job for this annotation

GitHub Actions / Build

indexConfig.SpecFilePath undefined (type *index.SpecIndexConfig has no field or method SpecFilePath)
indexConfigUnresolved := index.CreateClosedAPIIndexConfig()
indexConfigUnresolved.SpecFilePath = execution.SpecFilePath

Check failure on line 114 in motor/rule_applicator.go

View workflow job for this annotation

GitHub Actions / Build

indexConfigUnresolved.SpecFilePath undefined (type *index.SpecIndexConfig has no field or method 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

Check failure on line 121 in motor/rule_applicator.go

View workflow job for this annotation

GitHub Actions / Build

docConfig.SpecFilePath undefined (type *datamodel.DocumentConfiguration has no field or method SpecFilePath)
//docConfig.SkipCircularReferenceCheck = true

if execution.IgnoreCircularArrayRef {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit cb60f8e

Please sign in to comment.