|
15 | 15 | package openapi |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "encoding/json" |
19 | 18 | "log" |
20 | 19 | "slices" |
21 | 20 | "strings" |
@@ -122,11 +121,7 @@ func (o OasDiff) mergePaths() error { |
122 | 121 | if err != nil { |
123 | 122 | return err |
124 | 123 | } |
125 | | - |
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 | | - } |
| 124 | + basePaths.Set(path, removeExternalRefs(externalPathData)) |
130 | 125 | } |
131 | 126 | } |
132 | 127 | o.base.Spec.Paths = basePaths |
@@ -176,19 +171,33 @@ func (o OasDiff) handlePathConflict(basePath *openapi3.PathItem, basePathName st |
176 | 171 | } |
177 | 172 | } |
178 | 173 |
|
| 174 | + var pathsAreIdentical bool |
| 175 | + var err error |
| 176 | + if pathsAreIdentical, err = o.arePathsIdenticalWithExcludeExtensions(basePathName); err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + |
179 | 180 | log.Printf("Skipping conflict for path: %s", basePathName) |
180 | | - if o.arePathsIdenticalWithExcludeExtensions(basePathName) { |
181 | | - log.Printf("No doc diff detected for path %s, merging the paths", basePathName) |
| 181 | + if pathsAreIdentical { |
182 | 182 | return nil |
183 | 183 | } |
184 | 184 |
|
185 | | - if !allOperationsAllowDocsDiff(basePath, basePathName) { |
186 | | - log.Printf("Doc diff detected failing as allowDocsDiff=true is not supported.") |
187 | | - return errors.PathConflictError{ |
| 185 | + // allowDocsDiff = true not supported |
| 186 | + if allOperationsAllowDocsDiff(basePath) { |
| 187 | + return errors.AllowDocsDiffNotSupportedError{ |
188 | 188 | Entry: basePathName, |
189 | 189 | } |
190 | 190 | } |
191 | | - return nil |
| 191 | + |
| 192 | + d, err := o.getDiffWithoutExtensions() |
| 193 | + if err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + |
| 197 | + return errors.PathDocsDiffConflictError{ |
| 198 | + Entry: basePathName, |
| 199 | + Diff: d, |
| 200 | + } |
192 | 201 | } |
193 | 202 |
|
194 | 203 | // shouldSkipConflict checks if the conflict should be skipped. |
@@ -475,24 +484,24 @@ func (o OasDiff) areSchemaIdentical(name string) bool { |
475 | 484 | } |
476 | 485 |
|
477 | 486 | // arePathsIdenticalWithExcludeExtensions checks if the paths are identical with the extensions excluded |
478 | | -func (o OasDiff) arePathsIdenticalWithExcludeExtensions(name string) bool { |
| 487 | +func (o OasDiff) arePathsIdenticalWithExcludeExtensions(name string) (bool, error) { |
479 | 488 | // If the diff only has extensions diff, then we consider the paths to be identical |
480 | | - exclude := []string{"extensions"} |
481 | | - customConfig := diff.NewConfig().WithExcludeElements(exclude) |
482 | | - d, err := diff.Get(customConfig, o.base.Spec, o.external.Spec) |
| 489 | + d, err := o.getDiffWithoutExtensions() |
483 | 490 | if err != nil { |
484 | | - log.Fatalf("error in calculating the diff of the specs: %s", err) |
| 491 | + return false, err |
485 | 492 | } |
486 | 493 |
|
487 | 494 | if d.Empty() || d.PathsDiff.Empty() { |
488 | | - return true |
| 495 | + return true, nil |
489 | 496 | } |
490 | 497 | _, ok := d.PathsDiff.Modified[name] |
491 | | - if ok { |
492 | | - j, _ := json.MarshalIndent(d.PathsDiff.Modified[name], "", " ") |
493 | | - log.Printf("arePathsIdenticalWithExcludeExtensions diff: %s", j) |
494 | | - } |
495 | | - return !ok |
| 498 | + return !ok, nil |
| 499 | +} |
| 500 | + |
| 501 | +func (o OasDiff) getDiffWithoutExtensions() (*diff.Diff, error) { |
| 502 | + exclude := []string{"extensions"} |
| 503 | + customConfig := diff.NewConfig().WithExcludeElements(exclude) |
| 504 | + return diff.Get(customConfig, o.base.Spec, o.external.Spec) |
496 | 505 | } |
497 | 506 |
|
498 | 507 | type ByName []*openapi3.Tag |
|
0 commit comments