Skip to content

Commit 888bac5

Browse files
authored
Merge pull request #5 from MikeRalphson/mr/tests
New tests and hardening of actions updating
2 parents 7688307 + 71f5663 commit 888bac5

21 files changed

+154
-33
lines changed

src/overlay.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import mergician from 'mergician';
55

66
function applyOverlayToOpenAPI(spec, overlay) {
77
// Use jsonpath.apply to do the changes
8-
overlay.actions.forEach((a)=>{
8+
if (overlay.actions && overlay.actions.length >= 1) overlay.actions.forEach((a)=>{
99
// Is it a remove?
1010
if (a.hasOwnProperty('remove')) {
1111
while(true) {
@@ -19,34 +19,38 @@ function applyOverlayToOpenAPI(spec, overlay) {
1919
}
2020

2121
} else {
22-
// It must be an update
23-
jsonpath.apply(spec, a.target, (chunk) => {
24-
25-
// Deep merge using a module (built-in spread operator is only shallow)
26-
const merger = mergician({appendArrays: true});
27-
const merged = merger(chunk, a.update);
28-
return merged;
22+
try {
23+
// It must be an update
24+
jsonpath.apply(spec, a.target, (chunk) => {
25+
// Deep merge using a module (built-in spread operator is only shallow)
26+
const merger = mergician({appendArrays: true})
27+
return merger(chunk, a.update)
28+
});
29+
}
30+
catch (ex) {
31+
process.stderr.write(`Error applying overlay: ${ex.message}\n`)
32+
//return chunk
33+
}
2934

30-
});
3135
}
3236
})
3337

3438
return spec;
3539
}
3640

3741
function sortOpenAPIFields(field1, field2) {
38-
const orderedKeys = ["info", "servers", "paths", "components", "summary", "objectId", "description", "tags", "parameters", "responses"];
42+
const orderedKeys = ["info", "servers", "summary", "operationId", "tags", "paths", "components", "description", "parameters", "responses"];
3943

40-
const index1 = orderedKeys.indexOf(field1);
41-
const index2 = orderedKeys.indexOf(field2);
44+
const index1 = orderedKeys.indexOf(field1);
45+
const index2 = orderedKeys.indexOf(field2);
4246

43-
if (index1 === -1 || index2 === -1) {
44-
return 0;
45-
} else if (index1 > index2) {
46-
return 1;
47-
} else {
48-
return -1;
49-
}
47+
if (index1 === -1 || index2 === -1) {
48+
return 0;
49+
} else if (index1 > index2) {
50+
return 1;
51+
} else {
52+
return -1;
53+
}
5054
}
5155

5256
export function overlayFiles(openapiFile, overlayFile) {

test/expected/empty.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
openapi: 3.1.0
2+
info:
3+
version: 1.0.0
4+
title: API
5+
tags:
6+
- name: Things
7+
paths: {}

test/expected/immutable.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
openapi: 3.1.0
2+
info:
3+
version: 1.0.0
4+
title: API
5+
paths: {}

test/expected/not-jsonpath.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
openapi: 3.1.0
2+
info:
3+
version: 1.0.0
4+
title: API
5+
tags:
6+
- name: Things
7+
paths: {}

test/expected/not-overlay.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
openapi: 3.1.0
2+
info:
3+
version: 1.0.0
4+
title: API
5+
tags:
6+
- name: Things
7+
paths: {}

test/expected/output1.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ paths:
1010
/pets:
1111
get:
1212
operationId: listPets
13-
info:
14-
x-overlay-applied: structured-overlay
15-
description: This is an added description
1613
tags:
1714
- pets
1815
- wildcard-done
@@ -44,6 +41,9 @@ paths:
4441
application/json:
4542
schema:
4643
$ref: '#/components/schemas/Error'
44+
x-info:
45+
x-overlay-applied: structured-overlay
46+
description: This is an added description
4747
post:
4848
foo:
4949
bar: hello

test/expected/town-building-description.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ paths:
5353
type: object
5454
properties:
5555
location_id:
56-
type: int
56+
type: integer
5757
example: 44
5858
name:
5959
type: string
@@ -67,5 +67,5 @@ components:
6767
type: string
6868
example: house
6969
location_id:
70-
type: int
70+
type: integer
7171
example: 44

test/expected/town-remove-descriptions.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ paths:
4747
type: object
4848
properties:
4949
location_id:
50-
type: int
50+
type: integer
5151
example: 44
5252
name:
5353
type: string
@@ -61,5 +61,5 @@ components:
6161
type: string
6262
example: house
6363
location_id:
64-
type: int
64+
type: integer
6565
example: 44

test/expected/town-remove-example.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ paths:
5252
type: object
5353
properties:
5454
location_id:
55-
type: int
55+
type: integer
5656
example: 44
5757
name:
5858
type: string
@@ -65,5 +65,5 @@ components:
6565
building:
6666
type: string
6767
location_id:
68-
type: int
68+
type: integer
6969
example: 44

test/expected/town-remove-properties.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ paths:
5252
type: object
5353
properties:
5454
location_id:
55-
type: int
55+
type: integer
5656
example: 44
5757
components:
5858
schemas:
@@ -63,5 +63,5 @@ components:
6363
type: string
6464
example: house
6565
location_id:
66-
type: int
66+
type: integer
6767
example: 44

0 commit comments

Comments
 (0)