@@ -3,11 +3,13 @@ import { JsonPath } from '@stoplight/types';
33import { JSONSchema4 } from 'json-schema' ;
44import { cloneDeep , compact } from 'lodash' ;
55import { ResolvingError } from '../../errors' ;
6- import { WalkerRefResolver } from './populateTree' ;
6+ import { WalkingOptions } from './populateTree' ;
77
88const resolveAllOf = require ( '@stoplight/json-schema-merge-allof' ) ;
99
10- function _mergeAllOf ( schema : JSONSchema4 , path : JsonPath , resolveRef : WalkerRefResolver ) {
10+ const store = new WeakMap < WalkingOptions , WeakMap < JSONSchema4 , string [ ] > > ( ) ;
11+
12+ function _mergeAllOf ( schema : JSONSchema4 , path : JsonPath , opts : WalkingOptions ) {
1113 return resolveAllOf ( cloneDeep ( schema ) , {
1214 deep : false ,
1315 resolvers : {
@@ -37,14 +39,29 @@ function _mergeAllOf(schema: JSONSchema4, path: JsonPath, resolveRef: WalkerRefR
3739 throw new ResolvingError ( 'Circular reference detected' ) ;
3840 }
3941
40- return resolveRef ( null , $ref ) ;
42+ const allRefs = store . get ( opts ) ! ;
43+ const schemaRefs = allRefs . get ( schema ) ;
44+
45+ if ( schemaRefs === void 0 ) {
46+ allRefs . set ( schema , [ $ref ] ) ;
47+ } else if ( schemaRefs . includes ( $ref ) ) {
48+ throw new ResolvingError ( 'Circular reference detected' ) ;
49+ } else {
50+ schemaRefs . push ( $ref ) ;
51+ }
52+
53+ return opts . resolveRef ( null , $ref ) ;
4154 } ,
4255 } ) ;
4356}
4457
45- export const mergeAllOf = ( schema : JSONSchema4 , path : JsonPath , resolveRef : WalkerRefResolver ) => {
58+ export const mergeAllOf = ( schema : JSONSchema4 , path : JsonPath , opts : WalkingOptions ) => {
4659 try {
47- return _mergeAllOf ( schema , path , resolveRef ) ;
60+ if ( ! store . has ( opts ) ) {
61+ store . set ( opts , new WeakMap ( ) ) ;
62+ }
63+
64+ return _mergeAllOf ( schema , path , opts ) ;
4865 } catch ( ex ) {
4966 console . error ( ex . message ) ;
5067 throw ex ;
0 commit comments