forked from Jonathan-Rosenberg/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
paths_diff.go
185 lines (144 loc) · 4.38 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
package diff
import (
"fmt"
"regexp"
"github.com/getkin/kin-openapi/openapi3"
"github.com/treeverse/oasdiff/utils"
)
// PathsDiff describes the changes between a pair of Paths objects: https://swagger.io/specification/#paths-object
type PathsDiff struct {
Added utils.StringList `json:"added,omitempty" yaml:"added,omitempty"`
Deleted utils.StringList `json:"deleted,omitempty" yaml:"deleted,omitempty"`
Modified ModifiedPaths `json:"modified,omitempty" yaml:"modified,omitempty"`
Base openapi3.Paths `json:"-" yaml:"-"`
Revision openapi3.Paths `json:"-" yaml:"-"`
}
// 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
}
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 diff.Empty() {
return nil, nil
}
return diff, nil
}
func getPathsDiffInternal(config *Config, state *state, paths1, paths2 openapi3.Paths) (*PathsDiff, error) {
result := newPathsDiff()
paths1Mod := rewritePrefix(paths1, config.PathStripPrefixBase, config.PathPrefixBase)
paths2Mod := rewritePrefix(paths2, config.PathStripPrefixRevision, config.PathPrefixRevision)
addedPaths, deletedPaths, otherPaths := getPathItemsDiff(config, paths1Mod, paths2Mod)
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)
if err != nil {
return nil, err
}
}
result.Base = paths1Mod
result.Revision = paths2Mod
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, pathItemPair *pathItemPair) error {
return pathsDiff.Modified.addPathDiff(config, state, path1, pathItemPair)
}
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
}