Skip to content

Commit cb52f84

Browse files
committed
enhance: openapi3lint: update RuleOperationXPropertyStringExist to support spec and pathInfo level items
1 parent c0d29b5 commit cb52f84

File tree

2 files changed

+119
-43
lines changed

2 files changed

+119
-43
lines changed

openapi3/visit.go

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,70 @@ func VisitTypesFormats(spec *oas3.Swagger, visitTypeFormat func(jsonPointerRoot,
6363
)
6464
}
6565

66+
func VisitOperationsPathItem(pathURL string, pathItem *oas3.PathItem, visitOp func(path, method string, op *oas3.Operation)) {
67+
if pathItem == nil {
68+
return
69+
}
70+
if pathItem.Connect != nil {
71+
visitOp(pathURL, http.MethodConnect, pathItem.Connect)
72+
}
73+
if pathItem.Delete != nil {
74+
visitOp(pathURL, http.MethodDelete, pathItem.Delete)
75+
}
76+
if pathItem.Get != nil {
77+
visitOp(pathURL, http.MethodGet, pathItem.Get)
78+
}
79+
if pathItem.Head != nil {
80+
visitOp(pathURL, http.MethodHead, pathItem.Head)
81+
}
82+
if pathItem.Options != nil {
83+
visitOp(pathURL, http.MethodOptions, pathItem.Options)
84+
}
85+
if pathItem.Patch != nil {
86+
visitOp(pathURL, http.MethodPatch, pathItem.Patch)
87+
}
88+
if pathItem.Post != nil {
89+
visitOp(pathURL, http.MethodPost, pathItem.Post)
90+
}
91+
if pathItem.Put != nil {
92+
visitOp(pathURL, http.MethodPut, pathItem.Put)
93+
}
94+
if pathItem.Trace != nil {
95+
visitOp(pathURL, http.MethodTrace, pathItem.Trace)
96+
}
97+
}
98+
6699
func VisitOperations(spec *oas3.Swagger, visitOp func(path, method string, op *oas3.Operation)) {
67100
for path, pathItem := range spec.Paths {
68-
if pathItem.Connect != nil {
69-
visitOp(path, http.MethodConnect, pathItem.Connect)
70-
}
71-
if pathItem.Delete != nil {
72-
visitOp(path, http.MethodDelete, pathItem.Delete)
73-
}
74-
if pathItem.Get != nil {
75-
visitOp(path, http.MethodGet, pathItem.Get)
76-
}
77-
if pathItem.Head != nil {
78-
visitOp(path, http.MethodHead, pathItem.Head)
79-
}
80-
if pathItem.Options != nil {
81-
visitOp(path, http.MethodOptions, pathItem.Options)
82-
}
83-
if pathItem.Patch != nil {
84-
visitOp(path, http.MethodPatch, pathItem.Patch)
85-
}
86-
if pathItem.Post != nil {
87-
visitOp(path, http.MethodPost, pathItem.Post)
88-
}
89-
if pathItem.Put != nil {
90-
visitOp(path, http.MethodPut, pathItem.Put)
91-
}
92-
if pathItem.Trace != nil {
93-
visitOp(path, http.MethodTrace, pathItem.Trace)
94-
}
101+
VisitOperationsPathItem(path, pathItem, visitOp)
102+
/*
103+
if pathItem.Connect != nil {
104+
visitOp(path, http.MethodConnect, pathItem.Connect)
105+
}
106+
if pathItem.Delete != nil {
107+
visitOp(path, http.MethodDelete, pathItem.Delete)
108+
}
109+
if pathItem.Get != nil {
110+
visitOp(path, http.MethodGet, pathItem.Get)
111+
}
112+
if pathItem.Head != nil {
113+
visitOp(path, http.MethodHead, pathItem.Head)
114+
}
115+
if pathItem.Options != nil {
116+
visitOp(path, http.MethodOptions, pathItem.Options)
117+
}
118+
if pathItem.Patch != nil {
119+
visitOp(path, http.MethodPatch, pathItem.Patch)
120+
}
121+
if pathItem.Post != nil {
122+
visitOp(path, http.MethodPost, pathItem.Post)
123+
}
124+
if pathItem.Put != nil {
125+
visitOp(path, http.MethodPut, pathItem.Put)
126+
}
127+
if pathItem.Trace != nil {
128+
visitOp(path, http.MethodTrace, pathItem.Trace)
129+
}
130+
*/
95131
}
96132
}

openapi3lint/ruleopxpropertystringexist/rule_op_xproperty_string_exist.go

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"strings"
66

77
oas3 "github.com/getkin/kin-openapi/openapi3"
8-
"github.com/grokify/simplego/net/urlutil"
8+
"github.com/grokify/simplego/encoding/jsonutil"
99
"github.com/grokify/spectrum/openapi3"
1010
"github.com/grokify/spectrum/openapi3lint/lintutil"
1111
)
@@ -39,25 +39,65 @@ func (rule RuleOperationXPropertyStringExist) Name() string {
3939
}
4040

4141
func (rule RuleOperationXPropertyStringExist) Scope() string {
42-
return lintutil.ScopeOperation
42+
return lintutil.ScopeSpecification
4343
}
4444

4545
func (rule RuleOperationXPropertyStringExist) ProcessOperation(spec *oas3.Swagger, op *oas3.Operation, opPointer, path, method string) []lintutil.PolicyViolation {
46-
if spec == nil || op == nil || len(op.OperationID) == 0 {
47-
return nil
48-
}
49-
prop := strings.TrimSpace(
50-
openapi3.GetOperationExtensionPropStringOrEmpty(*op, rule.xPropertyName))
51-
if len(prop) > 0 {
52-
return nil
53-
}
54-
vio := lintutil.PolicyViolation{
55-
RuleName: rule.Name(),
56-
Location: urlutil.JoinAbsolute(opPointer, "operationId"),
57-
Value: op.OperationID}
58-
return []lintutil.PolicyViolation{vio}
46+
/*
47+
if spec == nil || op == nil || len(op.OperationID) == 0 {
48+
return nil
49+
}
50+
prop := strings.TrimSpace(
51+
openapi3.GetOperationExtensionPropStringOrEmpty(*op, rule.xPropertyName))
52+
if len(prop) > 0 {
53+
return nil
54+
}
55+
vio := lintutil.PolicyViolation{
56+
RuleName: rule.Name(),
57+
Location: urlutil.JoinAbsolute(opPointer, "operationId"),
58+
Value: op.OperationID}
59+
*/
60+
return []lintutil.PolicyViolation{}
5961
}
6062

6163
func (rule RuleOperationXPropertyStringExist) ProcessSpec(spec *oas3.Swagger, pointerBase string) []lintutil.PolicyViolation {
62-
return []lintutil.PolicyViolation{}
64+
vios := []lintutil.PolicyViolation{}
65+
if spec == nil {
66+
return []lintutil.PolicyViolation{}
67+
}
68+
propVal := strings.TrimSpace(openapi3.GetExtensionPropStringOrEmpty(
69+
spec.ExtensionProps, rule.xPropertyName))
70+
if len(propVal) > 0 {
71+
return []lintutil.PolicyViolation{}
72+
}
73+
for pathURL, pathItem := range spec.Paths {
74+
if pathItem == nil {
75+
continue
76+
}
77+
propVal := strings.TrimSpace(openapi3.GetExtensionPropStringOrEmpty(
78+
pathItem.ExtensionProps, rule.xPropertyName))
79+
if len(propVal) > 0 {
80+
continue
81+
}
82+
83+
openapi3.VisitOperationsPathItem(pathURL, pathItem,
84+
func(path, method string, op *oas3.Operation) {
85+
if op == nil {
86+
return
87+
}
88+
propVal := strings.TrimSpace(openapi3.GetExtensionPropStringOrEmpty(
89+
op.ExtensionProps, rule.xPropertyName))
90+
if len(propVal) == 0 {
91+
vios = append(vios, lintutil.PolicyViolation{
92+
RuleName: rule.Name(),
93+
Location: jsonutil.PointerSubEscapeAll(
94+
"%s#/paths/%s/%s/%s",
95+
pointerBase, pathURL, method, rule.xPropertyName,
96+
),
97+
})
98+
}
99+
},
100+
)
101+
}
102+
return vios
63103
}

0 commit comments

Comments
 (0)