Skip to content

Commit e46c9ac

Browse files
committed
Handle removing an element, rather than just emptying, with tests
1 parent 15489c2 commit e46c9ac

File tree

5 files changed

+69
-14
lines changed

5 files changed

+69
-14
lines changed

src/overlay.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,36 @@ import mergician from 'mergician';
66
function applyOverlayToOpenAPI(spec, overlay) {
77
// Use jsonpath.apply to do the changes
88
overlay.actions.forEach((a)=>{
9-
jsonpath.apply(spec, a.target, (chunk) => {
10-
// Is it a remove?
11-
if (a.hasOwnProperty('remove')) {
12-
return {};
9+
// Is it a remove?
10+
if (a.hasOwnProperty('remove')) {
11+
// split the path expression
12+
var target_pieces = jsonpath.parse(a.target);
13+
// snag the last piece, we need this info to work out which element to remove
14+
var final_segment = target_pieces.pop();
15+
var target_key = "";
16+
if(final_segment.expression.type == "identifier") {
17+
target_key = final_segment.expression.value;
1318
}
1419

15-
// It must be an update
20+
// Now rebuild the path up to before the final bit
21+
var remaining_path = jsonpath.stringify(target_pieces);
1622

17-
// Deep merge using a module (built-in spread operator is only shallow)
18-
const merger = mergician({appendArrays: true});
19-
console.debug(a);
20-
const merged = merger(chunk, a.update);
21-
return merged;
22-
23-
});
23+
// get the parent node and remove the target element
24+
var node_value = jsonpath.value(spec, remaining_path);
25+
delete node_value[target_key];
26+
27+
} else {
28+
// It must be an update
29+
jsonpath.apply(spec, a.target, (chunk) => {
30+
31+
// Deep merge using a module (built-in spread operator is only shallow)
32+
const merger = mergician({appendArrays: true});
33+
console.debug(a);
34+
const merged = merger(chunk, a.update);
35+
return merged;
36+
37+
});
38+
}
2439
})
2540

2641
return spec;

test/expected/output1.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ servers:
99
paths:
1010
/pets:
1111
get:
12-
summary: {}
1312
operationId: listPets
1413
tags:
1514
- pets
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: 3.1.0
2+
info:
3+
version: 1.0.0
4+
title: Imaginary town
5+
servers:
6+
- url: 'https://example.com'
7+
description: Example server
8+
paths:
9+
/buildings:
10+
get:
11+
summary: All buildings
12+
operationId: buildingsAll
13+
responses:
14+
'200':
15+
description: Return all known buildings
16+
content:
17+
application/json:
18+
schema:
19+
type: object
20+
properties:
21+
message:
22+
type: string

test/overlay.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test('apply an overlay and check the output', () => {
1313
expect(result).toEqual(expectedOutput);
1414
});
1515

16-
test('apply an overlay and check the output', () => {
16+
test('add a description and update the summary', () => {
1717
const openapiFile = "test/openapi/town.yaml";
1818
const overlayFile = "test/overlays/building-description.yaml";
1919
const expectedFile = "test/expected/town-building-description.yaml";
@@ -25,3 +25,15 @@ test('apply an overlay and check the output', () => {
2525
});
2626

2727

28+
test('remove an example', () => {
29+
const openapiFile = "test/openapi/town.yaml";
30+
const overlayFile = "test/overlays/remove-example.yaml";
31+
const expectedFile = "test/expected/town-remove-example.yaml";
32+
const expectedOutput = fs.readFileSync(expectedFile, 'utf8');
33+
34+
const result = overlayFiles(openapiFile, overlayFile);
35+
36+
expect(result).toEqual(expectedOutput);
37+
});
38+
39+

test/overlays/remove-example.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
overlay: 1.0.0
2+
info:
3+
title: Remove an example
4+
version: 1.0.0
5+
actions:
6+
- target: $.paths['/buildings'].get.responses['200'].content['application/json'].schema.properties['message'].example
7+
remove: true

0 commit comments

Comments
 (0)