forked from Tufin/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter_diff.go
112 lines (90 loc) · 4.16 KB
/
parameter_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
package diff
import (
"github.com/rjmohammad/kin-openapi/openapi3"
)
// ParameterDiff describes the changes between a pair of parameter objects: https://swagger.io/specification/#parameter-object
type ParameterDiff struct {
ExtensionsDiff *ExtensionsDiff `json:"extensions,omitempty" yaml:"extensions,omitempty"`
DescriptionDiff *ValueDiff `json:"description,omitempty" yaml:"description,omitempty"`
StyleDiff *ValueDiff `json:"style,omitempty" yaml:"style,omitempty"`
ExplodeDiff *ValueDiff `json:"explode,omitempty" yaml:"explode,omitempty"`
AllowEmptyValueDiff *ValueDiff `json:"allowEmptyValue,omitempty" yaml:"allowEmptyValue,omitempty"`
AllowReservedDiff *ValueDiff `json:"allowReserved,omitempty" yaml:"allowReserved,omitempty"`
DeprecatedDiff *ValueDiff `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
RequiredDiff *ValueDiff `json:"required,omitempty" yaml:"required,omitempty"`
SchemaDiff *SchemaDiff `json:"schema,omitempty" yaml:"schema,omitempty"`
ExampleDiff *ValueDiff `json:"example,omitempty" yaml:"example,omitempty"`
ExamplesDiff *ExamplesDiff `json:"examples,omitempty" yaml:"examples,omitempty"`
ContentDiff *ContentDiff `json:"content,omitempty" yaml:"content,omitempty"`
}
// Empty indicates whether a change was found in this element
func (diff *ParameterDiff) Empty() bool {
return diff == nil || *diff == ParameterDiff{}
}
func (diff *ParameterDiff) removeNonBreaking(config *Config, param2 *openapi3.Parameter) {
if diff.Empty() {
return
}
diff.ExtensionsDiff = nil
diff.DescriptionDiff = nil
diff.ExampleDiff = nil
diff.ExamplesDiff = nil
if deprecationPeriodSufficient(config.DeprecationDays, param2.ExtensionProps) {
diff.DeprecatedDiff = nil
}
if !diff.RequiredDiff.CompareWithDefault(false, true, false) {
diff.RequiredDiff = nil
}
// TODO: diff.ExplodeDiff is breaking only if type is array or object
// diff.AllowEmptyValueDiff.CompareWithDefault(true, false, false) || // TODO: only if this is a query param
// diff.AllowReservedDiff.CompareWithDefault(true, false, false) || // TODO: only if this id a query param
}
func getParameterDiff(config *Config, state *state, param1, param2 *openapi3.Parameter) (*ParameterDiff, error) {
diff, err := getParameterDiffInternal(config, state, param1, param2)
if err != nil {
return nil, err
}
if config.BreakingOnly {
diff.removeNonBreaking(config, param2)
}
if diff.Empty() {
return nil, nil
}
return diff, nil
}
func getParameterDiffInternal(config *Config, state *state, param1, param2 *openapi3.Parameter) (*ParameterDiff, error) {
result := ParameterDiff{}
var err error
result.ExtensionsDiff = getExtensionsDiff(config, state, param1.ExtensionProps, param2.ExtensionProps)
result.DescriptionDiff = getValueDiffConditional(config.ExcludeDescription, param1.Description, param2.Description)
result.StyleDiff = getValueDiff(param1.Style, param2.Style)
result.ExplodeDiff = getBoolRefDiff(param1.Explode, param2.Explode)
result.AllowEmptyValueDiff = getValueDiff(param1.AllowEmptyValue, param2.AllowEmptyValue)
result.AllowReservedDiff = getValueDiff(param1.AllowReserved, param2.AllowReserved)
result.DeprecatedDiff = getValueDiff(param1.Deprecated, param2.Deprecated)
result.RequiredDiff = getValueDiff(param1.Required, param2.Required)
result.SchemaDiff, err = getSchemaDiff(config, state, param1.Schema, param2.Schema)
if err != nil {
return nil, err
}
result.ExampleDiff = getValueDiffConditional(config.ExcludeExamples, param1.Example, param2.Example)
result.ExamplesDiff, err = getExamplesDiff(config, state, param1.Examples, param2.Examples)
if err != nil {
return nil, err
}
result.ContentDiff, err = getContentDiff(config, state, param1.Content, param2.Content)
if err != nil {
return nil, err
}
return &result, nil
}
// Patch applies the patch to a parameter
func (diff *ParameterDiff) Patch(parameter *openapi3.Parameter) error {
diff.DescriptionDiff.patchString(¶meter.Description)
schema, err := derefSchema(parameter.Schema)
if err != nil {
// no schema to patch, continue.
return nil
}
return diff.SchemaDiff.Patch(schema)
}