forked from Tufin/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
required_diff.go
152 lines (128 loc) · 3.85 KB
/
required_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
package diff
import (
"github.com/rjmohammad/kin-openapi/openapi3"
)
// RequiredPropertiesDiff describes the changes between a pair of lists of required properties
type RequiredPropertiesDiff struct {
StringsDiff
}
// Empty indicates whether a change was found in this element
func (diff *RequiredPropertiesDiff) Empty() bool {
if diff == nil {
return true
}
return diff.StringsDiff.Empty()
}
func (diff *RequiredPropertiesDiff) removeNonBreaking(state *state) {
if diff.Empty() {
return
}
if diff.StringsDiff.Empty() {
return
}
switch state.direction {
case directionRequest:
// if this is part of the request, then required properties can be deleted without breaking the client
diff.Deleted = nil
case directionResponse:
// if this is part of the response, then required properties can be added without breaking the client
diff.Added = nil
}
}
func (diff *RequiredPropertiesDiff) removeReadOnly(state *state, schema1, schema2 *openapi3.Schema) {
if diff.Empty() || diff.StringsDiff.Empty() || state.direction == directionResponse {
// readonly properties are only valid for responses
return
}
added := make(StringList, 0)
for _, v := range diff.Added {
if p, ok := schema2.Properties[v]; ok && !p.Value.ReadOnly {
added = append(added, v)
}
}
diff.Added = added
deleted := make(StringList, 0)
for _, v := range diff.Deleted {
if p, ok := schema1.Properties[v]; ok && !p.Value.ReadOnly {
deleted = append(deleted, v)
}
}
diff.Deleted = deleted
}
func (diff *RequiredPropertiesDiff) removeWriteOnly(state *state, schema1, schema2 *openapi3.Schema) {
if diff.Empty() || diff.StringsDiff.Empty() || state.direction == directionRequest {
// writeOnly properties are only valid for requests
return
}
added := make(StringList, 0)
for _, v := range diff.Added {
if p, ok := schema2.Properties[v]; ok && !p.Value.WriteOnly {
added = append(added, v)
}
}
diff.Added = added
deleted := make(StringList, 0)
for _, v := range diff.Deleted {
if p, ok := schema1.Properties[v]; ok && !p.Value.WriteOnly {
deleted = append(deleted, v)
}
}
diff.Deleted = deleted
}
func propDeleted(property string, schema1, schema2 *openapi3.Schema) bool {
if schema1 == nil || schema2 == nil {
return false
}
_, ok1 := schema1.Properties[property]
_, ok2 := schema2.Properties[property]
return ok1 && !ok2
}
func propSunsetAllowed(property string, schema1 *openapi3.Schema) bool {
if schema1 == nil {
return false
}
schemaRef, ok := schema1.Properties[property]
if !ok || schemaRef == nil || schemaRef.Value == nil {
return false
}
return sunsetAllowed(schemaRef.Value.Deprecated, schemaRef.Value.ExtensionProps)
}
func (diff *RequiredPropertiesDiff) removeSunsetProperties(state *state, schema1, schema2 *openapi3.Schema) {
if diff.Empty() {
return
}
if state.direction != directionResponse {
return
}
deleted := make(StringList, 0)
for _, property := range diff.Deleted {
// if property was sunset then making it optional is not breaking
if propDeleted(property, schema1, schema2) && propSunsetAllowed(property, schema1) {
continue
} else {
deleted = append(deleted, property)
}
}
diff.Deleted = deleted
}
func getRequiredPropertiesDiff(config *Config, state *state, schema1, schema2 *openapi3.Schema) *RequiredPropertiesDiff {
diff := getRequiredPropertiesDiffInternal(schema1.Required, schema2.Required)
diff.removeReadOnly(state, schema1, schema2)
diff.removeWriteOnly(state, schema1, schema2)
diff.removeSunsetProperties(state, schema1, schema2)
if config.BreakingOnly {
diff.removeNonBreaking(state)
}
if diff.Empty() {
return nil
}
return diff
}
func getRequiredPropertiesDiffInternal(strings1, strings2 StringList) *RequiredPropertiesDiff {
if stringsDiff := getStringsDiff(strings1, strings2); stringsDiff != nil {
return &RequiredPropertiesDiff{
StringsDiff: *stringsDiff,
}
}
return nil
}