Skip to content

Commit

Permalink
Addressed issue #442
Browse files Browse the repository at this point in the history
Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Feb 3, 2024
1 parent 527aec2 commit 895d7df
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions functions/openapi/unused_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ 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()

// 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 Expand Up @@ -119,6 +120,10 @@ func (uc UnusedComponent) RunRule(nodes []*yaml.Node, context model.RuleFunction
found = true
}

if mappedRefs[key] != nil {
found = true
}

// check if this is a security reference definition (that does not use a $ref)
if !found {
found = checkOpenAPISecurity(key)
Expand Down
59 changes: 59 additions & 0 deletions functions/openapi/unused_component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,62 @@ components:

assert.Len(t, res, 0)
}

func TestUnusedComponent_RunRule_Success_PolymorphicCheckAllOf(t *testing.T) {

yml := `paths:
"/naughty/{puppy}":
get:
responses:
"200":
description: The naughty pup
content:
application/json:
schema:
$ref: "#/components/schemas/Dog"
components:
schemas:
Dog:
type: object
allOf:
- $ref: "#/components/schemas/Pet"
required:
- breed
properties:
breed:
$ref: "#/components/schemas/Breed"
Pet:
type: object
properties:
id:
type: String
description: Unique identifier of the Pet
age:
type: integer
format: int64
Breed:
type: object
properties:
name:
type: String
category:
type: String`

path := "$"

var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr)

nodes, _ := utils.FindNodes([]byte(yml), path)

rule := buildOpenApiTestRuleAction(path, "unused_component", "", nil)
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil)
config := index.CreateOpenAPIIndexConfig()
ctx.Index = index.NewSpecIndexWithConfig(&rootNode, config)

def := UnusedComponent{}
res := def.RunRule(nodes, ctx)

assert.Len(t, res, 0)
}

0 comments on commit 895d7df

Please sign in to comment.