Skip to content

Commit 191e7da

Browse files
committed
Fix type primitives test case. Problem was that {} does not have declarations and microsoft/TypeScript#13498
1 parent 8be3ade commit 191e7da

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

test/programs/type-primitives/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class MyObject {
77

88
number1: number = 1;
99

10-
/** @type integer */
10+
/** @TJS-type integer */
1111
integer1: number = 1;
1212
integer2: integer = 1;
1313

typescript-json-schema.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ export class JsonSchemaGenerator {
118118
// jsdocs are separate from comments
119119
const jsdocs = symbol.getJsDocTags();
120120
jsdocs.forEach(doc => {
121-
if (JsonSchemaGenerator.validationKeywords[doc.name] || JsonSchemaGenerator.validationKeywords["TJS-" + doc.name]) {
122-
definition[doc.name] = this.parseValue(doc.text);
121+
// if we have @TJS-... annotations, we have to parse them
122+
const [name, text] = doc.name === "TJS" ? /^-([\w]+)\s([\w]+)/g.exec(doc.text).slice(1,3) : [doc.name, doc.text];
123+
if (JsonSchemaGenerator.validationKeywords[name]) {
124+
definition[name] = this.parseValue(text);
123125
} else {
124126
// special annotations
125127
otherAnnotations[doc.name] = true;
@@ -429,20 +431,20 @@ export class JsonSchemaGenerator {
429431
if(props.length === 0 && clazz.members && clazz.members.length === 1 && clazz.members[0].kind === ts.SyntaxKind.IndexSignature) {
430432
// for case "array-types"
431433
const indexSignature = <ts.IndexSignatureDeclaration>clazz.members[0];
432-
if(indexSignature.parameters.length !== 1) {
434+
if (indexSignature.parameters.length !== 1) {
433435
throw "Not supported: IndexSignatureDeclaration parameters.length != 1";
434436
}
435437
const indexSymbol: ts.Symbol = (<any>indexSignature.parameters[0]).symbol;
436438
const indexType = tc.getTypeOfSymbolAtLocation(indexSymbol, node);
437439
const isStringIndexed = (indexType.flags === ts.TypeFlags.String);
438-
if(indexType.flags !== ts.TypeFlags.Number && !isStringIndexed) {
440+
if (indexType.flags !== ts.TypeFlags.Number && !isStringIndexed) {
439441
throw "Not supported: IndexSignatureDeclaration with index symbol other than a number or a string";
440442
}
441443

442444
const typ = tc.getTypeAtLocation(indexSignature.type);
443445
const def = this.getTypeDefinition(typ, tc, undefined, "anyOf");
444446

445-
if(isStringIndexed) {
447+
if (isStringIndexed) {
446448
definition.type = "object";
447449
definition.additionalProperties = def;
448450
} else {
@@ -611,7 +613,7 @@ export class JsonSchemaGenerator {
611613
definition.title = fullTypeName;
612614
}
613615
}
614-
const node = symbol ? symbol.getDeclarations()[0] : null;
616+
const node = symbol && symbol.getDeclarations() !== undefined ? symbol.getDeclarations()[0] : null;
615617
if (typ.flags & ts.TypeFlags.Union) {
616618
this.getUnionDefinition(typ as ts.UnionType, prop, tc, unionModifier, definition);
617619
} else if (typ.flags & ts.TypeFlags.Intersection) {
@@ -624,6 +626,10 @@ export class JsonSchemaGenerator {
624626
this.getDefinitionForRootType(typ, tc, reffedType, definition);
625627
} else if (node && (node.kind === ts.SyntaxKind.EnumDeclaration || node.kind === ts.SyntaxKind.EnumMember)) {
626628
this.getEnumDefinition(typ, tc, definition);
629+
} else if (symbol && symbol.flags & ts.SymbolFlags.TypeLiteral && Object.keys(symbol.members).length === 0) {
630+
// {} is TypeLiteral with no members. Need special case because it doesn't have declarations.
631+
definition.type = "object";
632+
definition.properties = {};
627633
} else {
628634
this.getClassDefinition(typ, tc, definition);
629635
}

0 commit comments

Comments
 (0)