Skip to content

Commit

Permalink
CLOUDP-284014: Check soa migration in external path (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
blva authored Nov 12, 2024
1 parent 241e16e commit a1c638f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion tools/cli/internal/openapi/oasdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (o OasDiff) mergePaths() error {
if originalPathData := basePaths.Value(path); originalPathData == nil {
basePaths.Set(path, removeExternalRefs(externalPathData))
} else {
if err := o.handlePathConflict(originalPathData, path); err != nil {
if err := o.handlePathConflict(externalPathData, path); err != nil {
return err
}
basePaths.Set(path, removeExternalRefs(externalPathData))
Expand Down
34 changes: 17 additions & 17 deletions tools/cli/internal/openapi/oasdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestOasDiff_mergePaths(t *testing.T) {
wantErr: require.NoError,
},
{
name: "SuccessfulMergeWithEmptyBasePaths",
name: "SuccessfulMergeWithEmptyexternalPaths",
inputBase: &load.SpecInfo{
Url: "base",
Spec: &openapi3.T{
Expand Down Expand Up @@ -1152,15 +1152,15 @@ func TestUpdateExternalRefReqBody(t *testing.T) {

func TestHandlePathConflict(t *testing.T) {
testCases := []struct {
name string
basePath *openapi3.PathItem
basePathName string
specDiff *diff.Diff
expectedError error
name string
externalPath *openapi3.PathItem
externalPathName string
specDiff *diff.Diff
expectedError error
}{
{
name: "No Conflict - Identical Paths",
basePath: &openapi3.PathItem{
externalPath: &openapi3.PathItem{
Get: &openapi3.Operation{
Extensions: map[string]interface{}{
"x-xgen-soa-migration": map[string]interface{}{
Expand All @@ -1169,7 +1169,7 @@ func TestHandlePathConflict(t *testing.T) {
},
},
},
basePathName: "/test",
externalPathName: "/test",
specDiff: &diff.Diff{
PathsDiff: &diff.PathsDiff{
Modified: map[string]*diff.PathDiff{},
Expand All @@ -1179,7 +1179,7 @@ func TestHandlePathConflict(t *testing.T) {
},
{
name: "Conflict with AllowDocsDiff",
basePath: &openapi3.PathItem{
externalPath: &openapi3.PathItem{
Get: &openapi3.Operation{
Extensions: map[string]interface{}{
"x-xgen-soa-migration": map[string]interface{}{
Expand All @@ -1188,7 +1188,7 @@ func TestHandlePathConflict(t *testing.T) {
},
},
},
basePathName: "/test",
externalPathName: "/test",
specDiff: &diff.Diff{
PathsDiff: &diff.PathsDiff{
Modified: map[string]*diff.PathDiff{
Expand All @@ -1212,7 +1212,7 @@ func TestHandlePathConflict(t *testing.T) {
},
{
name: "Conflict with Different Operations",
basePath: &openapi3.PathItem{
externalPath: &openapi3.PathItem{
Get: &openapi3.Operation{
Extensions: map[string]interface{}{
"x-xgen-soa-migration": map[string]interface{}{
Expand All @@ -1221,7 +1221,7 @@ func TestHandlePathConflict(t *testing.T) {
},
},
},
basePathName: "/test",
externalPathName: "/test",
specDiff: &diff.Diff{
PathsDiff: &diff.PathsDiff{
Modified: map[string]*diff.PathDiff{
Expand All @@ -1240,7 +1240,7 @@ func TestHandlePathConflict(t *testing.T) {
},
{
name: "Conflict with Different Path Operation",
basePath: &openapi3.PathItem{
externalPath: &openapi3.PathItem{
Get: &openapi3.Operation{
Extensions: map[string]interface{}{
"x-xgen-soa-migration": map[string]interface{}{
Expand All @@ -1249,7 +1249,7 @@ func TestHandlePathConflict(t *testing.T) {
},
},
},
basePathName: "/test",
externalPathName: "/test",
specDiff: &diff.Diff{
PathsDiff: &diff.PathsDiff{
Modified: map[string]*diff.PathDiff{
Expand Down Expand Up @@ -1290,7 +1290,7 @@ func TestHandlePathConflict(t *testing.T) {
},
{
name: "Identical Paths with Extensions",
basePath: &openapi3.PathItem{
externalPath: &openapi3.PathItem{
Get: &openapi3.Operation{
Extensions: map[string]interface{}{
"x-xgen-soa-migration": map[string]interface{}{
Expand All @@ -1299,7 +1299,7 @@ func TestHandlePathConflict(t *testing.T) {
},
},
},
basePathName: "/test",
externalPathName: "/test",
specDiff: &diff.Diff{
PathsDiff: &diff.PathsDiff{
Modified: map[string]*diff.PathDiff{},
Expand Down Expand Up @@ -1342,7 +1342,7 @@ func TestHandlePathConflict(t *testing.T) {
Return(tc.specDiff, nil).
AnyTimes()

err := o.handlePathConflict(tc.basePath, tc.basePathName)
err := o.handlePathConflict(tc.externalPath, tc.externalPathName)
if tc.expectedError != nil {
require.Error(t, err)
assert.IsType(t, tc.expectedError, err)
Expand Down
50 changes: 25 additions & 25 deletions tools/cli/internal/openapi/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,42 @@ const (
)

// allOperationsHaveExtension checks if all the operations in the base path have the given extension name.
func allOperationsHaveExtension(basePath *openapi3.PathItem, basePathName, extensionName string) bool {
if basePath.Operations() == nil || len(basePath.Operations()) == 0 {
func allOperationsHaveExtension(pathData *openapi3.PathItem, path, extensionName string) bool {
if pathData.Operations() == nil || len(pathData.Operations()) == 0 {
return false
}

if basePath.Get != nil {
if result := getOperationExtensionWithName(basePath.Get, extensionName); result == nil {
if pathData.Get != nil {
if result := getOperationExtensionWithName(pathData.Get, extensionName); result == nil {
return false
}
}

if basePath.Put != nil {
if result := getOperationExtensionWithName(basePath.Put, extensionName); result == nil {
if pathData.Put != nil {
if result := getOperationExtensionWithName(pathData.Put, extensionName); result == nil {
return false
}
}

if basePath.Post != nil {
if result := getOperationExtensionWithName(basePath.Post, extensionName); result == nil {
if pathData.Post != nil {
if result := getOperationExtensionWithName(pathData.Post, extensionName); result == nil {
return false
}
}

if basePath.Patch != nil {
if result := getOperationExtensionWithName(basePath.Patch, extensionName); result == nil {
if pathData.Patch != nil {
if result := getOperationExtensionWithName(pathData.Patch, extensionName); result == nil {
return false
}
}

if basePath.Delete != nil {
if result := getOperationExtensionWithName(basePath.Delete, extensionName); result == nil {
if pathData.Delete != nil {
if result := getOperationExtensionWithName(pathData.Delete, extensionName); result == nil {
return false
}
}

log.Printf("Detected %s annotation in all operations for path: %s\n", extensionName, basePathName)
log.Printf("Detected %s annotation in all operations for path: %s\n", extensionName, path)
return true
}

Expand All @@ -74,41 +74,41 @@ func getOperationExtensionWithName(operation *openapi3.Operation, extensionName
return operation.Extensions[extensionName]
}

func allOperationsAllowDocsDiff(basePath *openapi3.PathItem) bool {
if basePath.Operations() == nil || len(basePath.Operations()) == 0 {
func allOperationsAllowDocsDiff(pathData *openapi3.PathItem) bool {
if pathData.Operations() == nil || len(pathData.Operations()) == 0 {
return false
}

if basePath.Get != nil {
prop := getOperationExtensionProperty(basePath.Get, xgenSoaMigration, allowDocsDiff)
if pathData.Get != nil {
prop := getOperationExtensionProperty(pathData.Get, xgenSoaMigration, allowDocsDiff)
if prop != "true" {
return false
}
}

if basePath.Put != nil {
prop := getOperationExtensionProperty(basePath.Put, xgenSoaMigration, allowDocsDiff)
if pathData.Put != nil {
prop := getOperationExtensionProperty(pathData.Put, xgenSoaMigration, allowDocsDiff)
if prop != "true" {
return false
}
}

if basePath.Post != nil {
prop := getOperationExtensionProperty(basePath.Post, xgenSoaMigration, allowDocsDiff)
if pathData.Post != nil {
prop := getOperationExtensionProperty(pathData.Post, xgenSoaMigration, allowDocsDiff)
if prop != "true" {
return false
}
}

if basePath.Patch != nil {
prop := getOperationExtensionProperty(basePath.Patch, xgenSoaMigration, allowDocsDiff)
if pathData.Patch != nil {
prop := getOperationExtensionProperty(pathData.Patch, xgenSoaMigration, allowDocsDiff)
if prop != "true" {
return false
}
}

if basePath.Delete != nil {
prop := getOperationExtensionProperty(basePath.Delete, xgenSoaMigration, allowDocsDiff)
if pathData.Delete != nil {
prop := getOperationExtensionProperty(pathData.Delete, xgenSoaMigration, allowDocsDiff)
if prop != "true" {
return false
}
Expand Down

0 comments on commit a1c638f

Please sign in to comment.