-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
operation_id.go
60 lines (47 loc) · 1.62 KB
/
operation_id.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2022 Dave Shanley / Quobix
// SPDX-License-Identifier: MIT
package openapi
import (
"fmt"
"github.com/daveshanley/vacuum/model"
vacuumUtils "github.com/daveshanley/vacuum/utils"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
)
// OperationId is a rule that will check if each operation provides an operationId
type OperationId struct {
}
// GetSchema returns a model.RuleFunctionSchema defining the schema of the OperationId rule.
func (oId OperationId) GetSchema() model.RuleFunctionSchema {
return model.RuleFunctionSchema{
Name: "oasOpId",
}
}
// GetCategory returns the category of the OperationId rule.
func (oId OperationId) GetCategory() string {
return model.FunctionCategoryOpenAPI
}
// RunRule will execute the OperationId rule, based on supplied context and a supplied []*yaml.Node slice.
func (oId OperationId) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) []model.RuleFunctionResult {
if len(nodes) <= 0 {
return nil
}
var results []model.RuleFunctionResult
paths := context.Index.GetAllPaths()
for path, methodMap := range paths {
for method, methodNode := range methodMap {
_, operationId := utils.FindKeyNode("operationId", methodNode.Node.Content)
if operationId == nil {
results = append(results, model.RuleFunctionResult{
Message: fmt.Sprintf("the '%s' operation at path '%s' does not contain an operationId",
method, path),
StartNode: methodNode.Node,
EndNode: vacuumUtils.BuildEndNode(methodNode.Node),
Path: fmt.Sprintf("$.paths['%s'].%s", path, method),
Rule: context.Rule,
})
}
}
}
return results
}