-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathoperations_diff.go
More file actions
146 lines (115 loc) · 3.62 KB
/
operations_diff.go
File metadata and controls
146 lines (115 loc) · 3.62 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package diff
import (
"fmt"
"net/http"
"regexp"
"github.com/getkin/kin-openapi/openapi3"
)
// OperationsDiff describes the changes between a pair of operation objects (https://swagger.io/specification/#operation-object) of two path item objects
type OperationsDiff struct {
Added []string `json:"added,omitempty" yaml:"added,omitempty"`
Deleted []string `json:"deleted,omitempty" yaml:"deleted,omitempty"`
Modified ModifiedOperations `json:"modified,omitempty" yaml:"modified,omitempty"`
}
// Empty indicates whether a change was found in this element
func (operationsDiff *OperationsDiff) Empty() bool {
if operationsDiff == nil {
return true
}
return len(operationsDiff.Added) == 0 &&
len(operationsDiff.Deleted) == 0 &&
len(operationsDiff.Modified) == 0
}
func newOperationsDiff() *OperationsDiff {
return &OperationsDiff{
Added: []string{},
Deleted: []string{},
Modified: ModifiedOperations{},
}
}
// ModifiedOperations is a map of HTTP methods to their respective diffs
type ModifiedOperations map[string]*MethodDiff
func getOperationsDiff(config *Config, state *state, pathItemPair *pathItemPair) (*OperationsDiff, error) {
if err := filterOperations(config.FilterExtension, pathItemPair); err != nil {
return nil, err
}
diff, err := getOperationsDiffInternal(config, state, pathItemPair)
if err != nil {
return nil, err
}
if diff.Empty() {
return nil, nil
}
return diff, nil
}
var operations = []string{
http.MethodGet,
http.MethodHead,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodConnect,
http.MethodOptions,
http.MethodTrace,
}
func getOperationsDiffInternal(config *Config, state *state, pathItemPair *pathItemPair) (*OperationsDiff, error) {
result := newOperationsDiff()
var err error
for _, op := range operations {
err = result.diffOperation(config, state, pathItemPair.PathItem1.GetOperation(op), pathItemPair.PathItem2.GetOperation(op), op, pathItemPair.PathParamsMap)
if err != nil {
return nil, err
}
}
return result, nil
}
func (operationsDiff *OperationsDiff) diffOperation(config *Config, state *state, operation1, operation2 *openapi3.Operation, method string, pathParamsMap PathParamsMap) error {
if operation1 == nil && operation2 == nil {
return nil
}
if operation1 == nil && operation2 != nil {
operationsDiff.Added = append(operationsDiff.Added, method)
return nil
}
if operation1 != nil && operation2 == nil {
operationsDiff.Deleted = append(operationsDiff.Deleted, method)
return nil
}
diff, err := getMethodDiff(config, state, operation1, operation2, pathParamsMap)
if err != nil {
return err
}
if !diff.Empty() {
operationsDiff.Modified[method] = diff
}
return nil
}
func filterOperations(filterExtension string, pathItemPair *pathItemPair) error {
if err := filterOperationsByExtensions(filterExtension, pathItemPair); err != nil {
return err
}
return nil
}
func filterOperationsByExtensions(filterExtension string, pathItemPair *pathItemPair) error {
if filterExtension == "" {
return nil
}
r, err := regexp.Compile(filterExtension)
if err != nil {
return fmt.Errorf("failed to compile extension filter regex %q: %w", filterExtension, err)
}
filterOperationsByExtensionInternal(pathItemPair.PathItem1, r)
filterOperationsByExtensionInternal(pathItemPair.PathItem2, r)
return nil
}
func filterOperationsByExtensionInternal(pathItem *openapi3.PathItem, r *regexp.Regexp) {
for method, operation := range pathItem.Operations() {
for extension := range operation.Extensions {
if r.MatchString(extension) {
pathItem.SetOperation(method, nil)
break
}
}
}
}