-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement response discriminator checker (#347)
- Loading branch information
Showing
21 changed files
with
1,306 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package checker | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tufin/oasdiff/diff" | ||
) | ||
|
||
func RequestDiscriminatorUpdatedCheck(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 { | ||
if operationItem.RequestBodyDiff == nil || | ||
operationItem.RequestBodyDiff.ContentDiff == nil || | ||
operationItem.RequestBodyDiff.ContentDiff.MediaTypeModified == nil { | ||
continue | ||
} | ||
source := (*operationsSources)[operationItem.Revision] | ||
|
||
appendResultItem := func(messageId string, a ...any) { | ||
result = append(result, ApiChange{ | ||
Id: messageId, | ||
Level: INFO, | ||
Text: fmt.Sprintf(config.i18n(messageId), a...), | ||
Operation: operation, | ||
OperationId: operationItem.Revision.OperationID, | ||
Path: path, | ||
Source: source, | ||
}) | ||
} | ||
|
||
for _, mediaTypeDiff := range operationItem.RequestBodyDiff.ContentDiff.MediaTypeModified { | ||
if mediaTypeDiff.SchemaDiff == nil { | ||
continue | ||
} | ||
|
||
processDiscriminatorDiffForRequest( | ||
mediaTypeDiff.SchemaDiff.DiscriminatorDiff, | ||
"", | ||
appendResultItem) | ||
|
||
CheckModifiedPropertiesDiff( | ||
mediaTypeDiff.SchemaDiff, | ||
func(propertyPath string, propertyName string, propertyDiff *diff.SchemaDiff, parent *diff.SchemaDiff) { | ||
processDiscriminatorDiffForRequest( | ||
propertyDiff.DiscriminatorDiff, | ||
propertyFullName(propertyPath, propertyName), | ||
appendResultItem) | ||
}) | ||
|
||
} | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func processDiscriminatorDiffForRequest( | ||
discriminatorDiff *diff.DiscriminatorDiff, | ||
propertyName string, | ||
appendResultItem func(messageId string, a ...any)) { | ||
|
||
if discriminatorDiff == nil { | ||
return | ||
} | ||
|
||
messageIdPrefix := "request-body-discriminator" | ||
if propertyName != "" { | ||
messageIdPrefix = "request-property-discriminator" | ||
} | ||
|
||
if discriminatorDiff.Added { | ||
if propertyName == "" { | ||
appendResultItem(messageIdPrefix + "-added") | ||
} else { | ||
appendResultItem(messageIdPrefix+"-added", ColorizedValue(propertyName)) | ||
} | ||
return | ||
} | ||
if discriminatorDiff.Deleted { | ||
if propertyName == "" { | ||
appendResultItem(messageIdPrefix + "-removed") | ||
} else { | ||
appendResultItem(messageIdPrefix+"-removed", ColorizedValue(propertyName)) | ||
} | ||
return | ||
} | ||
|
||
if discriminatorDiff.PropertyNameDiff != nil { | ||
if propertyName == "" { | ||
appendResultItem(messageIdPrefix+"-property-name-changed", | ||
ColorizedValue(discriminatorDiff.PropertyNameDiff.From), | ||
ColorizedValue(discriminatorDiff.PropertyNameDiff.To)) | ||
} else { | ||
appendResultItem(messageIdPrefix+"-property-name-changed", | ||
ColorizedValue(propertyName), | ||
ColorizedValue(discriminatorDiff.PropertyNameDiff.From), | ||
ColorizedValue(discriminatorDiff.PropertyNameDiff.To)) | ||
} | ||
} | ||
|
||
if discriminatorDiff.MappingDiff != nil { | ||
if len(discriminatorDiff.MappingDiff.Added) > 0 { | ||
if propertyName == "" { | ||
appendResultItem(messageIdPrefix+"-mapping-added", | ||
ColorizedValue(discriminatorDiff.MappingDiff.Added)) | ||
} else { | ||
appendResultItem(messageIdPrefix+"-mapping-added", | ||
ColorizedValue(discriminatorDiff.MappingDiff.Added), | ||
ColorizedValue(propertyName)) | ||
} | ||
} | ||
|
||
if len(discriminatorDiff.MappingDiff.Deleted) > 0 { | ||
if propertyName == "" { | ||
appendResultItem(messageIdPrefix+"-mapping-deleted", | ||
ColorizedValue(discriminatorDiff.MappingDiff.Deleted)) | ||
} else { | ||
appendResultItem(messageIdPrefix+"-mapping-deleted", | ||
ColorizedValue(discriminatorDiff.MappingDiff.Deleted), | ||
ColorizedValue(propertyName)) | ||
} | ||
} | ||
|
||
for k, v := range discriminatorDiff.MappingDiff.Modified { | ||
if propertyName == "" { | ||
appendResultItem(messageIdPrefix+"-mapping-changed", | ||
ColorizedValue(k), | ||
ColorizedValue(v.From), | ||
ColorizedValue(v.To)) | ||
} else { | ||
appendResultItem(messageIdPrefix+"-mapping-changed", | ||
ColorizedValue(k), | ||
ColorizedValue(v.From), | ||
ColorizedValue(v.To), | ||
ColorizedValue(propertyName)) | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package checker_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tufin/oasdiff/checker" | ||
"github.com/tufin/oasdiff/diff" | ||
) | ||
|
||
// CL: adding discriminator to the request body or request body property | ||
func TestRequestDiscriminatorUpdatedCheckAdded(t *testing.T) { | ||
s1, err := open("../data/checker/request_property_discriminator_added_base.yaml") | ||
require.NoError(t, err) | ||
s2, err := open("../data/checker/request_property_discriminator_added_revision.yaml") | ||
require.NoError(t, err) | ||
|
||
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2) | ||
require.NoError(t, err) | ||
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestDiscriminatorUpdatedCheck), d, osm, checker.INFO) | ||
|
||
require.Len(t, errs, 2) | ||
|
||
require.ElementsMatch(t, []checker.ApiChange{ | ||
{ | ||
Id: "request-body-discriminator-added", | ||
Text: "added request discriminator", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/pets", | ||
Source: "../data/checker/request_property_discriminator_added_revision.yaml", | ||
OperationId: "updatePets", | ||
}, | ||
{ | ||
Id: "request-property-discriminator-added", | ||
Text: "added discriminator to '/oneOf[#/components/schemas/Dog]/breed' request property", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/pets", | ||
Source: "../data/checker/request_property_discriminator_added_revision.yaml", | ||
OperationId: "updatePets", | ||
}}, errs) | ||
} | ||
|
||
// CL: removing discriminator from the request body or request body property | ||
func TestRequestDiscriminatorUpdatedCheckRemoved(t *testing.T) { | ||
s1, err := open("../data/checker/request_property_discriminator_added_revision.yaml") | ||
require.NoError(t, err) | ||
s2, err := open("../data/checker/request_property_discriminator_added_base.yaml") | ||
require.NoError(t, err) | ||
|
||
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2) | ||
require.NoError(t, err) | ||
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestDiscriminatorUpdatedCheck), d, osm, checker.INFO) | ||
|
||
require.Len(t, errs, 2) | ||
|
||
require.ElementsMatch(t, []checker.ApiChange{ | ||
{ | ||
Id: "request-body-discriminator-removed", | ||
Text: "removed request discriminator", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/pets", | ||
Source: "../data/checker/request_property_discriminator_added_base.yaml", | ||
OperationId: "updatePets", | ||
}, | ||
{ | ||
Id: "request-property-discriminator-removed", | ||
Text: "removed discriminator from '/oneOf[#/components/schemas/Dog]/breed' request property", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/pets", | ||
Source: "../data/checker/request_property_discriminator_added_base.yaml", | ||
OperationId: "updatePets", | ||
}}, errs) | ||
} | ||
|
||
// CL: changing discriminator propertyName in the request body or request body property | ||
func TestRequestDiscriminatorUpdatedCheckPropertyNameChanging(t *testing.T) { | ||
s1, err := open("../data/checker/request_property_discriminator_added_revision.yaml") | ||
require.NoError(t, err) | ||
s2, err := open("../data/checker/request_property_discriminator_added_property_name_changed.yaml") | ||
require.NoError(t, err) | ||
|
||
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2) | ||
require.NoError(t, err) | ||
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestDiscriminatorUpdatedCheck), d, osm, checker.INFO) | ||
|
||
require.Len(t, errs, 2) | ||
|
||
require.ElementsMatch(t, []checker.ApiChange{ | ||
{ | ||
Id: "request-body-discriminator-property-name-changed", | ||
Text: "request discriminator property name changed from 'petType' to 'petType2'", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/pets", | ||
Source: "../data/checker/request_property_discriminator_added_property_name_changed.yaml", | ||
OperationId: "updatePets", | ||
}, | ||
{ | ||
Id: "request-property-discriminator-property-name-changed", | ||
Text: "request discriminator property name changed for '/oneOf[#/components/schemas/Dog]/breed' request property from 'name' to 'name2'", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/pets", | ||
Source: "../data/checker/request_property_discriminator_added_property_name_changed.yaml", | ||
OperationId: "updatePets", | ||
}}, errs) | ||
} | ||
|
||
// CL: changing discriminator mapping in the request body or request body property | ||
func TestRequestDiscriminatorUpdatedCheckMappingChanging(t *testing.T) { | ||
s1, err := open("../data/checker/request_property_discriminator_added_revision.yaml") | ||
require.NoError(t, err) | ||
s2, err := open("../data/checker/request_property_discriminator_mapping_changed.yaml") | ||
require.NoError(t, err) | ||
|
||
d, osm, err := diff.GetWithOperationsSourcesMap(getConfig(), s1, s2) | ||
require.NoError(t, err) | ||
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestDiscriminatorUpdatedCheck), d, osm, checker.INFO) | ||
|
||
require.Len(t, errs, 5) | ||
|
||
require.ElementsMatch(t, []checker.ApiChange{ | ||
{ | ||
Id: "request-body-discriminator-mapping-added", | ||
Text: "added '[cats]' mapping keys to the request discriminator", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/pets", | ||
Source: "../data/checker/request_property_discriminator_mapping_changed.yaml", | ||
OperationId: "updatePets", | ||
}, | ||
{ | ||
Id: "request-body-discriminator-mapping-deleted", | ||
Text: "removed '[cat]' mapping keys from the request discriminator", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/pets", | ||
Source: "../data/checker/request_property_discriminator_mapping_changed.yaml", | ||
OperationId: "updatePets", | ||
}, | ||
{ | ||
Id: "request-property-discriminator-mapping-added", | ||
Text: "added '[breed1Code]' discriminator mapping keys to the '/oneOf[#/components/schemas/Dog]/breed' request property", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/pets", | ||
Source: "../data/checker/request_property_discriminator_mapping_changed.yaml", | ||
OperationId: "updatePets", | ||
}, | ||
{ | ||
Id: "request-property-discriminator-mapping-changed", | ||
Text: "mapped value for discriminator key 'breed2' changed from '#/components/schemas/Breed2' to '#/components/schemas/Breed3' for '/oneOf[#/components/schemas/Dog]/breed' request property", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/pets", | ||
Source: "../data/checker/request_property_discriminator_mapping_changed.yaml", | ||
OperationId: "updatePets", | ||
}, | ||
{ | ||
Id: "request-property-discriminator-mapping-deleted", | ||
Text: "removed '[breed1]' discriminator mapping keys from the '/oneOf[#/components/schemas/Dog]/breed' request property", | ||
Comment: "", | ||
Level: checker.INFO, | ||
Operation: "POST", | ||
Path: "/pets", | ||
Source: "../data/checker/request_property_discriminator_mapping_changed.yaml", | ||
OperationId: "updatePets", | ||
}}, errs) | ||
} |
Oops, something went wrong.