1
1
import * as Path from "path" ;
2
+ import DotProp from "dot-prop" ;
2
3
3
4
import ts from "typescript" ;
4
5
@@ -8,6 +9,8 @@ import * as TypeScriptCodeGenerator from "../TsGenerator";
8
9
import * as ConverterContext from "./ConverterContext" ;
9
10
import * as ToTypeNode from "./toTypeNode" ;
10
11
import type * as Walker from "./Walker" ;
12
+ import * as Guard from "./Guard" ;
13
+ import * as Reference from "./components/Reference" ;
11
14
12
15
export interface ReferencePathSet {
13
16
pathArray : string [ ] ;
@@ -93,6 +96,27 @@ export const create = (
93
96
const { pathArray, base } = generatePath ( entryPoint , currentPoint , referencePath ) ;
94
97
return calculateReferencePath ( store , base , pathArray , converterContext ) ;
95
98
} ;
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
+ } ;
96
120
const setReferenceHandler : ToTypeNode . Context [ "setReferenceHandler" ] = ( currentPoint , reference ) => {
97
121
if ( store . hasStatement ( reference . path , [ "interface" , "typeAlias" ] ) ) {
98
122
return ;
@@ -107,6 +131,7 @@ export const create = (
107
131
rootSchema,
108
132
setReferenceHandler,
109
133
resolveReferencePath,
134
+ findSchemaByPathArray,
110
135
} ,
111
136
converterContext ,
112
137
) ;
@@ -133,6 +158,7 @@ export const create = (
133
158
rootSchema,
134
159
setReferenceHandler,
135
160
resolveReferencePath,
161
+ findSchemaByPathArray,
136
162
} ,
137
163
converterContext ,
138
164
) ,
@@ -161,5 +187,10 @@ export const create = (
161
187
}
162
188
}
163
189
} ;
164
- return { rootSchema, setReferenceHandler : setReferenceHandler , resolveReferencePath : resolveReferencePath } ;
190
+ return {
191
+ rootSchema,
192
+ setReferenceHandler : setReferenceHandler ,
193
+ resolveReferencePath : resolveReferencePath ,
194
+ findSchemaByPathArray : findSchemaByPathArray ,
195
+ } ;
165
196
} ;
0 commit comments