forked from Jonathan-Rosenberg/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_items_diff.go
81 lines (66 loc) · 2.16 KB
/
path_items_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
package diff
import (
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/treeverse/oasdiff/utils"
)
type pathItemPair struct {
PathItem1 *openapi3.PathItem
PathItem2 *openapi3.PathItem
PathParamsMap PathParamsMap
}
type pathItemPairs map[string]*pathItemPair
func getPathItemsDiff(config *Config, paths1, paths2 openapi3.Paths) (openapi3.Paths, openapi3.Paths, pathItemPairs) {
added := openapi3.Paths{}
deleted := openapi3.Paths{}
other := pathItemPairs{}
for endpoint1, pathItem1 := range paths1 {
if pathItem2, pathParamsMap, ok := findEndpoint(config, endpoint1, paths2); ok {
other[endpoint1] = &pathItemPair{
PathItem1: pathItem1,
PathItem2: pathItem2,
PathParamsMap: pathParamsMap,
}
} else {
deleted[endpoint1] = pathItem1
}
}
for endpoint2, pathItem2 := range paths2 {
if _, _, ok := findEndpoint(config, endpoint2, paths1); !ok {
added[endpoint2] = pathItem2
}
}
return added, deleted, other
}
func rewritePrefix(paths openapi3.Paths, strip, prepend string) openapi3.Paths {
result := make(openapi3.Paths, len(paths))
for path, pathItem := range paths {
result[prepend+strings.TrimPrefix(path, strip)] = pathItem
}
return result
}
func findEndpoint(config *Config, endpoint string, paths openapi3.Paths) (*openapi3.PathItem, PathParamsMap, bool) {
if pathItem, ok := paths[endpoint]; ok {
return pathItem, PathParamsMap{}, true
}
if config.IncludePathParams {
return nil, nil, false
}
return findNormalizedEndpoint(endpoint, paths)
}
/*
findNormalizedEndpoint finds a corresponding path ignoring differences in template variable names
This implementation is based on Paths.Find in openapi3
*/
func findNormalizedEndpoint(key string, paths openapi3.Paths) (*openapi3.PathItem, PathParamsMap, bool) {
normalizedPath, expected, pathParams1 := utils.NormalizeTemplatedPath(key)
for path, pathItem := range paths {
pathNormalized, got, pathParams2 := utils.NormalizeTemplatedPath(path)
if got == expected && pathNormalized == normalizedPath {
if pathParamsMap, ok := NewPathParamsMap(pathParams1, pathParams2); ok {
return pathItem, pathParamsMap, true
}
}
}
return nil, nil, false
}