Skip to content

Commit 319d55f

Browse files
authored
Merge pull request #1 from lornajane/removal-improval
Removal improval
2 parents 629bc29 + 7b0506f commit 319d55f

11 files changed

+343
-40
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-building-description.yaml

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,63 @@ paths:
99
/buildings:
1010
get:
1111
summary: All of the available buildings
12-
operationId: buildingsAll
13-
description: This is the summary of getting the buildings
12+
operationId: buildingsList
13+
description: This is the summary of getting the buildings
1414
responses:
1515
'200':
1616
description: Return all known buildings
1717
content:
1818
application/json:
1919
schema:
20-
type: object
21-
properties:
22-
message:
23-
type: string
24-
example: house
25-
20+
type: array
21+
items:
22+
$ref: '#/components/schemas/Building'
23+
'/buildings/{buildingId}':
24+
get:
25+
summary: Specific building
26+
operationId: buildingById
27+
parameters:
28+
- name: buildingId
29+
in: path
30+
required: true
31+
description: Which building to return
32+
schema:
33+
type: string
34+
responses:
35+
'200':
36+
description: Return a building
37+
content:
38+
application/json:
39+
schema:
40+
$ref: '#/components/schemas/Building'
41+
/locations:
42+
get:
43+
summary: All locations
44+
operationId: locationList
45+
responses:
46+
'200':
47+
description: Returns all locations
48+
content:
49+
application/json:
50+
schema:
51+
type: array
52+
items:
53+
type: object
54+
properties:
55+
location_id:
56+
type: int
57+
example: 44
58+
name:
59+
type: string
60+
example: North Village
61+
components:
62+
schemas:
63+
Building:
64+
type: object
65+
properties:
66+
building:
67+
type: string
68+
example: house
69+
location_id:
70+
type: int
71+
example: 44
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
openapi: 3.1.0
2+
info:
3+
version: 1.0.0
4+
title: Imaginary town
5+
servers:
6+
- url: 'https://example.com'
7+
paths:
8+
/buildings:
9+
get:
10+
summary: All buildings
11+
operationId: buildingsList
12+
responses:
13+
'200':
14+
content:
15+
application/json:
16+
schema:
17+
type: array
18+
items:
19+
$ref: '#/components/schemas/Building'
20+
'/buildings/{buildingId}':
21+
get:
22+
summary: Specific building
23+
operationId: buildingById
24+
parameters:
25+
- name: buildingId
26+
in: path
27+
required: true
28+
schema:
29+
type: string
30+
responses:
31+
'200':
32+
content:
33+
application/json:
34+
schema:
35+
$ref: '#/components/schemas/Building'
36+
/locations:
37+
get:
38+
summary: All locations
39+
operationId: locationList
40+
responses:
41+
'200':
42+
content:
43+
application/json:
44+
schema:
45+
type: array
46+
items:
47+
type: object
48+
properties:
49+
location_id:
50+
type: int
51+
example: 44
52+
name:
53+
type: string
54+
example: North Village
55+
components:
56+
schemas:
57+
Building:
58+
type: object
59+
properties:
60+
building:
61+
type: string
62+
example: house
63+
location_id:
64+
type: int
65+
example: 44

test/expected/town-remove-example.yaml

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,61 @@ paths:
99
/buildings:
1010
get:
1111
summary: All buildings
12-
operationId: buildingsAll
12+
operationId: buildingsList
1313
responses:
1414
'200':
1515
description: Return all known buildings
1616
content:
1717
application/json:
1818
schema:
19-
type: object
20-
properties:
21-
message:
22-
type: string
19+
type: array
20+
items:
21+
$ref: '#/components/schemas/Building'
22+
'/buildings/{buildingId}':
23+
get:
24+
summary: Specific building
25+
operationId: buildingById
26+
parameters:
27+
- name: buildingId
28+
in: path
29+
required: true
30+
description: Which building to return
31+
schema:
32+
type: string
33+
responses:
34+
'200':
35+
description: Return a building
36+
content:
37+
application/json:
38+
schema:
39+
$ref: '#/components/schemas/Building'
40+
/locations:
41+
get:
42+
summary: All locations
43+
operationId: locationList
44+
responses:
45+
'200':
46+
description: Returns all locations
47+
content:
48+
application/json:
49+
schema:
50+
type: array
51+
items:
52+
type: object
53+
properties:
54+
location_id:
55+
type: int
56+
example: 44
57+
name:
58+
type: string
59+
example: North Village
60+
components:
61+
schemas:
62+
Building:
63+
type: object
64+
properties:
65+
building:
66+
type: string
67+
location_id:
68+
type: int
69+
example: 44
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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: buildingsList
13+
responses:
14+
'200':
15+
description: Return all known buildings
16+
content:
17+
application/json:
18+
schema:
19+
type: array
20+
items:
21+
$ref: '#/components/schemas/Building'
22+
'/buildings/{buildingId}':
23+
get:
24+
summary: Specific building
25+
operationId: buildingById
26+
parameters:
27+
- name: buildingId
28+
in: path
29+
required: true
30+
description: Which building to return
31+
schema:
32+
type: string
33+
responses:
34+
'200':
35+
description: Return a building
36+
content:
37+
application/json:
38+
schema:
39+
$ref: '#/components/schemas/Building'
40+
/locations:
41+
get:
42+
summary: All locations
43+
operationId: locationList
44+
responses:
45+
'200':
46+
description: Returns all locations
47+
content:
48+
application/json:
49+
schema:
50+
type: array
51+
items:
52+
type: object
53+
properties:
54+
location_id:
55+
type: int
56+
example: 44
57+
components:
58+
schemas:
59+
Building:
60+
type: object
61+
properties:
62+
building:
63+
type: string
64+
example: house
65+
location_id:
66+
type: int
67+
example: 44

test/openapi/town.yaml

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,66 @@ servers:
88
paths:
99
'/buildings':
1010
get:
11-
summary: All buildings
12-
operationId: buildingsAll
11+
summary: All buildings
12+
operationId: buildingsList
1313
responses:
1414
'200':
1515
description: Return all known buildings
1616
content:
1717
application/json:
1818
schema:
19-
type: object
20-
properties:
21-
message:
22-
type: string
23-
example: "house"
19+
type: array
20+
items:
21+
$ref: "#/components/schemas/Building"
2422

23+
'/buildings/{buildingId}':
24+
get:
25+
summary: Specific building
26+
operationId: buildingById
27+
parameters:
28+
- name: buildingId
29+
in: path
30+
required: true
31+
description: Which building to return
32+
schema:
33+
type: string
34+
responses:
35+
'200':
36+
description: Return a building
37+
content:
38+
application/json:
39+
schema:
40+
$ref: "#/components/schemas/Building"
41+
42+
'/locations':
43+
get:
44+
summary: All locations
45+
operationId: locationList
46+
responses:
47+
'200':
48+
description: Returns all locations
49+
content:
50+
application/json:
51+
schema:
52+
type: array
53+
items:
54+
type: object
55+
properties:
56+
location_id:
57+
type: int
58+
example: 44
59+
name:
60+
type: string
61+
example: "North Village"
62+
63+
components:
64+
schemas:
65+
Building:
66+
type: object
67+
properties:
68+
building:
69+
type: string
70+
example: "house"
71+
location_id:
72+
type: int
73+
example: 44

0 commit comments

Comments
 (0)