forked from Jonathan-Rosenberg/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
operations_diff.go
164 lines (129 loc) · 4.01 KB
/
operations_diff.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package diff
import (
"fmt"
"net/http"
"regexp"
"github.com/getkin/kin-openapi/openapi3"
"github.com/treeverse/oasdiff/utils"
)
// 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 utils.StringList `json:"added,omitempty" yaml:"added,omitempty"`
Deleted utils.StringList `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: utils.StringList{},
Deleted: utils.StringList{},
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 with %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
}
}
}
}
// Patch applies the patch to operations
func (operationsDiff *OperationsDiff) Patch(operations map[string]*openapi3.Operation) error {
if operationsDiff.Empty() {
return nil
}
for method, methodDiff := range operationsDiff.Modified {
err := methodDiff.Patch(operations[method])
if err != nil {
return err
}
}
return nil
}