Skip to content

Commit 39f2d95

Browse files
author
Domagoj Kriskovic
authored
fix: use lodash setWith when target is an object (#102)
* fix: use lodash setWith when target is an object * chore: lint fix
1 parent 4e597d3 commit 39f2d95

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/__tests__/bundle.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,72 @@ describe('bundleTargetPath()', () => {
809809
});
810810
});
811811

812+
it('should not convert object to array when using numeric keys', () => {
813+
const result = bundleTarget({
814+
document: {
815+
openapi: '3.0.0',
816+
paths: {
817+
'/path': {
818+
get: {
819+
responses: {
820+
'200': {
821+
content: {
822+
'application/json': {
823+
schema: {
824+
type: 'object',
825+
properties: {
826+
numModel: { $ref: '#/components/schemas/200' },
827+
},
828+
},
829+
},
830+
},
831+
},
832+
},
833+
},
834+
},
835+
},
836+
components: {
837+
schemas: {
838+
'200': {
839+
title: '200',
840+
type: 'object',
841+
properties: { id: { type: 'string' } },
842+
},
843+
},
844+
},
845+
__target__: {
846+
id: '?http-operation-id?',
847+
method: 'get',
848+
path: '/path',
849+
responses: [
850+
{
851+
code: '200',
852+
headers: [],
853+
contents: [
854+
{
855+
mediaType: 'application/json',
856+
schema: {
857+
type: 'object',
858+
properties: {
859+
numModel: { $ref: '#/components/schemas/200' },
860+
},
861+
$schema: 'http://json-schema.org/draft-07/schema#',
862+
},
863+
examples: [],
864+
encodings: [],
865+
},
866+
],
867+
},
868+
],
869+
},
870+
},
871+
path: '#/__target__',
872+
bundleRoot: '#/components/schemas',
873+
});
874+
875+
expect(Array.isArray(result.components.schemas)).toBe(false);
876+
});
877+
812878
describe('when custom bundleRoot is provided', () => {
813879
it('should work', () => {
814880
const bundleRoot = '#/__custom-root__';

src/bundle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cloneDeep, get, has, set } from 'lodash';
1+
import { cloneDeep, get, has, set, setWith } from 'lodash';
22

33
import { Dictionary, JsonPath } from '@stoplight/types';
44
import { hasRef } from './hasRef';
@@ -114,7 +114,7 @@ const bundle = (document: unknown, bundleRoot: JsonPath, errorsRoot: JsonPath) =
114114
if (Array.isArray(target)) {
115115
set(bundledObj, inventoryPath, new Array(target.length).fill(null));
116116
} else if (typeof target === 'object') {
117-
set(bundledObj, inventoryPath, {});
117+
setWith(bundledObj, inventoryPath, {}, Object);
118118
}
119119

120120
set(bundledObj, inventoryPath, bundled$Ref);

0 commit comments

Comments
 (0)