Skip to content

Commit dd500c8

Browse files
committed
update
1 parent ef3b6e6 commit dd500c8

File tree

2 files changed

+86
-14
lines changed

2 files changed

+86
-14
lines changed

tools/cli/internal/openapi/oasdiff.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,20 @@ func (o OasDiff) mergePaths() error {
113113
return nil
114114
}
115115

116-
for externalPath, externalPathData := range pathsToMerge.Map() {
116+
for path, externalPathData := range pathsToMerge.Map() {
117117
// Tries to find if the path already exists or not
118-
if originalPath := basePaths.Value(externalPath); originalPath == nil {
119-
basePaths.Set(externalPath, removeExternalRefs(externalPathData))
118+
if originalPathData := basePaths.Value(path); originalPathData == nil {
119+
basePaths.Set(path, removeExternalRefs(externalPathData))
120120
} else {
121-
err := o.handlePathConflict(originalPath, externalPath)
121+
err := o.handlePathConflict(originalPathData, path)
122122
if err != nil {
123123
return err
124124
}
125125

126-
basePaths.Set(externalPath, removeExternalRefs(externalPathData))
126+
// if diff is not allowed and there is a diff, then fail
127+
if !allOperationsAllowDocsDiff(originalPathData, path) {
128+
basePaths.Set(path, removeExternalRefs(externalPathData))
129+
}
127130
}
128131
}
129132
o.base.Spec.Paths = basePaths
@@ -179,10 +182,13 @@ func (o OasDiff) handlePathConflict(basePath *openapi3.PathItem, basePathName st
179182
return nil
180183
}
181184

182-
log.Printf("Doc diff detected failing as allowDocsDiff=true is not supported.")
183-
return errors.PathConflictError{
184-
Entry: basePathName,
185+
if !allOperationsAllowDocsDiff(basePath, basePathName) {
186+
log.Printf("Doc diff detected failing as allowDocsDiff=true is not supported.")
187+
return errors.PathConflictError{
188+
Entry: basePathName,
189+
}
185190
}
191+
return nil
186192
}
187193

188194
// shouldSkipConflict checks if the conflict should be skipped.

tools/cli/internal/openapi/paths.go

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,106 @@ import (
2222

2323
const (
2424
xgenSoaMigration = "x-xgen-soa-migration"
25+
allowDocsDiff = "allowDocsDiff"
2526
)
2627

27-
// allMethodsHaveExtension checks if all the operations in the base pat have the given extension name.
28+
// allOperationsHaveExtension checks if all the operations in the base pat have the given extension name.
2829
func allOperationsHaveExtension(basePath *openapi3.PathItem, basePathName, extensionName string) bool {
2930
if basePath.Get != nil {
30-
if basePath.Get.Extensions == nil || basePath.Get.Extensions[extensionName] == nil {
31+
if result := getOperationExtensionWithName(basePath.Get, extensionName); result == nil {
3132
return false
3233
}
3334
}
3435

3536
if basePath.Put != nil {
36-
if basePath.Put.Extensions == nil || basePath.Put.Extensions[extensionName] == nil {
37+
if result := getOperationExtensionWithName(basePath.Put, extensionName); result == nil {
3738
return false
3839
}
3940
}
4041

4142
if basePath.Post != nil {
42-
if basePath.Post.Extensions == nil || basePath.Post.Extensions[extensionName] == nil {
43+
if result := getOperationExtensionWithName(basePath.Post, extensionName); result == nil {
4344
return false
4445
}
4546
}
4647

4748
if basePath.Patch != nil {
48-
if basePath.Patch.Extensions == nil || basePath.Patch.Extensions[extensionName] == nil {
49+
if result := getOperationExtensionWithName(basePath.Patch, extensionName); result == nil {
4950
return false
5051
}
5152
}
5253

5354
if basePath.Delete != nil {
54-
if basePath.Delete.Extensions == nil || basePath.Delete.Extensions[extensionName] == nil {
55+
if result := getOperationExtensionWithName(basePath.Delete, extensionName); result == nil {
5556
return false
5657
}
5758
}
5859

5960
log.Println("Detected x-xgen-soa-migration annotation in all operations for path: ", basePathName)
6061
return true
6162
}
63+
64+
func getOperationExtensionWithName(operation *openapi3.Operation, extensionName string) interface{} {
65+
if operation.Extensions == nil || operation.Extensions[extensionName] == nil {
66+
log.Printf("Operation %s does not have extension %q", operation.OperationID, extensionName)
67+
return nil
68+
}
69+
70+
return operation.Extensions[extensionName]
71+
}
72+
73+
func allOperationsAllowDocsDiff(basePath *openapi3.PathItem, basePathName string) bool {
74+
if basePath.Get != nil {
75+
prop := getOperationExtensionProperty(basePath.Get, xgenSoaMigration, allowDocsDiff)
76+
if prop != "true" {
77+
return false
78+
}
79+
}
80+
81+
if basePath.Put != nil {
82+
prop := getOperationExtensionProperty(basePath.Put, xgenSoaMigration, allowDocsDiff)
83+
if prop != "true" {
84+
return false
85+
}
86+
}
87+
88+
if basePath.Post != nil {
89+
prop := getOperationExtensionProperty(basePath.Post, xgenSoaMigration, allowDocsDiff)
90+
if prop != "true" {
91+
return false
92+
}
93+
}
94+
95+
if basePath.Patch != nil {
96+
prop := getOperationExtensionProperty(basePath.Patch, xgenSoaMigration, allowDocsDiff)
97+
if prop != "true" {
98+
return false
99+
}
100+
}
101+
102+
if basePath.Delete != nil {
103+
prop := getOperationExtensionProperty(basePath.Delete, xgenSoaMigration, allowDocsDiff)
104+
if prop != "true" {
105+
return false
106+
}
107+
}
108+
109+
return true
110+
}
111+
112+
func getOperationExtensionProperty(operation *openapi3.Operation, extensionName, extensionProperty string) string {
113+
if operation.Extensions == nil || operation.Extensions[extensionName] == nil {
114+
return ""
115+
}
116+
117+
extension := operation.Extensions[extensionName]
118+
if extension == nil {
119+
return ""
120+
}
121+
122+
value, ok := extension.(map[string]interface{})[extensionProperty].(string)
123+
if ok {
124+
return value
125+
}
126+
return ""
127+
}

0 commit comments

Comments
 (0)