@@ -12,6 +12,7 @@ import {
12
12
getTsDocTagsOfNode
13
13
} from '../utils/ast-utils' ;
14
14
import {
15
+ convertPath ,
15
16
getDecoratorOrUndefinedByNames ,
16
17
getTypeReferenceAsString ,
17
18
hasPropertyKey ,
@@ -204,12 +205,19 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
204
205
decorators ,
205
206
factory
206
207
) ;
207
- const apiOperationExpr : ts . ObjectLiteralExpression | undefined =
208
- apiOperationDecorator &&
209
- head ( getDecoratorArguments ( apiOperationDecorator ) ) ;
210
- const apiOperationExprProperties =
211
- apiOperationExpr &&
212
- ( apiOperationExpr . properties as ts . NodeArray < ts . PropertyAssignment > ) ;
208
+ let apiOperationExistingProps :
209
+ | ts . NodeArray < ts . PropertyAssignment >
210
+ | undefined = undefined ;
211
+
212
+ if ( apiOperationDecorator && ! options . readonly ) {
213
+ const apiOperationExpr = head (
214
+ getDecoratorArguments ( apiOperationDecorator )
215
+ ) ;
216
+ if ( apiOperationExpr ) {
217
+ apiOperationExistingProps =
218
+ apiOperationExpr . properties as ts . NodeArray < ts . PropertyAssignment > ;
219
+ }
220
+ }
213
221
214
222
const extractedComments = getMainCommentOfNode ( node , sourceFile ) ;
215
223
if ( ! extractedComments ) {
@@ -222,12 +230,12 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
222
230
keyToGenerate ,
223
231
factory . createStringLiteral ( extractedComments )
224
232
) ,
225
- ...( apiOperationExprProperties ?? factory . createNodeArray ( ) )
233
+ ...( apiOperationExistingProps ?? factory . createNodeArray ( ) )
226
234
] ;
227
235
228
236
const hasDeprecatedKey = hasPropertyKey (
229
237
'deprecated' ,
230
- factory . createNodeArray ( apiOperationExprProperties )
238
+ factory . createNodeArray ( apiOperationExistingProps )
231
239
) ;
232
240
if ( ! hasDeprecatedKey && tags . deprecated ) {
233
241
const deprecatedPropertyAssignment = factory . createPropertyAssignment (
@@ -345,16 +353,15 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
345
353
if ( ! type ) {
346
354
return undefined ;
347
355
}
348
- const { typeName , isArray } = getTypeReferenceAsString ( type , typeChecker ) ;
349
- if ( ! typeName ) {
356
+ const typeReferenceDescriptor = getTypeReferenceAsString ( type , typeChecker ) ;
357
+ if ( ! typeReferenceDescriptor . typeName ) {
350
358
return undefined ;
351
359
}
352
- if ( typeName . includes ( 'node_modules' ) ) {
360
+ if ( typeReferenceDescriptor . typeName . includes ( 'node_modules' ) ) {
353
361
return undefined ;
354
362
}
355
363
const identifier = this . typeReferenceStringToIdentifier (
356
- typeName ,
357
- isArray ,
364
+ typeReferenceDescriptor ,
358
365
hostFilename ,
359
366
options ,
360
367
factory
@@ -399,20 +406,23 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
399
406
}
400
407
401
408
private normalizeImportPath ( pathToSource : string , path : string ) {
402
- let relativePath = posix . relative ( pathToSource , path ) ;
409
+ let relativePath = posix . relative ( pathToSource , convertPath ( path ) ) ;
403
410
relativePath = relativePath [ 0 ] !== '.' ? './' + relativePath : relativePath ;
404
411
return relativePath ;
405
412
}
406
413
407
414
private typeReferenceStringToIdentifier (
408
- _typeReference : string ,
409
- isArray : boolean ,
415
+ typeReferenceDescriptor : {
416
+ typeName : string ;
417
+ isArray ?: boolean ;
418
+ arrayDepth ?: number ;
419
+ } ,
410
420
hostFilename : string ,
411
421
options : PluginOptions ,
412
422
factory : ts . NodeFactory
413
423
) {
414
- const { typeReference, importPath } = replaceImportPath (
415
- _typeReference ,
424
+ const { typeReference, importPath, typeName } = replaceImportPath (
425
+ typeReferenceDescriptor . typeName ,
416
426
hostFilename ,
417
427
options
418
428
) ;
@@ -423,14 +433,25 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
423
433
this . _typeImports [ importPath ] = typeReference ;
424
434
}
425
435
426
- identifier = factory . createIdentifier (
427
- isArray ? `[t["${ importPath } "]]` : `t["${ importPath } "]`
428
- ) ;
436
+ let ref = `t["${ importPath } "].${ typeName } ` ;
437
+ if ( typeReferenceDescriptor . isArray ) {
438
+ ref = this . wrapTypeInArray ( ref , typeReferenceDescriptor . arrayDepth ) ;
439
+ }
440
+ identifier = factory . createIdentifier ( ref ) ;
429
441
} else {
430
- identifier = factory . createIdentifier (
431
- isArray ? `[${ typeReference } ]` : typeReference
432
- ) ;
442
+ let ref = typeReference ;
443
+ if ( typeReferenceDescriptor . isArray ) {
444
+ ref = this . wrapTypeInArray ( ref , typeReferenceDescriptor . arrayDepth ) ;
445
+ }
446
+ identifier = factory . createIdentifier ( ref ) ;
433
447
}
434
448
return identifier ;
435
449
}
450
+
451
+ private wrapTypeInArray ( typeRef : string , arrayDepth : number ) {
452
+ for ( let i = 0 ; i < arrayDepth ; i ++ ) {
453
+ typeRef = `[${ typeRef } ]` ;
454
+ }
455
+ return typeRef ;
456
+ }
436
457
}
0 commit comments