forked from Tufin/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_list_diff.go
205 lines (169 loc) · 5.32 KB
/
schema_list_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
package diff
import (
"fmt"
"reflect"
"github.com/rjmohammad/kin-openapi/openapi3"
)
/*
SchemaListDiff describes the changes between a pair of lists of schema objects: https://swagger.io/specification/#schema-object
The result is a combination of two diffs:
1. Diff of schemas with a $ref: number of added/deleted schemas; modified=diff of schemas with the same $ref
2. Diff of schemas without a $ref (inline schemas): number of added/deleted schemas; modified=only if exactly one schema was added and one deleted, the Modified field will show a diff between them
*/
type SchemaListDiff struct {
Added int `json:"added,omitempty" yaml:"added,omitempty"`
Deleted int `json:"deleted,omitempty" yaml:"deleted,omitempty"`
Modified ModifiedSchemas `json:"modified,omitempty" yaml:"modified,omitempty"`
}
// Empty indicates whether a change was found in this element
func (diff *SchemaListDiff) Empty() bool {
if diff == nil {
return true
}
return diff.Added == 0 &&
diff.Deleted == 0 &&
len(diff.Modified) == 0
}
func getSchemaListsDiff(config *Config, state *state, schemaRefs1, schemaRefs2 openapi3.SchemaRefs) (*SchemaListDiff, error) {
diff, err := getSchemaListsDiffInternal(config, state, schemaRefs1, schemaRefs2)
if err != nil {
return nil, err
}
if diff.Empty() {
return nil, nil
}
return diff, nil
}
func (diff SchemaListDiff) combine(other SchemaListDiff) (*SchemaListDiff, error) {
return &SchemaListDiff{
Added: diff.Added + other.Added,
Deleted: diff.Deleted + other.Deleted,
Modified: diff.Modified.combine(other.Modified),
}, nil
}
func getSchemaListsDiffInternal(config *Config, state *state, schemaRefs1, schemaRefs2 openapi3.SchemaRefs) (*SchemaListDiff, error) {
if len(schemaRefs1) == 0 && len(schemaRefs2) == 0 {
return nil, nil
}
diffRefs, err := getSchemaListsRefsDiff(config, state, schemaRefs1, schemaRefs2, isSchemaRef)
if err != nil {
return nil, err
}
diffInline, err := getSchemaListsInlineDiff(config, state, schemaRefs1, schemaRefs2, isSchemaInline)
if err != nil {
return nil, err
}
return diffRefs.combine(diffInline)
}
type schemaRefsFilter func(schemaRef *openapi3.SchemaRef) bool
// getSchemaListsRefsDiff compares schemas by $ref name
func getSchemaListsRefsDiff(config *Config, state *state, schemaRefs1, schemaRefs2 openapi3.SchemaRefs, filter schemaRefsFilter) (SchemaListDiff, error) {
deleted := 0
modified := ModifiedSchemas{}
schemaMap2 := toSchemaRefMap(schemaRefs2, filter)
for _, schema1 := range schemaRefs1 {
if !filter(schema1) {
continue
}
ref := schema1.Ref
if schema2, found := schemaMap2[ref]; found {
schemaMap2.delete(ref)
if err := modified.addSchemaDiff(config, state, ref, schema1, schema2.schemaRef); err != nil {
return SchemaListDiff{}, err
}
} else {
deleted++
}
}
added := 0
schemaMap1 := toSchemaRefMap(schemaRefs1, filter)
for _, schema2 := range schemaRefs2 {
if !filter(schema2) {
continue
}
ref := schema2.Ref
if _, found := schemaMap1[ref]; found {
schemaMap1.delete(ref)
} else {
added++
}
}
return SchemaListDiff{
Added: added,
Deleted: deleted,
Modified: modified,
}, nil
}
// getSchemaListsRefsDiff compares schemas by their syntax
func getSchemaListsInlineDiff(config *Config, state *state, schemaRefs1, schemaRefs2 openapi3.SchemaRefs, filter schemaRefsFilter) (SchemaListDiff, error) {
added, err := getGroupDifference(schemaRefs2, schemaRefs1, filter)
if err != nil {
return SchemaListDiff{}, err
}
deleted, err := getGroupDifference(schemaRefs1, schemaRefs2, filter)
if err != nil {
return SchemaListDiff{}, err
}
if len(added) == 1 && len(deleted) == 1 {
d, err := getSchemaDiff(config, state, schemaRefs1[deleted[0]], schemaRefs2[added[0]])
if err != nil {
return SchemaListDiff{}, err
}
if d.Empty() {
return SchemaListDiff{}, err
}
return SchemaListDiff{
Modified: ModifiedSchemas{fmt.Sprintf("#%d", 1+deleted[0]): d},
}, nil
}
return SchemaListDiff{
Added: len(added),
Deleted: len(deleted),
}, nil
}
func getGroupDifference(schemaRefs1, schemaRefs2 openapi3.SchemaRefs, filter schemaRefsFilter) ([]int, error) {
notContained := []int{}
matched := map[int]struct{}{}
for index1, schemaRef1 := range schemaRefs1 {
if !filter(schemaRef1) {
continue
}
if found, index2 := findIndenticalSchema(schemaRef1, schemaRefs2, matched, filter); !found {
notContained = append(notContained, index1)
} else {
matched[index2] = struct{}{}
}
}
return notContained, nil
}
func findIndenticalSchema(schemaRef1 *openapi3.SchemaRef, schemasRefs2 openapi3.SchemaRefs, matched map[int]struct{}, filter schemaRefsFilter) (bool, int) {
for index2, schemaRef2 := range schemasRefs2 {
if !filter(schemaRef1) {
continue
}
if alreadyMatched(index2, matched) {
continue
}
// compare with DeepEqual rather than SchemaDiff to ensure an exact syntactical match
if reflect.DeepEqual(schemaRef1, schemaRef2) {
return true, index2
}
}
return false, 0
}
func alreadyMatched(index int, matched map[int]struct{}) bool {
_, found := matched[index]
return found
}
func isSchemaInline(schemaRef *openapi3.SchemaRef) bool {
if schemaRef == nil {
return false
}
return schemaRef.Ref == ""
}
func isSchemaRef(schemaRef *openapi3.SchemaRef) bool {
if schemaRef == nil {
return false
}
return schemaRef.Ref != ""
}