@@ -2,9 +2,9 @@ import { TreeListNode, TreeListParentNode } from '@stoplight/tree-list';
22import { JsonPath , Optional } from '@stoplight/types' ;
33import { JSONSchema4 } from 'json-schema' ;
44import { isObject as _isObject } from 'lodash' ;
5+
56import { IArrayNode , IObjectNode , SchemaKind , SchemaNode , SchemaTreeListNode } from '../../types' ;
67import { generateId } from '../../utils/generateId' ;
7- import { getCombiners } from '../../utils/getCombiners' ;
88import { getPrimaryType } from '../../utils/getPrimaryType' ;
99import { isCombinerNode , isRefNode } from '../../utils/guards' ;
1010import { getNodeMetadata , getSchemaNodeMetadata , metadataStore } from '../metadata' ;
@@ -139,18 +139,31 @@ function processArray(
139139 }
140140 }
141141 } else {
142- const subtype = getPrimaryType ( items ) ;
143- switch ( subtype ) {
144- case SchemaKind . Object :
145- return processObject ( node , items as IObjectNode , level , [ ...path , 'items' ] , options ) ;
146- case SchemaKind . Array :
147- return processArray ( node , items as IObjectNode , level , [ ...path , 'items' ] , options ) ;
148- default :
149- const combiners = getCombiners ( items ) ;
150- if ( combiners !== void 0 ) {
151- ( node as TreeListParentNode ) . children = [ ] ;
152- populateTree ( items , node as TreeListParentNode , level , [ ...path , 'items' ] , options ) ;
142+ const children : TreeListNode [ ] = [ ] ;
143+ ( node as TreeListParentNode ) . children = children ;
144+ populateTree ( items , node as TreeListParentNode , level , [ ...path , 'items' ] , options ) ;
145+
146+ // optional flattening
147+ if ( children . length === 1 ) {
148+ let schemaNode ;
149+ try {
150+ ( { schemaNode } = getSchemaNodeMetadata ( children [ 0 ] ) ) ;
151+ } catch {
152+ return node ;
153+ }
154+
155+ if ( ! ( 'children' in children [ 0 ] ) ) {
156+ // we'll render this in subtype next to array, i.e. array[subtype], so let's get rid of these redundant nodes
157+ // @ts -ignore
158+ delete node . children ;
159+ } else if ( ! ( 'combiner' in schemaNode ) ) {
160+ for ( const child of children [ 0 ] . children ) {
161+ // re-parenting
162+ child . parent = node as TreeListParentNode ;
153163 }
164+
165+ children . splice ( 0 , children . length , ...children [ 0 ] . children ) ;
166+ }
154167 }
155168 }
156169
0 commit comments