forked from Tufin/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths_diff.go
211 lines (164 loc) · 4.81 KB
/
paths_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package diff
import (
"fmt"
"regexp"
"github.com/rjmohammad/kin-openapi/openapi3"
)
// PathsDiff describes the changes between a pair of Paths objects: https://swagger.io/specification/#paths-object
type PathsDiff struct {
Added StringList `json:"added,omitempty" yaml:"added,omitempty"`
Deleted StringList `json:"deleted,omitempty" yaml:"deleted,omitempty"`
Modified ModifiedPaths `json:"modified,omitempty" yaml:"modified,omitempty"`
}
// Empty indicates whether a change was found in this element
func (pathsDiff *PathsDiff) Empty() bool {
if pathsDiff == nil {
return true
}
return len(pathsDiff.Added) == 0 &&
len(pathsDiff.Deleted) == 0 &&
len(pathsDiff.Modified) == 0
}
// removeSunset removes deleted paths that were deleted after a sufficient deprecation period for all contained operations
func (pathsDiff *PathsDiff) removeSunset(paths1 openapi3.Paths) {
if paths1 == nil {
return
}
deleted := []string{}
for _, path := range pathsDiff.Deleted {
pathItem := paths1[path]
for _, operation := range pathItem.Operations() {
if !sunsetAllowed(operation.Deprecated, operation.ExtensionProps) {
deleted = append(deleted, path)
break
}
}
}
pathsDiff.Deleted = deleted
}
func (pathsDiff *PathsDiff) removeNonBreaking(paths1 openapi3.Paths) {
if pathsDiff.Empty() {
return
}
pathsDiff.removeSunset(paths1)
pathsDiff.Added = nil
}
func newPathsDiff() *PathsDiff {
return &PathsDiff{
Added: []string{},
Deleted: []string{},
Modified: ModifiedPaths{},
}
}
func getPathsDiff(config *Config, state *state, paths1, paths2 openapi3.Paths) (*PathsDiff, error) {
if err := filterPaths(config.PathFilter, config.FilterExtension, paths1, paths2); err != nil {
return nil, err
}
diff, err := getPathsDiffInternal(config, state, paths1, paths2)
if err != nil {
return nil, err
}
if config.BreakingOnly {
diff.removeNonBreaking(paths1)
}
if diff.Empty() {
return nil, nil
}
return diff, nil
}
func getPathsDiffInternal(config *Config, state *state, paths1, paths2 openapi3.Paths) (*PathsDiff, error) {
result := newPathsDiff()
addedPaths, deletedPaths, otherPaths := getPathItemsDiff(paths1, paths2, config)
for endpoint := range addedPaths {
result.addAddedPath(endpoint)
}
for endpoint := range deletedPaths {
result.addDeletedPath(endpoint)
}
for endpoint, pathItemPair := range otherPaths {
err := result.addModifiedPath(config, state, endpoint, pathItemPair.PathItem1, pathItemPair.PathItem2)
if err != nil {
return nil, err
}
}
return result, nil
}
func (pathsDiff *PathsDiff) getSummary() *SummaryDetails {
return &SummaryDetails{
Added: len(pathsDiff.Added),
Deleted: len(pathsDiff.Deleted),
Modified: len(pathsDiff.Modified),
}
}
func (pathsDiff *PathsDiff) addAddedPath(path string) {
pathsDiff.Added = append(pathsDiff.Added, path)
}
func (pathsDiff *PathsDiff) addDeletedPath(path string) {
pathsDiff.Deleted = append(pathsDiff.Deleted, path)
}
func (pathsDiff *PathsDiff) addModifiedPath(config *Config, state *state, path1 string, pathItem1, pathItem2 *openapi3.PathItem) error {
return pathsDiff.Modified.addPathDiff(config, state, path1, pathItem1, pathItem2)
}
func filterPaths(filter, filterExtension string, paths1, paths2 openapi3.Paths) error {
if err := filterPathsByName(filter, paths1, paths2); err != nil {
return err
}
if err := filterPathsByExtensions(filterExtension, paths1, paths2); err != nil {
return err
}
return nil
}
func filterPathsByName(filter string, paths1, paths2 openapi3.Paths) error {
if filter == "" {
return nil
}
r, err := regexp.Compile(filter)
if err != nil {
return fmt.Errorf("failed to compile filter regex %q with %w", filter, err)
}
filterPathsInternal(paths1, r)
filterPathsInternal(paths2, r)
return nil
}
func filterPathsInternal(paths openapi3.Paths, r *regexp.Regexp) {
for path := range paths {
if !r.MatchString(path) {
delete(paths, path)
}
}
}
func filterPathsByExtensions(filterExtension string, paths1, paths2 openapi3.Paths) 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)
}
filterPathsByExtensionInternal(paths1, r)
filterPathsByExtensionInternal(paths2, r)
return nil
}
func filterPathsByExtensionInternal(paths openapi3.Paths, r *regexp.Regexp) {
for path, pathItem := range paths {
for extension := range pathItem.Extensions {
if r.MatchString(extension) {
delete(paths, path)
break
}
}
}
}
// Patch applies the patch to paths
func (pathsDiff *PathsDiff) Patch(paths openapi3.Paths) error {
if pathsDiff.Empty() {
return nil
}
for path, pathDiff := range pathsDiff.Modified {
err := pathDiff.Patch(paths.Find(path))
if err != nil {
return err
}
}
return nil
}