forked from Tufin/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-response-optional-property-updated.go
92 lines (83 loc) · 2.99 KB
/
check-response-optional-property-updated.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
package checker
import (
"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/diff"
"golang.org/x/exp/slices"
)
const (
ResponseOptionalPropertyRemovedId = "response-optional-property-removed"
ResponseOptionalWriteOnlyPropertyRemovedId = "response-optional-write-only-property-removed"
ResponseOptionalPropertyAddedId = "response-optional-property-added"
ResponseOptionalWriteOnlyPropertyAddedId = "response-optional-write-only-property-added"
)
func ResponseOptionalPropertyUpdatedCheck(diffReport *diff.Diff, operationsSources *diff.OperationsSourcesMap, config *Config) Changes {
result := make(Changes, 0)
if diffReport.PathsDiff == nil {
return result
}
for path, pathItem := range diffReport.PathsDiff.Modified {
if pathItem.OperationsDiff == nil {
continue
}
for operation, operationItem := range pathItem.OperationsDiff.Modified {
source := (*operationsSources)[operationItem.Revision]
if operationItem.ResponsesDiff == nil {
continue
}
for responseStatus, responseDiff := range operationItem.ResponsesDiff.Modified {
if responseDiff.ContentDiff == nil ||
responseDiff.ContentDiff.MediaTypeModified == nil {
continue
}
modifiedMediaTypes := responseDiff.ContentDiff.MediaTypeModified
for _, mediaTypeDiff := range modifiedMediaTypes {
CheckDeletedPropertiesDiff(
mediaTypeDiff.SchemaDiff,
func(propertyPath string, propertyName string, propertyItem *openapi3.Schema, parent *diff.SchemaDiff) {
level := WARN
id := ResponseOptionalPropertyRemovedId
if propertyItem.WriteOnly {
level = INFO
id = ResponseOptionalWriteOnlyPropertyRemovedId
}
if slices.Contains(parent.Base.Required, propertyName) {
// covered by response-required-property-removed
return
}
result = append(result, ApiChange{
Id: id,
Level: level,
Args: []any{propertyFullName(propertyPath, propertyName), responseStatus},
Operation: operation,
OperationId: operationItem.Revision.OperationID,
Path: path,
Source: source,
})
})
CheckAddedPropertiesDiff(
mediaTypeDiff.SchemaDiff,
func(propertyPath string, propertyName string, propertyItem *openapi3.Schema, parent *diff.SchemaDiff) {
id := ResponseOptionalPropertyAddedId
if propertyItem.WriteOnly {
id = ResponseOptionalWriteOnlyPropertyAddedId
}
if slices.Contains(parent.Revision.Required, propertyName) {
// covered by response-required-property-added
return
}
result = append(result, ApiChange{
Id: id,
Level: INFO,
Args: []any{propertyFullName(propertyPath, propertyName), responseStatus},
Operation: operation,
OperationId: operationItem.Revision.OperationID,
Path: path,
Source: source,
})
})
}
}
}
}
return result
}