Skip to content

Commit 6a21b9c

Browse files
authored
Merge pull request #20 from paulRbr/allow-root-target
root: allow updates on the root target ('$')
2 parents 6e5961f + 0d18f96 commit 6a21b9c

File tree

4 files changed

+103
-5
lines changed

4 files changed

+103
-5
lines changed

src/overlay.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ function applyOverlayToOpenAPI (spec, overlay) {
2525
} else {
2626
try {
2727
// It must be an update
28-
jsonpath.apply(spec, a.target, (chunk) => {
29-
// Deep merge using a module (built-in spread operator is only shallow)
30-
const merger = mergician({ appendArrays: true })
31-
return merger(chunk, a.update)
32-
})
28+
if (a.target === '$') {
29+
spec = merger(a.update)(spec)
30+
} else {
31+
jsonpath.apply(spec, a.target, merger(a.update))
32+
}
3333
} catch (ex) {
3434
process.stderr.write(`Error applying overlay: ${ex.message}\n`)
3535
// return chunk
@@ -41,6 +41,13 @@ function applyOverlayToOpenAPI (spec, overlay) {
4141
return spec
4242
}
4343

44+
// Deep merge using a module (built-in spread operator is only shallow)
45+
function merger (obj) {
46+
return (chunk) => {
47+
return mergician({ appendArrays: true })(chunk, obj)
48+
}
49+
}
50+
4451
function sortOpenAPIFields (field1, field2) {
4552
const orderedKeys = ['info', 'servers', 'summary', 'operationId', 'tags', 'paths', 'components', 'description', 'parameters', 'responses']
4653

test/expected/town-root-updated.yaml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
openapi: 3.1.0
2+
info:
3+
version: 1.0.0
4+
title: Imaginary town
5+
x-overlaid: true
6+
servers:
7+
- url: 'https://example.com'
8+
description: Example server
9+
paths:
10+
/buildings:
11+
get:
12+
summary: All buildings
13+
operationId: buildingsList
14+
responses:
15+
'200':
16+
description: Return all known buildings
17+
content:
18+
application/json:
19+
schema:
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: integer
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: integer
71+
example: 44

test/overlay.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ test('add a description and update the summary', () => {
2323
expect(result).toEqual(expectedOutput)
2424
})
2525

26+
test('apply an overlay to the root object', () => {
27+
const openapiFile = 'test/openapi/town.yaml'
28+
const overlayFile = 'test/overlays/update-root.yaml'
29+
const expectedFile = 'test/expected/town-root-updated.yaml'
30+
const expectedOutput = fs.readFileSync(expectedFile, 'utf8')
31+
32+
const result = overlayFiles(openapiFile, overlayFile)
33+
34+
expect(result).toEqual(expectedOutput)
35+
})
36+
2637
test('remove an example', () => {
2738
const openapiFile = 'test/openapi/town.yaml'
2839
const overlayFile = 'test/overlays/remove-example.yaml'

test/overlays/update-root.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
overlay: 1.0.0
2+
info:
3+
title: Structured Overlay
4+
version: 1.0.0
5+
actions:
6+
- target: "$" # Root of document
7+
update:
8+
info:
9+
x-overlaid: true

0 commit comments

Comments
 (0)