Skip to content

Commit 6141268

Browse files
authored
Merge pull request #8 from paulRbr/array-item-removal
remove: improve removal of array items
2 parents 2496500 + 644718a commit 6141268

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

src/overlay.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ function applyOverlayToOpenAPI(spec, overlay) {
1515
}
1616
var parent = jsonpath.parent(spec, a.target)
1717
const thingToRemove = path[0][path[0].length - 1]
18-
delete parent[thingToRemove]
18+
if (Array.isArray(parent)) {
19+
parent.splice(thingToRemove, 1);
20+
} else {
21+
delete parent[thingToRemove];
22+
}
1923
}
2024

2125
} else {

test/expected/one-less-server.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
openapi: 3.1.0
2+
info:
3+
title: API servers
4+
version: 1.0.0
5+
servers:
6+
- url: 'https://api.example.com'
7+
description: Production
8+
paths: {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
openapi: 3.1.0
2+
info:
3+
title: API servers
4+
version: 1.0.0
5+
servers:
6+
- url: 'https://api.dev.example.com'
7+
description: Dev
8+
- url: 'https://api.example.com'
9+
description: Production
10+
paths: {}

test/overlay.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ test('remove all description fields', () => {
5959
expect(result).toEqual(expectedOutput);
6060
});
6161

62+
test('remove a server array entry', () => {
63+
const openapiFile = "test/openapi/openapi-with-servers.yaml";
64+
const overlayFile = "test/overlays/remove-server.yaml";
65+
const expectedFile = "test/expected/one-less-server.yaml";
66+
const expectedOutput = fs.readFileSync(expectedFile, 'utf8');
67+
68+
const result = overlayFiles(openapiFile, overlayFile);
69+
70+
expect(result).toEqual(expectedOutput);
71+
});
72+
6273
test('fail to update a primitive string type', () => {
6374
const openapiFile = "test/openapi/immutable.yaml";
6475
const overlayFile = "test/overlays/immutable.yaml";

test/overlays/remove-server.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
overlay: 1.0.0
2+
info:
3+
title: Remove dev server
4+
version: 1.0.0
5+
extends: openapi-with-servers.yaml
6+
7+
actions:
8+
- target: $.servers[?( @.description == 'Dev' )]
9+
remove: true

0 commit comments

Comments
 (0)