11import { red , blue , green } from 'colorette' ;
22import * as fs from 'node:fs' ;
3- import { parseYaml , slash , isRef , isTruthy , dequal , logger } from '@redocly/openapi-core' ;
3+ import {
4+ parseYaml ,
5+ slash ,
6+ isRef ,
7+ isTruthy ,
8+ dequal ,
9+ logger ,
10+ isEmptyObject ,
11+ isPlainObject ,
12+ } from '@redocly/openapi-core' ;
413import * as path from 'node:path' ;
514import { performance } from 'perf_hooks' ;
615import {
@@ -12,7 +21,6 @@ import {
1221 writeToFileByExtension ,
1322 getAndValidateFileExtension ,
1423} from '../../utils/miscellaneous.js' ;
15- import { isObject , isEmptyObject } from '../../utils/js-utils.js' ;
1624import { exitWithError } from '../../utils/error.js' ;
1725import {
1826 OPENAPI3_COMPONENT ,
@@ -148,19 +156,23 @@ function traverseDirectoryDeepCallback(
148156}
149157
150158export function crawl ( object : unknown , visitor : ( node : Record < string , unknown > ) => void ) {
151- if ( ! isObject ( object ) ) return ;
152-
153- visitor ( object ) ;
154- for ( const key of Object . keys ( object ) ) {
155- crawl ( object [ key ] , visitor ) ;
159+ if ( isPlainObject ( object ) ) {
160+ visitor ( object ) ;
161+ for ( const key of Object . keys ( object ) ) {
162+ crawl ( object [ key ] , visitor ) ;
163+ }
164+ } else if ( Array . isArray ( object ) ) {
165+ for ( const item of object ) {
166+ crawl ( item , visitor ) ;
167+ }
156168 }
157169}
158170
159171function replace$Refs ( obj : unknown , relativeFrom : string , componentFiles = { } as ComponentsFiles ) {
160172 crawl ( obj , ( node : Record < string , unknown > ) => {
161- if ( node . $ref && typeof node . $ref === 'string' && startsWithComponents ( node . $ref ) ) {
173+ if ( isRef ( node ) && startsWithComponents ( node . $ref ) ) {
162174 replace ( node as RefObject , '$ref' ) ;
163- } else if ( isObject ( node . discriminator ) && isObject ( node . discriminator . mapping ) ) {
175+ } else if ( isPlainObject ( node . discriminator ) && isPlainObject ( node . discriminator . mapping ) ) {
164176 const { mapping } = node . discriminator ;
165177 for ( const name of Object . keys ( mapping ) ) {
166178 const mappingPointer = mapping [ name ] ;
0 commit comments