Skip to content

Commit 6980514

Browse files
committed
fix(diff): swagger diff is not working for certain cases
Signed-off-by: Suraj Chafle <suraj.chafle@crowdstrike.com>
1 parent 265a770 commit 6980514

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

diff/compatibility.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ func init() {
5959
DeletedTag: NonBreaking,
6060
DeletedConstraint: NonBreaking,
6161
AddedConstraint: Breaking,
62+
ChangedDefault: Warning,
63+
AddedDefault: Warning,
64+
DeletedDefault: Warning,
65+
ChangedExample: NonBreaking,
66+
AddedExample: NonBreaking,
67+
DeletedExample: NonBreaking,
68+
ChangedCollectionFormat: Breaking,
6269
},
6370
ForChange: map[SpecChangeCode]Compatibility{
6471
NoChangeDetected: NonBreaking,

diff/difftypes.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ const (
9999
DeletedDefinition
100100
// AddedDefinition removed one of the definitions
101101
AddedDefinition
102+
// ChangedDefault - Changed default value
103+
ChangedDefault
104+
// AddedDefault - Added a default value
105+
AddedDefault
106+
// DeletedDefault - Deleted a default value
107+
DeletedDefault
108+
// ChangedExample - Changed an example value
109+
ChangedExample
110+
// AddedExample - Added an example value
111+
AddedExample
112+
// DeletedExample - Deleted an example value
113+
DeletedExample
114+
// ChangedCollectionFormat - A collectionFormat has been changed to a collectionFormat whose relative compatibility cannot be determined
115+
ChangedCollectionFormat
102116
)
103117

104118
var toLongStringSpecChangeCode = map[SpecChangeCode]string{
@@ -146,6 +160,13 @@ var toLongStringSpecChangeCode = map[SpecChangeCode]string{
146160
AddedConstraint: "Added a schema constraint",
147161
DeletedDefinition: "Deleted a schema definition",
148162
AddedDefinition: "Added a schema definition",
163+
ChangedDefault: "Default value is changed",
164+
AddedDefault: "Default value is added",
165+
DeletedDefault: "Default value is removed",
166+
ChangedExample: "Example value is changed",
167+
AddedExample: "Example value is added",
168+
DeletedExample: "Example value is removed",
169+
ChangedCollectionFormat: "Changed collection format",
149170
}
150171

151172
var toStringSpecChangeCode = map[SpecChangeCode]string{
@@ -193,6 +214,13 @@ var toStringSpecChangeCode = map[SpecChangeCode]string{
193214
AddedConstraint: "AddedConstraint",
194215
DeletedDefinition: "DeletedDefinition",
195216
AddedDefinition: "AddedDefinition",
217+
ChangedDefault: "ChangedDefault",
218+
AddedDefault: "AddedDefault",
219+
DeletedDefault: "DeletedDefault",
220+
ChangedExample: "ChangedExample",
221+
AddedExample: "AddedExample",
222+
DeletedExample: "DeletedExample",
223+
ChangedCollectionFormat: "ChangedCollectionFormat",
196224
}
197225

198226
var toIDSpecChangeCode = map[string]SpecChangeCode{}

diff/spec_analyser.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (sd *SpecAnalyser) analyseEndpointData() {
169169
}
170170

171171
func (sd *SpecAnalyser) analyseRequestParams() {
172-
locations := []string{"query", "path", "body", "header"}
172+
locations := []string{"query", "path", "body", "header", "formData"}
173173

174174
for _, paramLocation := range locations {
175175
rootNode := getNameOnlyDiffNode(strings.Title(paramLocation))
@@ -355,9 +355,9 @@ func (sd *SpecAnalyser) compareParams(urlMethod URLMethod, location string, name
355355
if len(name) > 0 {
356356
childLocation = childLocation.AddNode(getSchemaDiffNode(name, param2.Schema))
357357
}
358-
359358
sd.compareSchema(childLocation, param1.Schema, param2.Schema)
360359
}
360+
361361
diffs := sd.CompareProps(forParam(param1), forParam(param2))
362362

363363
childLocation = childLocation.AddNode(getSchemaDiffNode(name, &param2.SimpleSchema))
@@ -369,6 +369,10 @@ func (sd *SpecAnalyser) compareParams(urlMethod URLMethod, location string, name
369369
if len(diffs) > 0 {
370370
sd.addDiffs(childLocation, diffs)
371371
}
372+
373+
if &param1.SimpleSchema != nil && &param2.SimpleSchema != nil {
374+
sd.compareSimpleSchema(childLocation, &param1.SimpleSchema, &param2.SimpleSchema)
375+
}
372376
}
373377

374378
func (sd *SpecAnalyser) addTypeDiff(location DifferenceLocation, diff *TypeDiff) {
@@ -462,6 +466,51 @@ func (sd *SpecAnalyser) compareSchema(location DifferenceLocation, schema1, sche
462466
}
463467
}
464468

469+
func (sd *SpecAnalyser) compareSimpleSchema(location DifferenceLocation, schema1, schema2 *spec.SimpleSchema) {
470+
// check optional/required
471+
if schema1.Nullable != schema2.Nullable {
472+
// If optional is made required
473+
if schema1.Nullable && !schema2.Nullable {
474+
sd.addDiffs(location, addTypeDiff([]TypeDiff{}, TypeDiff{Change: ChangedOptionalToRequired, FromType: getSchemaTypeStr(schema1), ToType: getSchemaTypeStr(schema2)}))
475+
} else if !schema1.Nullable && schema2.Nullable {
476+
// If required is made optional
477+
sd.addDiffs(location, addTypeDiff([]TypeDiff{}, TypeDiff{Change: ChangedRequiredToOptional, FromType: getSchemaTypeStr(schema1), ToType: getSchemaTypeStr(schema2)}))
478+
}
479+
}
480+
481+
if schema1.CollectionFormat != schema2.CollectionFormat {
482+
sd.addDiffs(location, addTypeDiff([]TypeDiff{}, TypeDiff{Change: ChangedCollectionFormat, FromType: getSchemaTypeStr(schema1), ToType: getSchemaTypeStr(schema2)}))
483+
}
484+
485+
if schema1.Default != schema2.Default {
486+
if schema1.Default == nil && schema2.Default != nil {
487+
sd.addDiffs(location, addTypeDiff([]TypeDiff{}, TypeDiff{Change: AddedDefault, FromType: getSchemaTypeStr(schema1), ToType: getSchemaTypeStr(schema2)}))
488+
} else if schema1.Default != nil && schema2.Default == nil {
489+
sd.addDiffs(location, addTypeDiff([]TypeDiff{}, TypeDiff{Change: DeletedDefault, FromType: getSchemaTypeStr(schema1), ToType: getSchemaTypeStr(schema2)}))
490+
} else {
491+
sd.addDiffs(location, addTypeDiff([]TypeDiff{}, TypeDiff{Change: ChangedDefault, FromType: getSchemaTypeStr(schema1), ToType: getSchemaTypeStr(schema2)}))
492+
}
493+
}
494+
495+
if schema1.Example != schema2.Example {
496+
if schema1.Example == nil && schema2.Example != nil {
497+
sd.addDiffs(location, addTypeDiff([]TypeDiff{}, TypeDiff{Change: AddedExample, FromType: getSchemaTypeStr(schema1), ToType: getSchemaTypeStr(schema2)}))
498+
} else if schema1.Example != nil && schema2.Example == nil {
499+
sd.addDiffs(location, addTypeDiff([]TypeDiff{}, TypeDiff{Change: DeletedExample, FromType: getSchemaTypeStr(schema1), ToType: getSchemaTypeStr(schema2)}))
500+
} else {
501+
sd.addDiffs(location, addTypeDiff([]TypeDiff{}, TypeDiff{Change: ChangedExample, FromType: getSchemaTypeStr(schema1), ToType: getSchemaTypeStr(schema2)}))
502+
}
503+
}
504+
505+
if isArray(schema1) {
506+
if isArray(schema2) {
507+
sd.compareSimpleSchema(location, &schema1.Items.SimpleSchema, &schema2.Items.SimpleSchema)
508+
} else {
509+
sd.addDiffs(location, addTypeDiff([]TypeDiff{}, TypeDiff{Change: ChangedType, FromType: getSchemaTypeStr(schema1), ToType: getSchemaTypeStr(schema2)}))
510+
}
511+
}
512+
}
513+
465514
func (sd *SpecAnalyser) addDiffs(location DifferenceLocation, diffs []TypeDiff) {
466515
for _, e := range diffs {
467516
eachTypeDiff := e

diff/spec_difference.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ func (sd SpecDifferences) BreakingChangeCount() int {
6060
return count
6161
}
6262

63+
// WarningChangeCount Calculates the warning change count
64+
func (sd SpecDifferences) WarningChangeCount() int {
65+
count := 0
66+
for _, eachDiff := range sd {
67+
if eachDiff.Compatibility == Warning {
68+
count++
69+
}
70+
}
71+
return count
72+
}
73+
6374
// FilterIgnores returns a copy of the list without the items in the specified ignore list
6475
func (sd SpecDifferences) FilterIgnores(ignores SpecDifferences) SpecDifferences {
6576
newDiffs := SpecDifferences{}
@@ -190,6 +201,10 @@ func (sd SpecDifferences) ReportAllDiffs(fmtJSON bool) (io.Reader, error, error)
190201
if numDiffs != sd.BreakingChangeCount() {
191202
fmt.Fprintln(&out, "NON-BREAKING CHANGES:\n=====================")
192203
_, _ = out.ReadFrom(sd.reportChanges(NonBreaking))
204+
if sd.WarningChangeCount() > 0 {
205+
fmt.Fprintln(&out, "\nNON-BREAKING CHANGES WITH WARNING:\n==================================")
206+
_, _ = out.ReadFrom(sd.reportChanges(Warning))
207+
}
193208
}
194209

195210
more, err, warn := sd.ReportCompatibility()

0 commit comments

Comments
 (0)