@@ -94,6 +94,7 @@ export const genTemplateModelByYapi = async (
9494 const reqBodyScheme = JSON . parse (
9595 stripJsonComments ( res . data . data . req_body_other ) ,
9696 ) ;
97+ fixSchema ( reqBodyScheme ) ;
9798 delete reqBodyScheme . title ;
9899 requestBodyType = await compile (
99100 reqBodyScheme ,
@@ -121,6 +122,7 @@ export const genTemplateModelByYapi = async (
121122 // const ts = await jsonToTs(selectInfo.typeName, res.data.data.res_body);
122123 const resBodyJson = JSON . parse ( stripJsonComments ( res . data . data . res_body ) ) ;
123124 const schema = GenerateSchema . json ( typeName || 'Schema' , resBodyJson ) ;
125+ fixSchema ( schema ) ;
124126 let ts = await compile ( schema , typeName , {
125127 bannerComment : '' ,
126128 } ) ;
@@ -131,6 +133,7 @@ export const genTemplateModelByYapi = async (
131133 const reqBodyScheme = JSON . parse (
132134 stripJsonComments ( res . data . data . req_body_other ) ,
133135 ) ;
136+ fixSchema ( reqBodyScheme ) ;
134137 delete reqBodyScheme . title ;
135138 requestBodyType = await compile (
136139 reqBodyScheme ,
@@ -156,18 +159,34 @@ export const genTemplateModelByYapi = async (
156159 return model ;
157160} ;
158161
159- const fixSchema = ( obj : object ) => {
162+ function fixSchema ( obj : object , fieldNames : string [ ] = [ '$ref' , '$$ref' ] ) {
160163 // eslint-disable-next-line no-restricted-syntax
161164 for ( const key in obj ) {
162- // @ts -ignore
163- if ( typeof obj [ key ] === 'object' && obj [ key ] !== null ) {
164- // @ts -ignore
165+ if ( Array . isArray ( obj [ key ] ) ) {
166+ obj [ key ] . forEach ( ( item : object ) => {
167+ if ( typeof item === 'object' && item !== null ) {
168+ fixSchema ( item , fieldNames ) ;
169+ } else {
170+ // eslint-disable-next-line no-restricted-syntax
171+ for ( const fieldName of fieldNames ) {
172+ if ( item && item [ fieldName ] ) {
173+ delete item [ fieldName ] ;
174+ }
175+ }
176+ }
177+ } ) ;
178+ } else if ( typeof obj [ key ] === 'object' && obj [ key ] !== null ) {
165179 if ( obj [ key ] . type === 'object' && ! obj [ key ] . properties ) {
166- // @ts -ignore
167180 delete obj [ key ] ;
168181 }
169- // @ts -ignore
170- fixSchema ( obj [ key ] ) ; // 递归处理
182+ fixSchema ( obj [ key ] , fieldNames ) ;
183+ } else {
184+ // eslint-disable-next-line no-restricted-syntax
185+ for ( const fieldName of fieldNames ) {
186+ if ( key === fieldName ) {
187+ delete obj [ key ] ;
188+ }
189+ }
171190 }
172191 }
173- } ;
192+ }
0 commit comments