1
1
import { pathToPointer , pointerToPath , startsWith , trimStart } from '@stoplight/json' ;
2
2
import produce from 'immer' ;
3
- import _get = require( 'lodash/get' ) ;
4
- import _set = require( 'lodash/set' ) ;
3
+ import { get , set } from 'lodash' ;
5
4
import * as URI from 'urijs' ;
6
5
7
6
import { Cache } from './cache' ;
@@ -95,7 +94,7 @@ export class ResolveRunner implements Types.IResolveRunner {
95
94
jsonPointer = jsonPointer && jsonPointer . trim ( ) ;
96
95
if ( jsonPointer && jsonPointer !== '#' && jsonPointer !== '#/' ) {
97
96
targetPath = pointerToPath ( jsonPointer ) ;
98
- resolved . result = _get ( resolved . result , targetPath ) ;
97
+ resolved . result = get ( resolved . result , targetPath ) ;
99
98
}
100
99
101
100
if ( ! resolved . result ) {
@@ -153,7 +152,7 @@ export class ResolveRunner implements Types.IResolveRunner {
153
152
if ( ! resolvedTargetPath . length ) {
154
153
return r . resolved . result ;
155
154
} else {
156
- _set ( draft , resolvedTargetPath , r . resolved . result ) ;
155
+ set ( draft , resolvedTargetPath , r . resolved . result ) ;
157
156
}
158
157
}
159
158
} ) ;
@@ -181,7 +180,7 @@ export class ResolveRunner implements Types.IResolveRunner {
181
180
if ( ! dependants . length ) continue ;
182
181
183
182
const pointerPath = pointerToPath ( pointer ) ;
184
- const val = _get ( draft , pointerPath ) ;
183
+ const val = get ( draft , pointerPath ) ;
185
184
for ( const dependant of dependants ) {
186
185
// check to prevent circular references in the resulting JS object
187
186
// this implementation is MUCH more performant than decycling the final object to remove circulars
@@ -201,7 +200,7 @@ export class ResolveRunner implements Types.IResolveRunner {
201
200
resolved . refMap [ pathToPointer ( dependantPath ) ] = pathToPointer ( pointerPath ) ;
202
201
203
202
if ( val ) {
204
- _set ( draft , dependantPath , val ) ;
203
+ set ( draft , dependantPath , val ) ;
205
204
} else {
206
205
resolved . errors . push ( {
207
206
code : 'POINTER_MISSING' ,
@@ -221,7 +220,7 @@ export class ResolveRunner implements Types.IResolveRunner {
221
220
}
222
221
223
222
if ( targetPath ) {
224
- resolved . result = _get ( this . _source , targetPath ) ;
223
+ resolved . result = get ( this . _source , targetPath ) ;
225
224
} else {
226
225
resolved . result = this . _source ;
227
226
}
@@ -288,7 +287,24 @@ export class ResolveRunner implements Types.IResolveRunner {
288
287
throw new Error ( `No reader defined for scheme '${ ref . scheme ( ) } ' in ref ${ ref . toString ( ) } ` ) ;
289
288
}
290
289
291
- const result = await reader . read ( ref , this . ctx ) ;
290
+ let result = await reader . read ( ref , this . ctx ) ;
291
+
292
+ // support custom parsers
293
+ if ( this . parseAuthorityResult ) {
294
+ try {
295
+ const parsed = await this . parseAuthorityResult ( {
296
+ authorityResult : result ,
297
+ result,
298
+ targetAuthority : ref ,
299
+ parentAuthority : this . authority ,
300
+ parentPath : [ ] ,
301
+ } ) ;
302
+
303
+ result = parsed . result ;
304
+ } catch ( e ) {
305
+ throw new Error ( `Could not parse remote reference response for '${ ref . toString ( ) } ' - ${ String ( e ) } ` ) ;
306
+ }
307
+ }
292
308
293
309
return new ResolveRunner ( result , {
294
310
depth : this . depth + 1 ,
@@ -380,46 +396,13 @@ export class ResolveRunner implements Types.IResolveRunner {
380
396
: error . path ;
381
397
382
398
if ( errorPathInResult && errorPathInResult . length ) {
383
- _set ( lookupResult . resolved . result , errorPathInResult , val ) ;
399
+ set ( lookupResult . resolved . result , errorPathInResult , val ) ;
384
400
} else if ( lookupResult . resolved . result ) {
385
401
lookupResult . resolved . result = val ;
386
402
}
387
403
}
388
404
}
389
405
}
390
-
391
- // support custom parsers
392
- if ( this . parseAuthorityResult ) {
393
- try {
394
- // TODO: rework this to pass in an addValidation function to allow custom parsers to add their own validations
395
- // then generally re-work the error system here to be based around more flexible validations
396
- const parsed = await this . parseAuthorityResult ( {
397
- authorityResult : lookupResult ,
398
- result : lookupResult . resolved . result ,
399
- targetAuthority : ref ,
400
- parentAuthority : this . authority ,
401
- parentPath,
402
- } ) ;
403
-
404
- // if (parsed.errors) {
405
- // TODO: as mentioned above, allow caller to add errors/validations
406
- // }
407
-
408
- lookupResult . resolved . result = parsed . result ;
409
- } catch ( e ) {
410
- // could not parse... roll back to original value
411
- lookupResult . resolved . result = val ;
412
-
413
- lookupResult . error = {
414
- code : 'PARSE_AUTHORITY' ,
415
- message : `Error parsing lookup result for '${ ref . toString ( ) } ': ${ String ( e ) } ` ,
416
- authority : ref ,
417
- authorityStack : this . authorityStack ,
418
- pointerStack,
419
- path : parentPath ,
420
- } ;
421
- }
422
- }
423
406
}
424
407
}
425
408
0 commit comments