Skip to content

Commit a275d2a

Browse files
authored
fix: bundleTarget should maintain array or object from original source (#49)
* chore: dep update * fix: bundleTarget should respect original array or object * chore: lint fix
1 parent 580d5e1 commit a275d2a

File tree

4 files changed

+107
-49
lines changed

4 files changed

+107
-49
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
},
4040
"dependencies": {
4141
"@stoplight/ordered-object-literal": "^1.0.1",
42-
"@stoplight/types": "^11.4.0",
43-
"jsonc-parser": "~2.2.0",
42+
"@stoplight/types": "^11.6.0",
43+
"jsonc-parser": "~2.2.1",
4444
"lodash": "^4.17.15",
4545
"safe-stable-stringify": "^1.1"
4646
},

src/__tests__/bundle.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,67 @@ describe('bundleTargetPath()', () => {
119119
},
120120
});
121121
});
122+
123+
it('should mirror original source decision re arrays or objects', () => {
124+
const document = {
125+
parameters: [
126+
{},
127+
{
128+
schema: {
129+
name: 'param',
130+
},
131+
},
132+
],
133+
responses: {
134+
'200': {
135+
other: 'foo',
136+
schema: {
137+
title: 'OK',
138+
parameter: {
139+
$ref: '#/parameters/1/schema',
140+
},
141+
},
142+
},
143+
},
144+
__target__: {
145+
entity: {
146+
$ref: '#/responses/200/schema',
147+
},
148+
},
149+
};
150+
151+
const clone = cloneDeep(document);
152+
153+
const result = bundleTarget({
154+
document: clone,
155+
path: '#/__target__',
156+
});
157+
158+
// Do not mutate document
159+
expect(clone).toEqual(document);
160+
161+
expect(result).toEqual({
162+
entity: {
163+
$ref: '#/responses/200/schema',
164+
},
165+
parameters: [
166+
undefined,
167+
{
168+
schema: {
169+
name: 'param',
170+
},
171+
},
172+
],
173+
responses: {
174+
'200': {
175+
schema: {
176+
title: 'OK',
177+
parameter: {
178+
$ref: '#/parameters/1/schema',
179+
},
180+
},
181+
},
182+
},
183+
});
184+
});
122185
});

src/bundle.ts

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

33
import { isLocalRef } from './isLocalRef';
44
import { pointerToPath } from './pointerToPath';
@@ -14,8 +14,24 @@ const _bundle = (document: unknown, path: string, cur?: unknown) => {
1414
if (property === '$ref' && typeof propertyValue === 'string' && isLocalRef(propertyValue)) {
1515
const _path = pointerToPath(propertyValue);
1616
const bundled$Ref = get(document, _path);
17-
const exists = !!get(objectToBundle, _path);
17+
const exists = has(objectToBundle, _path);
1818
if (bundled$Ref && !exists) {
19+
const pathProcessed = [];
20+
21+
// make sure arrays and object decisions are preserved when copying over the portion of the tree
22+
for (const key of _path) {
23+
pathProcessed.push(key);
24+
25+
if (has(objectToBundle, pathProcessed)) continue;
26+
27+
const target = get(document, pathProcessed);
28+
if (Array.isArray(target)) {
29+
set(objectToBundle, pathProcessed, []);
30+
} else if (typeof target === 'object') {
31+
set(objectToBundle, pathProcessed, {});
32+
}
33+
}
34+
1935
set(objectToBundle, _path, bundled$Ref);
2036
_bundle(document, path, bundled$Ref);
2137
}

yarn.lock

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -827,12 +827,13 @@
827827
shelljs "0.8.x"
828828
tslib "1.9.3"
829829

830-
"@stoplight/types@^11.4.0":
831-
version "11.4.0"
832-
resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-11.4.0.tgz#a2bedbbec37700d3c5a8c7d36c763a61d7a8bce7"
833-
integrity sha512-kMh1Sv7bA8BdbUaRXsRyi2K8Y5PzPOUWNSjB4qKOi0P6+dLczjlKggEIw9Xzuu1tCgBFdEvNwjnYDey0iqgeZQ==
830+
"@stoplight/types@^11.6.0":
831+
version "11.6.0"
832+
resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-11.6.0.tgz#c4507f564ea4be719f66ae2fd2bb83f4719c2210"
833+
integrity sha512-J2wOl6FlN4IeY99MZTbgLVbIqrE9eVcHIvWmSEFzxfnbHCh4reXcGkvxlQ7I/pTKScd5/F/HJKSYnNXRjCnM2A==
834834
dependencies:
835-
"@types/json-schema" "^7.0.3"
835+
"@types/json-schema" "^7.0.4"
836+
utility-types "^3.10.0"
836837

837838
"@types/babel__core@^7.1.0":
838839
version "7.1.3"
@@ -918,10 +919,10 @@
918919
dependencies:
919920
"@types/jest-diff" "*"
920921

921-
"@types/json-schema@^7.0.3":
922-
version "7.0.3"
923-
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
924-
integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A==
922+
"@types/json-schema@^7.0.4":
923+
version "7.0.4"
924+
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339"
925+
integrity sha512-8+KAKzEvSUdeo+kmqnKrqgeE+LcA0tjYWFY7RPProVYwnqDjukzO+3b6dLD56rYX5TdWejnEOLJYOIeh4CXKuA==
925926

926927
"@types/lodash@^4.14.144":
927928
version "4.14.144"
@@ -2742,12 +2743,7 @@ fast-glob@^3.0.3:
27422743
merge2 "^1.3.0"
27432744
micromatch "^4.0.2"
27442745

2745-
2746-
version "2.0.0"
2747-
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2"
2748-
integrity sha1-1RQsDK7msRifh9OnYREGT4bIu/I=
2749-
2750-
fast-json-stable-stringify@^2.0.0:
2746+
[email protected], fast-json-stable-stringify@^2.0.0:
27512747
version "2.1.0"
27522748
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
27532749
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==
@@ -4481,10 +4477,10 @@ [email protected], json5@^2.1.0:
44814477
dependencies:
44824478
minimist "^1.2.0"
44834479

4484-
jsonc-parser@~2.2.0:
4485-
version "2.2.0"
4486-
resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-2.2.0.tgz#f206f87f9d49d644b7502052c04e82dd6392e9ef"
4487-
integrity sha512-4fLQxW1j/5fWj6p78vAlAafoCKtuBm6ghv+Ij5W2DrDx0qE+ZdEl2c6Ko1mgJNF5ftX1iEWQQ4Ap7+3GlhjkOA==
4480+
jsonc-parser@~2.2.1:
4481+
version "2.2.1"
4482+
resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-2.2.1.tgz#db73cd59d78cce28723199466b2a03d1be1df2bc"
4483+
integrity sha512-o6/yDBYccGvTz1+QFevz6l6OBZ2+fMVu2JZ9CIhzsYRX4mjaK5IyX9eldUdCmga16zlgQxyrj5pt9kzuj2C02w==
44884484

44894485
jsonfile@^4.0.0:
44904486
version "4.0.0"
@@ -5314,12 +5310,12 @@ minimist-options@^3.0.1:
53145310
arrify "^1.0.1"
53155311
is-plain-obj "^1.1.0"
53165312

5317-
[email protected], minimist@^1.1.1, minimist@^1.1.3:
5313+
53185314
version "1.2.0"
53195315
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
53205316
integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=
53215317

5322-
minimist@^1.2.0, minimist@^1.2.5:
5318+
minimist@^1.1.1, minimist@^1.1.3, minimist@^1.2.0, minimist@^1.2.5:
53235319
version "1.2.5"
53245320
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
53255321
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==
@@ -5732,7 +5728,6 @@ npm@^6.10.3, npm@^6.8.0:
57325728
cmd-shim "^3.0.3"
57335729
columnify "~1.5.4"
57345730
config-chain "^1.1.12"
5735-
debuglog "*"
57365731
detect-indent "~5.0.0"
57375732
detect-newline "^2.1.0"
57385733
dezalgo "~1.0.3"
@@ -5747,7 +5742,6 @@ npm@^6.10.3, npm@^6.8.0:
57475742
has-unicode "~2.0.1"
57485743
hosted-git-info "^2.8.8"
57495744
iferr "^1.0.2"
5750-
imurmurhash "*"
57515745
infer-owner "^1.0.4"
57525746
inflight "~1.0.6"
57535747
inherits "^2.0.4"
@@ -5766,14 +5760,8 @@ npm@^6.10.3, npm@^6.8.0:
57665760
libnpx "^10.2.2"
57675761
lock-verify "^2.1.0"
57685762
lockfile "^1.0.4"
5769-
lodash._baseindexof "*"
57705763
lodash._baseuniq "~4.6.0"
5771-
lodash._bindcallback "*"
5772-
lodash._cacheindexof "*"
5773-
lodash._createcache "*"
5774-
lodash._getnative "*"
57755764
lodash.clonedeep "~4.5.0"
5776-
lodash.restparam "*"
57775765
lodash.union "~4.6.0"
57785766
lodash.uniq "~4.5.0"
57795767
lodash.without "~4.4.0"
@@ -6067,20 +6055,13 @@ p-limit@^1.1.0:
60676055
dependencies:
60686056
p-try "^1.0.0"
60696057

6070-
p-limit@^2.0.0:
6058+
p-limit@^2.0.0, p-limit@^2.2.0:
60716059
version "2.3.0"
60726060
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.3.0.tgz#3dd33c647a214fdfffd835933eb086da0dc21db1"
60736061
integrity sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==
60746062
dependencies:
60756063
p-try "^2.0.0"
60766064

6077-
p-limit@^2.2.0:
6078-
version "2.2.1"
6079-
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.2.1.tgz#aa07a788cc3151c939b5131f63570f0dd2009537"
6080-
integrity sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==
6081-
dependencies:
6082-
p-try "^2.0.0"
6083-
60846065
p-locate@^2.0.0:
60856066
version "2.0.0"
60866067
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43"
@@ -6939,14 +6920,7 @@ [email protected]:
69396920
dependencies:
69406921
path-parse "^1.0.6"
69416922

6942-
[email protected], resolve@^1.1.6, resolve@^1.3.2:
6943-
version "1.12.0"
6944-
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.12.0.tgz#3fc644a35c84a48554609ff26ec52b66fa577df6"
6945-
integrity sha512-B/dOmuoAik5bKcD6s6nXDCjzUKnaDvdkRyAk6rsmsKLipWj4797iothd7jmmUhWTfinVMU+wc56rYKsit2Qy4w==
6946-
dependencies:
6947-
path-parse "^1.0.6"
6948-
6949-
resolve@^1.10.0:
6923+
[email protected], resolve@^1.1.6, resolve@^1.10.0, resolve@^1.3.2:
69506924
version "1.17.0"
69516925
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.17.0.tgz#b25941b54968231cc2d1bb76a79cb7f2c0bf8444"
69526926
integrity sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==
@@ -8223,6 +8197,11 @@ util.promisify@^1.0.0:
82238197
define-properties "^1.1.2"
82248198
object.getownpropertydescriptors "^2.0.3"
82258199

8200+
utility-types@^3.10.0:
8201+
version "3.10.0"
8202+
resolved "https://registry.yarnpkg.com/utility-types/-/utility-types-3.10.0.tgz#ea4148f9a741015f05ed74fd615e1d20e6bed82b"
8203+
integrity sha512-O11mqxmi7wMKCo6HKFt5AhO4BwY3VV68YU07tgxfz8zJTIxr4BpsezN49Ffwy9j3ZpwwJp4fkRwjRzq3uWE6Rg==
8204+
82268205
uuid@^3.3.2, uuid@^3.3.3:
82278206
version "3.4.0"
82288207
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"

0 commit comments

Comments
 (0)