Skip to content

Commit

Permalink
Fix type primitives test case. Problem was that {} does not have decl…
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Jan 15, 2017
1 parent 8be3ade commit 191e7da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion test/programs/type-primitives/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class MyObject {

number1: number = 1;

/** @type integer */
/** @TJS-type integer */
integer1: number = 1;
integer2: integer = 1;

Expand Down
18 changes: 12 additions & 6 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ export class JsonSchemaGenerator {
// jsdocs are separate from comments
const jsdocs = symbol.getJsDocTags();
jsdocs.forEach(doc => {
if (JsonSchemaGenerator.validationKeywords[doc.name] || JsonSchemaGenerator.validationKeywords["TJS-" + doc.name]) {
definition[doc.name] = this.parseValue(doc.text);
// if we have @TJS-... annotations, we have to parse them
const [name, text] = doc.name === "TJS" ? /^-([\w]+)\s([\w]+)/g.exec(doc.text).slice(1,3) : [doc.name, doc.text];
if (JsonSchemaGenerator.validationKeywords[name]) {
definition[name] = this.parseValue(text);
} else {
// special annotations
otherAnnotations[doc.name] = true;
Expand Down Expand Up @@ -429,20 +431,20 @@ export class JsonSchemaGenerator {
if(props.length === 0 && clazz.members && clazz.members.length === 1 && clazz.members[0].kind === ts.SyntaxKind.IndexSignature) {
// for case "array-types"
const indexSignature = <ts.IndexSignatureDeclaration>clazz.members[0];
if(indexSignature.parameters.length !== 1) {
if (indexSignature.parameters.length !== 1) {
throw "Not supported: IndexSignatureDeclaration parameters.length != 1";
}
const indexSymbol: ts.Symbol = (<any>indexSignature.parameters[0]).symbol;
const indexType = tc.getTypeOfSymbolAtLocation(indexSymbol, node);
const isStringIndexed = (indexType.flags === ts.TypeFlags.String);
if(indexType.flags !== ts.TypeFlags.Number && !isStringIndexed) {
if (indexType.flags !== ts.TypeFlags.Number && !isStringIndexed) {
throw "Not supported: IndexSignatureDeclaration with index symbol other than a number or a string";
}

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

if(isStringIndexed) {
if (isStringIndexed) {
definition.type = "object";
definition.additionalProperties = def;
} else {
Expand Down Expand Up @@ -611,7 +613,7 @@ export class JsonSchemaGenerator {
definition.title = fullTypeName;
}
}
const node = symbol ? symbol.getDeclarations()[0] : null;
const node = symbol && symbol.getDeclarations() !== undefined ? symbol.getDeclarations()[0] : null;
if (typ.flags & ts.TypeFlags.Union) {
this.getUnionDefinition(typ as ts.UnionType, prop, tc, unionModifier, definition);
} else if (typ.flags & ts.TypeFlags.Intersection) {
Expand All @@ -624,6 +626,10 @@ export class JsonSchemaGenerator {
this.getDefinitionForRootType(typ, tc, reffedType, definition);
} else if (node && (node.kind === ts.SyntaxKind.EnumDeclaration || node.kind === ts.SyntaxKind.EnumMember)) {
this.getEnumDefinition(typ, tc, definition);
} else if (symbol && symbol.flags & ts.SymbolFlags.TypeLiteral && Object.keys(symbol.members).length === 0) {
// {} is TypeLiteral with no members. Need special case because it doesn't have declarations.
definition.type = "object";
definition.properties = {};
} else {
this.getClassDefinition(typ, tc, definition);
}
Expand Down

0 comments on commit 191e7da

Please sign in to comment.