Skip to content

Commit 445c5b7

Browse files
authored
fix(overlays): copy update object to avoid applying to references (#183)
1 parent 9a6b4d9 commit 445c5b7

5 files changed

Lines changed: 131 additions & 1 deletion

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type: object
2+
properties:
3+
parent:
4+
type: object
5+
properties:
6+
one:
7+
type: object
8+
properties:
9+
child: {}
10+
two:
11+
type: object
12+
properties:
13+
child: {}
14+
three:
15+
type: object
16+
properties:
17+
child: {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
output: output.yaml
2+
overlayFile: overlay.yaml
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
type: object
2+
properties:
3+
parent:
4+
type: object
5+
properties:
6+
one:
7+
type: object
8+
properties:
9+
child:
10+
oneOf:
11+
- type: object
12+
properties:
13+
type:
14+
const: a
15+
a:
16+
type: object
17+
oneOf:
18+
- type: object
19+
properties:
20+
type:
21+
const: c
22+
c:
23+
type: object
24+
- type: object
25+
properties:
26+
type:
27+
const: b
28+
b:
29+
type: object
30+
two:
31+
type: object
32+
properties:
33+
child:
34+
oneOf:
35+
- type: object
36+
properties:
37+
type:
38+
const: a
39+
a:
40+
type: object
41+
oneOf:
42+
- type: object
43+
properties:
44+
type:
45+
const: c
46+
c:
47+
type: object
48+
- type: object
49+
properties:
50+
type:
51+
const: b
52+
b:
53+
type: object
54+
three:
55+
type: object
56+
properties:
57+
child:
58+
oneOf:
59+
- type: object
60+
properties:
61+
type:
62+
const: a
63+
a:
64+
type: object
65+
oneOf:
66+
- type: object
67+
properties:
68+
type:
69+
const: c
70+
c:
71+
type: object
72+
- type: object
73+
properties:
74+
type:
75+
const: b
76+
b:
77+
type: object
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
actions:
2+
3+
# apply an overlay to add an object from this overlay into the original OpenAPI schema
4+
# this should be applied as a copy to each target, not a reference
5+
- target: $..child
6+
update:
7+
oneOf:
8+
- type: object
9+
properties:
10+
type:
11+
const: a
12+
a:
13+
type: object
14+
- type: object
15+
properties:
16+
type:
17+
const: b
18+
b:
19+
type: object
20+
21+
# apply a further overlay to the above overlaid object
22+
# as the above overlaid object should be a copy, we shouldn't keep
23+
# adding the overlay multiple times to the same referenced object
24+
- target: $..child.oneOf[0].properties.a
25+
update:
26+
oneOf:
27+
- type: object
28+
properties:
29+
type:
30+
const: c
31+
c:
32+
type: object

utils/overlay.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ async function openapiOverlay(oaObj, options) {
6060
// Handle update actions
6161
targets.forEach(node => {
6262
if (node.parent && node.key !== undefined) {
63-
node.parent[node.key] = deepMerge(node.value, update);
63+
// make a copy of the update object any further updates aren't applied
64+
// multiple times to the same object
65+
node.parent[node.key] = deepMerge(node.value, structuredClone(update));
6466
}
6567
});
6668
}

0 commit comments

Comments
 (0)