From 5530873597e3aeeadad5be4273f953957a957be6 Mon Sep 17 00:00:00 2001 From: Tiberiu Musat Date: Mon, 10 Oct 2022 13:34:27 +0200 Subject: [PATCH 1/5] Added parameters field to definition interface --- typescript-json-schema.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index d1e8f561..9c009e73 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -121,6 +121,7 @@ export interface Definition extends Omit { propertyOrder?: string[]; defaultProperties?: string[]; typeof?: "function"; + parameters?: Definition[]; // Fields that must be redefined because they make use of this definition itself items?: DefinitionOrBoolean | DefinitionOrBoolean[]; From e90789e73aa123d80db61496fdb5e23670019118 Mon Sep 17 00:00:00 2001 From: Tiberiu Musat Date: Mon, 10 Oct 2022 13:52:00 +0200 Subject: [PATCH 2/5] Added function type definition --- typescript-json-schema.ts | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index 9c009e73..0e7d396e 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -553,16 +553,16 @@ export class JsonSchemaGenerator { if (comments.length) { definition.description = comments .map((comment) => { - const newlineNormalizedComment = comment.text.replace(/\r\n/g, "\n"); + const newlineNormalizedComment = comment.text.replace(/\r\n/g, "\n"); - // If a comment contains a "{@link XYZ}" inline tag that could not be - // resolved by the TS checker, then this comment will contain a trailing - // whitespace that we need to remove. - if (comment.kind === "linkText") { - return newlineNormalizedComment.trim(); - } + // If a comment contains a "{@link XYZ}" inline tag that could not be + // resolved by the TS checker, then this comment will contain a trailing + // whitespace that we need to remove. + if (comment.kind === "linkText") { + return newlineNormalizedComment.trim(); + } - return newlineNormalizedComment; + return newlineNormalizedComment; }) .join("").trim(); } @@ -1003,6 +1003,19 @@ export class JsonSchemaGenerator { return definition; } + private getFunctionDefinition(funcType: ts.Type, definition: Definition): Definition { + const node = funcType.getSymbol()!.getDeclarations()![0]; + + // tiberiu + const parameters = (node as ts.FunctionLikeDeclaration).parameters; + definition.typeof = "function"; + definition.parameters = parameters + .map((p) => this.tc.getTypeAtLocation(p.type)) + .map((type) => this.getTypeDefinition(type)); + + return definition; + } + private getClassDefinition(clazzType: ts.Type, definition: Definition): Definition { const node = clazzType.getSymbol()!.getDeclarations()![0]; @@ -1375,6 +1388,8 @@ export class JsonSchemaGenerator { // {} is TypeLiteral with no members. Need special case because it doesn't have declarations. definition.type = "object"; definition.properties = {}; + } else if (ts.isFunctionLike(node)) { + this.getFunctionDefinition(typ, definition); } else { this.getClassDefinition(typ, definition); } From deb118ce3224620a475f717da9e1d8e5447466da Mon Sep 17 00:00:00 2001 From: Tiberiu Musat Date: Mon, 10 Oct 2022 13:54:26 +0200 Subject: [PATCH 3/5] Removed old func type defs --- typescript-json-schema.ts | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index 0e7d396e..bb393bf2 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -1025,11 +1025,6 @@ export class JsonSchemaGenerator { return definition; } - if (this.args.typeOfKeyword && node.kind === ts.SyntaxKind.FunctionType) { - definition.typeof = "function"; - return definition; - } - const clazz = node; const props = this.tc.getPropertiesOfType(clazzType).filter((prop) => { // filter never @@ -1209,15 +1204,6 @@ export class JsonSchemaGenerator { reffedType = undefined; } - if ( - this.args.typeOfKeyword && - typ.flags & ts.TypeFlags.Object && - (typ).objectFlags & ts.ObjectFlags.Anonymous - ) { - definition.typeof = "function"; - return definition; - } - let returnedDefinition = definition; // returned definition, may be a $ref // Parse property comments now to skip recursive if ignore. From 056ae68ea43b2929021e0b0c0cf2e896115e6e6e Mon Sep 17 00:00:00 2001 From: Tiberiu Musat Date: Mon, 10 Oct 2022 13:55:36 +0200 Subject: [PATCH 4/5] Added test for typeOfKeyword argument --- typescript-json-schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index bb393bf2..3fb634b5 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -1374,7 +1374,7 @@ export class JsonSchemaGenerator { // {} is TypeLiteral with no members. Need special case because it doesn't have declarations. definition.type = "object"; definition.properties = {}; - } else if (ts.isFunctionLike(node)) { + } else if (this.args.typeOfKeyword && ts.isFunctionLike(node)) { this.getFunctionDefinition(typ, definition); } else { this.getClassDefinition(typ, definition); From 8616b1cb2acbaf73073395bb1ddfab1b8047362b Mon Sep 17 00:00:00 2001 From: Tiberiu Musat Date: Mon, 10 Oct 2022 14:01:15 +0200 Subject: [PATCH 5/5] fixed errors --- typescript-json-schema.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/typescript-json-schema.ts b/typescript-json-schema.ts index 3fb634b5..522d489c 100644 --- a/typescript-json-schema.ts +++ b/typescript-json-schema.ts @@ -1006,11 +1006,10 @@ export class JsonSchemaGenerator { private getFunctionDefinition(funcType: ts.Type, definition: Definition): Definition { const node = funcType.getSymbol()!.getDeclarations()![0]; - // tiberiu const parameters = (node as ts.FunctionLikeDeclaration).parameters; definition.typeof = "function"; definition.parameters = parameters - .map((p) => this.tc.getTypeAtLocation(p.type)) + .map((p) => this.tc.getTypeAtLocation(p.type!)) .map((type) => this.getTypeDefinition(type)); return definition; @@ -1374,7 +1373,7 @@ export class JsonSchemaGenerator { // {} is TypeLiteral with no members. Need special case because it doesn't have declarations. definition.type = "object"; definition.properties = {}; - } else if (this.args.typeOfKeyword && ts.isFunctionLike(node)) { + } else if (this.args.typeOfKeyword && node && ts.isFunctionLike(node)) { this.getFunctionDefinition(typ, definition); } else { this.getClassDefinition(typ, definition);