-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from daveshanley/buffing
A collection of new rules and tuning.
- Loading branch information
Showing
23 changed files
with
88,365 additions
and
2,419 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2022 Dave Shanley / Quobix | ||
// SPDX-License-Identifier: MIT | ||
|
||
package openapi | ||
|
||
import ( | ||
"fmt" | ||
"github.com/daveshanley/vacuum/model" | ||
"gopkg.in/yaml.v3" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// AmbiguousPaths will determine if paths can be confused by a compiler. | ||
type AmbiguousPaths struct { | ||
} | ||
|
||
// GetSchema returns a model.RuleFunctionSchema defining the schema of the AmbiguousPaths rule. | ||
func (ap AmbiguousPaths) GetSchema() model.RuleFunctionSchema { | ||
return model.RuleFunctionSchema{Name: "ambiguousPaths"} | ||
} | ||
|
||
// RunRule will execute the AmbiguousPaths rule, based on supplied context and a supplied []*yaml.Node slice. | ||
func (ap AmbiguousPaths) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) []model.RuleFunctionResult { | ||
|
||
if len(nodes) <= 0 { | ||
return nil | ||
} | ||
|
||
var results []model.RuleFunctionResult | ||
var seen []string | ||
|
||
ops := context.Index.GetPathsNode() | ||
|
||
var opPath string | ||
|
||
if ops != nil { | ||
for i, op := range ops.Content { | ||
if i%2 == 0 { | ||
opPath = op.Value | ||
continue | ||
} | ||
path := fmt.Sprintf("$.paths.%s", opPath) | ||
for _, p := range seen { | ||
ambigious := checkPaths(p, opPath) | ||
if ambigious { | ||
|
||
results = append(results, model.RuleFunctionResult{ | ||
Message: fmt.Sprintf("Paths are ambiguous with one another: `%s` and `%s`", p, opPath), | ||
StartNode: op, | ||
EndNode: op, | ||
Path: path, | ||
Rule: context.Rule, | ||
}) | ||
|
||
} | ||
} | ||
seen = append(seen, opPath) | ||
|
||
} | ||
} | ||
return results | ||
} | ||
|
||
func checkPaths(pA, pB string) bool { | ||
segsA := strings.Split(pA, "/")[1:] | ||
segsB := strings.Split(pB, "/")[1:] | ||
|
||
if len(segsA) != len(segsB) { | ||
return false | ||
} | ||
|
||
a := 0 | ||
b := 0 | ||
amb := true | ||
for i, part := range segsA { | ||
aVar, _ := regexp.MatchString("^{.+?}$", part) | ||
bVar, _ := regexp.MatchString("^{.+?}$", segsB[i]) | ||
if aVar || bVar { | ||
if aVar { | ||
a++ | ||
} | ||
if bVar { | ||
b++ | ||
} | ||
continue | ||
} else { | ||
if segsA[i] != segsB[i] { | ||
amb = false | ||
} | ||
} | ||
} | ||
return amb && a == b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package openapi | ||
|
||
import ( | ||
"github.com/daveshanley/vacuum/model" | ||
"github.com/daveshanley/vacuum/utils" | ||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v3" | ||
"testing" | ||
) | ||
|
||
func TestNoAmbiguousPaths_GetSchema(t *testing.T) { | ||
def := AmbiguousPaths{} | ||
assert.Equal(t, "ambiguousPaths", def.GetSchema().Name) | ||
} | ||
|
||
func TestNoAmbiguousPaths_RunRule(t *testing.T) { | ||
def := AmbiguousPaths{} | ||
res := def.RunRule(nil, model.RuleFunctionContext{}) | ||
assert.Len(t, res, 0) | ||
} | ||
|
||
func TestAmbiguousPaths_RunRule_SuccessCheck(t *testing.T) { | ||
|
||
yml := `openapi: 3.0.0 | ||
paths: | ||
'/good/{id}': | ||
get: | ||
summary: List all pets | ||
'/good/last': | ||
get: | ||
summary: List all pets | ||
'/good/{id}/{pet}': | ||
get: | ||
summary: List all pets | ||
'/good/last/{id}': | ||
get: | ||
summary: List all pets | ||
'/{id}/ambiguous': | ||
get: | ||
summary: List all pets | ||
'/ambiguous/{id}': | ||
get: | ||
summary: List all pets | ||
'/pet/last': | ||
get: | ||
summary: List all pets | ||
'/pet/first': | ||
get: | ||
summary: List all pets | ||
'/{entity}/{id}/last': | ||
get: | ||
summary: List all pets | ||
'/pet/first/{id}': | ||
get: | ||
summary: List all pets` | ||
|
||
path := "$" | ||
|
||
var rootNode yaml.Node | ||
yaml.Unmarshal([]byte(yml), &rootNode) | ||
|
||
nodes, _ := utils.FindNodes([]byte(yml), path) | ||
|
||
rule := buildOpenApiTestRuleAction(path, "ambiguousPaths", "", nil) | ||
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil) | ||
ctx.Rule = &rule | ||
ctx.Index = model.NewSpecIndex(&rootNode) | ||
|
||
def := AmbiguousPaths{} | ||
res := def.RunRule(nodes, ctx) | ||
|
||
assert.Len(t, res, 3) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.