Skip to content

Commit 08becd7

Browse files
magicmonkeylornajane
authored andcommitted
Support multi-node removal
1 parent 629bc29 commit 08becd7

File tree

3 files changed

+10
-19
lines changed

3 files changed

+10
-19
lines changed

src/overlay.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,22 @@ function applyOverlayToOpenAPI(spec, overlay) {
88
overlay.actions.forEach((a)=>{
99
// Is it a remove?
1010
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;
11+
while(true) {
12+
var path = jsonpath.paths(spec, a.target, 1)
13+
if (path.length == 0) {
14+
break
15+
}
16+
var parent = jsonpath.parent(spec, a.target)
17+
const thingToRemove = path[0][path[0].length - 1]
18+
delete parent[thingToRemove]
1819
}
1920

20-
// Now rebuild the path up to before the final bit
21-
var remaining_path = jsonpath.stringify(target_pieces);
22-
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-
2721
} else {
2822
// It must be an update
2923
jsonpath.apply(spec, a.target, (chunk) => {
3024

3125
// Deep merge using a module (built-in spread operator is only shallow)
3226
const merger = mergician({appendArrays: true});
33-
console.debug(a);
3427
const merged = merger(chunk, a.update);
3528
return merged;
3629

@@ -43,8 +36,6 @@ function applyOverlayToOpenAPI(spec, overlay) {
4336

4437
export function overlayFiles(openapiFile, overlayFile) {
4538
// Parse the "input" OpenAPI document
46-
console.debug(openapiFile);
47-
console.debug (typeof openapiFile);
4839
const specraw = fs.readFileSync(openapiFile, 'utf8');
4940
var spec = parseWithPointers(specraw).data;
5041

test/expected/town-remove-example.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ info:
44
title: Imaginary town
55
servers:
66
- url: 'https://example.com'
7-
description: Example server
87
paths:
98
/buildings:
109
get:
1110
summary: All buildings
1211
operationId: buildingsAll
1312
responses:
1413
'200':
15-
description: Return all known buildings
1614
content:
1715
application/json:
1816
schema:

test/overlays/remove-example.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ info:
55
actions:
66
- target: $.paths['/buildings'].get.responses['200'].content['application/json'].schema.properties['message'].example
77
remove: true
8+
- target: $..description
9+
remove: true

0 commit comments

Comments
 (0)