11import * as Path from "path" ;
2+ import DotProp from "dot-prop" ;
23
34import ts from "typescript" ;
45
@@ -8,6 +9,8 @@ import * as TypeScriptCodeGenerator from "../TsGenerator";
89import * as ConverterContext from "./ConverterContext" ;
910import * as ToTypeNode from "./toTypeNode" ;
1011import type * as Walker from "./Walker" ;
12+ import * as Guard from "./Guard" ;
13+ import * as Reference from "./components/Reference" ;
1114
1215export interface ReferencePathSet {
1316 pathArray : string [ ] ;
@@ -93,6 +96,27 @@ export const create = (
9396 const { pathArray, base } = generatePath ( entryPoint , currentPoint , referencePath ) ;
9497 return calculateReferencePath ( store , base , pathArray , converterContext ) ;
9598 } ;
99+ const findSchemaByPathArray = (
100+ pathArray : string [ ] ,
101+ remainPathArray : string [ ] = [ ] ,
102+ ) : OpenApi . Schema | OpenApi . Reference | OpenApi . JSONSchemaDefinition => {
103+ const schema = DotProp . get < OpenApi . Schema > ( rootSchema , pathArray . join ( "." ) ) ;
104+ if ( ! schema ) {
105+ return findSchemaByPathArray ( pathArray . slice ( 0 , pathArray . length - 1 ) , [ pathArray [ pathArray . length - 1 ] , ...remainPathArray ] ) ;
106+ }
107+ if ( Guard . isReference ( schema ) ) {
108+ const ref = Reference . generate ( entryPoint , entryPoint , schema ) ;
109+ return findSchemaByPathArray ( ref . path . split ( "/" ) , remainPathArray ) ;
110+ }
111+ if ( remainPathArray . length ) {
112+ const moreNestSchema = DotProp . get < OpenApi . Schema > ( schema , remainPathArray . join ( "." ) ) ;
113+ if ( ! moreNestSchema ) {
114+ throw new Error ( "Not found" ) ;
115+ }
116+ return moreNestSchema ;
117+ }
118+ return schema ;
119+ } ;
96120 const setReferenceHandler : ToTypeNode . Context [ "setReferenceHandler" ] = ( currentPoint , reference ) => {
97121 if ( store . hasStatement ( reference . path , [ "interface" , "typeAlias" ] ) ) {
98122 return ;
@@ -107,6 +131,7 @@ export const create = (
107131 rootSchema,
108132 setReferenceHandler,
109133 resolveReferencePath,
134+ findSchemaByPathArray,
110135 } ,
111136 converterContext ,
112137 ) ;
@@ -133,6 +158,7 @@ export const create = (
133158 rootSchema,
134159 setReferenceHandler,
135160 resolveReferencePath,
161+ findSchemaByPathArray,
136162 } ,
137163 converterContext ,
138164 ) ,
@@ -161,5 +187,10 @@ export const create = (
161187 }
162188 }
163189 } ;
164- return { rootSchema, setReferenceHandler : setReferenceHandler , resolveReferencePath : resolveReferencePath } ;
190+ return {
191+ rootSchema,
192+ setReferenceHandler : setReferenceHandler ,
193+ resolveReferencePath : resolveReferencePath ,
194+ findSchemaByPathArray : findSchemaByPathArray ,
195+ } ;
165196} ;
0 commit comments