Skip to content

update: concatenate Arrays when target node is an array #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ function applyOverlayToOpenAPI(spec, overlay) {
try {
// It must be an update
jsonpath.apply(spec, a.target, (chunk) => {
// Deep merge using a module (built-in spread operator is only shallow)
const merger = mergician({appendArrays: true})
return merger(chunk, a.update)
if (Array.isArray(chunk) && Array.isArray(a.update)) {
// Concat arrays if target and update are both array objects
return chunk.concat(a.update);
} else {
// Deep merge objects using a module (built-in spread operator is only shallow)
const merger = mergician({ appendArrays: true });
return merger(chunk, a.update);
}
});
}
catch (ex) {
Expand Down
72 changes: 72 additions & 0 deletions test/expected/town-update-array.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
openapi: 3.1.0
info:
version: 1.0.0
title: Imaginary town
servers:
- url: 'https://example.com'
description: Example server
- url: 'https://example.com/prod'
description: Production server
paths:
/buildings:
get:
summary: All buildings
operationId: buildingsList
responses:
'200':
description: Return all known buildings
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Building'
'/buildings/{buildingId}':
get:
summary: Specific building
operationId: buildingById
parameters:
- name: buildingId
in: path
required: true
description: Which building to return
schema:
type: string
responses:
'200':
description: Return a building
content:
application/json:
schema:
$ref: '#/components/schemas/Building'
/locations:
get:
summary: All locations
operationId: locationList
responses:
'200':
description: Returns all locations
content:
application/json:
schema:
type: array
items:
type: object
properties:
location_id:
type: integer
example: 44
name:
type: string
example: North Village
components:
schemas:
Building:
type: object
properties:
building:
type: string
example: house
location_id:
type: integer
example: 44
11 changes: 11 additions & 0 deletions test/overlay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ test('remove all description fields', () => {
expect(result).toEqual(expectedOutput);
});

test('concatenate lists when target is an array type', () => {
const openapiFile = "test/openapi/town.yaml";
const overlayFile = "test/overlays/update-array.yaml";
const expectedFile = "test/expected/town-update-array.yaml";
const expectedOutput = fs.readFileSync(expectedFile, 'utf8');

const result = overlayFiles(openapiFile, overlayFile);

expect(result).toEqual(expectedOutput);
});

test('fail to update a primitive string type', () => {
const openapiFile = "test/openapi/immutable.yaml";
const overlayFile = "test/overlays/immutable.yaml";
Expand Down
9 changes: 9 additions & 0 deletions test/overlays/update-array.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
overlay: 1.0.0
info:
title: Structured Overlay
version: 1.0.0
actions:
- target: $.servers
update:
- url: https://example.com/prod
description: Production server