@@ -5,7 +5,7 @@ import { JSONSchema4 } from 'json-schema';
55import { get as _get , isEqual as _isEqual , isObject as _isObject } from 'lodash' ;
66import { SchemaTreeListNode } from '../types' ;
77import { generateId } from '../utils/generateId' ;
8- import { isRefNode } from '../utils/guards' ;
8+ import { hasRefItems , isRefNode } from '../utils/guards' ;
99import { getSchemaNodeMetadata , metadataStore } from './metadata' ;
1010import { canStepIn } from './utils/canStepIn' ;
1111import { populateTree } from './utils/populateTree' ;
@@ -44,7 +44,10 @@ export class SchemaTree extends Tree {
4444 populateTree ( this . schema , this . root , 0 , [ ] , {
4545 mergeAllOf : this . mergeAllOf ,
4646 onNode : ( fragment , node , parentTreeNode , level ) : boolean => {
47- if ( isRefNode ( node ) && node . $ref !== null && isLocalRef ( node . $ref ) ) {
47+ if (
48+ ( isRefNode ( node ) && node . $ref !== null && isLocalRef ( node . $ref ) ) ||
49+ ( hasRefItems ( node ) && node . items . $ref !== null && isLocalRef ( node . items . $ref ) )
50+ ) {
4851 expanded [ node . id ] = false ;
4952 }
5053
@@ -109,22 +112,15 @@ export class SchemaTree extends Tree {
109112 if ( node . children . length !== 0 || this . visited . has ( node ) ) {
110113 return super . unwrap ( node ) ;
111114 }
112-
113115 const metadata = getSchemaNodeMetadata ( node ) ;
114116 const { path, schemaNode, schema } = metadata ;
115117 try {
116- if ( ! isRefNode ( schemaNode ) ) {
118+ if ( ! isRefNode ( schemaNode ) && ! hasRefItems ( schemaNode ) ) {
117119 this . populateTreeFragment ( node , schema , path , true ) ;
118- } else if ( schemaNode . $ref !== null ) {
119- const refPath = pointerToPath ( schemaNode . $ref ) ;
120- const schemaFragment = this . resolveRef
121- ? this . resolveRef ( refPath , path , this . schema )
122- : _get ( this . schema , refPath ) ;
123- if ( ! _isObject ( schemaFragment ) ) {
124- throw new ReferenceError ( `Could not dereference "${ pathToPointer ( refPath ) } "` ) ;
125- }
126-
127- this . populateTreeFragment ( node , schemaFragment , path , false ) ;
120+ } else if ( isRefNode ( schemaNode ) ) {
121+ this . populateRefFragment ( node , path , schemaNode . $ref ) ;
122+ } else if ( hasRefItems ( schemaNode ) ) {
123+ this . populateRefFragment ( node , path , schemaNode . items . $ref ) ;
128124 } else {
129125 throw new Error ( `I do know not how not expand node ${ path . join ( '.' ) } ` ) ;
130126 }
@@ -135,4 +131,17 @@ export class SchemaTree extends Tree {
135131 this . visited . add ( node ) ;
136132 return super . unwrap ( node ) ;
137133 }
134+
135+ protected populateRefFragment ( node : TreeListParentNode , path : JsonPath , ref : string | null ) {
136+ if ( ! ref ) {
137+ throw new Error ( 'Unknown $ref value' ) ;
138+ }
139+ const refPath = pointerToPath ( ref ) ;
140+ const schemaFragment = this . resolveRef ? this . resolveRef ( refPath , path , this . schema ) : _get ( this . schema , refPath ) ;
141+ if ( ! _isObject ( schemaFragment ) ) {
142+ throw new ReferenceError ( `Could not dereference "${ pathToPointer ( refPath ) } "` ) ;
143+ }
144+
145+ this . populateTreeFragment ( node , schemaFragment , path , false ) ;
146+ }
138147}
0 commit comments