@@ -254,29 +254,6 @@ export class TypeResolver {
254254 return stringMetaType ;
255255 }
256256
257- const type = this . current . typeChecker . getTypeFromTypeNode ( this . typeNode ) ;
258- const toJSON = this . current . typeChecker . getPropertyOfType ( type , 'toJSON' ) ;
259- if ( toJSON ) {
260- const declaration = toJSON . declarations [ 0 ] as ts . MethodDeclaration ;
261-
262- let nodeType = declaration . type ;
263- if ( ! nodeType ) {
264- const typeChecker = this . current . typeChecker ;
265- const signature = typeChecker . getSignatureFromDeclaration ( declaration ) ;
266- const implicitType = typeChecker . getReturnTypeOfSignature ( signature ! ) ;
267- nodeType = typeChecker . typeToTypeNode ( implicitType ) as ts . TypeNode ;
268- }
269- const type = new TypeResolver ( nodeType , this . current ) . resolve ( ) ;
270- const referenceType : Tsoa . ReferenceType = {
271- refName : typeReference . getText ( ) ,
272- dataType : 'refAlias' ,
273- type,
274- validators : { } ,
275- } ;
276- this . current . AddReferenceType ( referenceType ) ;
277- return referenceType ;
278- }
279-
280257 if ( this . context [ typeReference . typeName . text ] ) {
281258 return new TypeResolver ( this . context [ typeReference . typeName . text ] , this . current , this . parentNode , this . context ) . resolve ( ) ;
282259 }
@@ -478,15 +455,45 @@ export class TypeResolver {
478455 }
479456
480457 private getModelReference ( modelType : ts . InterfaceDeclaration | ts . ClassDeclaration , name : string ) {
458+ const example = this . getNodeExample ( modelType ) ;
459+ const description = this . getNodeDescription ( modelType ) ;
460+
461+ // Handle toJSON methods
462+ let toJSON : ts . ClassElement | ts . TypeElement | undefined ;
463+ if ( ts . isClassDeclaration ( modelType ) ) {
464+ toJSON = modelType . members . find ( member => member . name && member . name . getText ( ) === 'toJSON' ) ;
465+ } else {
466+ toJSON = modelType . members . find ( member => member . name && member . name . getText ( ) === 'toJSON' ) ;
467+ }
468+
469+ if ( toJSON && ( ts . isMethodDeclaration ( toJSON ) || ts . isMethodSignature ( toJSON ) ) ) {
470+ let nodeType = toJSON . type ;
471+ if ( ! nodeType ) {
472+ const typeChecker = this . current . typeChecker ;
473+ const signature = typeChecker . getSignatureFromDeclaration ( toJSON ) ;
474+ const implicitType = typeChecker . getReturnTypeOfSignature ( signature ! ) ;
475+ nodeType = typeChecker . typeToTypeNode ( implicitType ) as ts . TypeNode ;
476+ }
477+ const type = new TypeResolver ( nodeType , this . current ) . resolve ( ) ;
478+ const referenceType : Tsoa . ReferenceType = {
479+ refName : this . getRefTypeName ( name ) ,
480+ dataType : 'refAlias' ,
481+ description,
482+ type,
483+ validators : { } ,
484+ ...( example && { example } ) ,
485+ } ;
486+ return referenceType ;
487+ }
488+
481489 const properties = this . getModelProperties ( modelType ) ;
482490 const additionalProperties = this . getModelAdditionalProperties ( modelType ) ;
483491 const inheritedProperties = this . getModelInheritedProperties ( modelType ) || [ ] ;
484- const example = this . getNodeExample ( modelType ) ;
485492
486493 const referenceType : Tsoa . ReferenceType = {
487494 additionalProperties,
488495 dataType : 'refObject' ,
489- description : this . getNodeDescription ( modelType ) ,
496+ description,
490497 properties : inheritedProperties ,
491498 refName : this . getRefTypeName ( name ) ,
492499 ...( example && { example } ) ,
0 commit comments