Skip to content

Commit 4906ea6

Browse files
authored
Merge pull request #693 from paulRbr/fix-overlay-remove-action
fix(overlay): fix removal of elements of an array
2 parents ce8b6f6 + 3063c43 commit 4906ea6

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

examples/valid/openapi.v3.json

+6
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@
252252
"servers": [
253253
{
254254
"url": "https://bump.sh/api/v1"
255+
},
256+
{
257+
"url": "https://staging.bump.sh/api/v1"
258+
},
259+
{
260+
"url": "http://localhost:3000/api/v1"
255261
}
256262
],
257263
"tags": [

src/core/overlay.ts

+20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ export class Overlay {
4343
continue
4444
}
4545

46+
// When we execute a 'remove' action we need to be careful if
47+
// the targets are elements of an array. Because the list of
48+
// paths contains an index of each element.
49+
//
50+
// E.g.
51+
// ["servers"][0]
52+
// ["servers"][1]
53+
// ["servers"][2]
54+
//
55+
// And if we remove elements starting from the 0, the array
56+
// will be reindexed and thus the '2' index won't be valid
57+
// anymore.
58+
//
59+
// Thus, in that case we revers the list of paths
60+
if (action.remove) {
61+
paths.reverse()
62+
}
63+
4664
for (const path of paths) {
4765
// The 'executeAction' will mutate the passed spec object in
4866
// place.
@@ -83,8 +101,10 @@ export class Overlay {
83101
// Do the overlay action
84102
// Is it a remove?
85103
if (Object.hasOwn(action, 'remove')) {
104+
this.d(`Executing 'remove' on target path: ${path}`)
86105
this.remove(parent, thingToActUpon)
87106
} else if (Object.hasOwn(action, 'update')) {
107+
this.d(`Executing 'update' on target path: ${path}`)
88108
spec = this.update(spec, parent, action.update, thingToActUpon)
89109
} else {
90110
process.stderr.write(`WARNING: ${this.humanName(action)} needs either a 'remove' or an 'update' property\n`)

0 commit comments

Comments
 (0)