Skip to content

Commit 356aae2

Browse files
jfurlerdomoritz
authored andcommitted
Fixing error thrown when field is of type "Function"
1 parent 05ba3db commit 356aae2

File tree

5 files changed

+84
-23
lines changed

5 files changed

+84
-23
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ test/*.js
33
test/*.d.ts
44
test/*.map
55
.vscode/*.*
6-
npm-debug.log
6+
npm-debug.log
7+
.idea

test/programs/type-function/main.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface MyObject {
2+
myFunction: Function;
3+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"definitions": {
4+
"Function": {
5+
"description": "Creates a new function.",
6+
"properties": {
7+
"apply": {
8+
"description": "Calls the function, substituting the specified object for the this value of the function, and the specified array for the arguments of the function.",
9+
"type": "object"
10+
},
11+
"arguments": {},
12+
"bind": {
13+
"description": "For a given function, creates a bound function that has the same body as the original function.\nThe this object of the bound function is associated with the specified object, and has the specified initial parameters.",
14+
"type": "object"
15+
},
16+
"call": {
17+
"description": "Calls a method of an object, substituting another object for the current object.",
18+
"type": "object"
19+
},
20+
"caller": {
21+
"$ref": "#/definitions/Function"
22+
},
23+
"length": {
24+
"type": "number"
25+
},
26+
"prototype": {},
27+
"toString": {
28+
"description": "Returns a string representation of a function.",
29+
"type": "object"
30+
}
31+
},
32+
"required": [
33+
"apply",
34+
"arguments",
35+
"bind",
36+
"call",
37+
"caller",
38+
"length",
39+
"prototype",
40+
"toString"
41+
],
42+
"type": "object"
43+
}
44+
},
45+
"properties": {
46+
"myFunction": {
47+
"$ref": "#/definitions/Function"
48+
}
49+
},
50+
"required": [
51+
"myFunction"
52+
],
53+
"type": "object"
54+
}

test/schema.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ describe("schema", () => {
170170
assertSchema("type-anonymous", "MyObject");
171171
assertSchema("type-primitives", "MyObject");
172172
assertSchema("type-nullable", "MyObject");
173+
assertSchema("type-function", "MyObject");
173174
});
174175

175176
describe("class and interface", () => {
@@ -207,7 +208,7 @@ describe("schema", () => {
207208
assertSchema("string-literals-inline", "MyObject");
208209
});
209210

210-
describe("namspeaces", () => {
211+
describe("namespaces", () => {
211212
assertSchema("namespace", "Type");
212213
assertSchema("namespace-deep-1", "RootNamespace.Def");
213214
assertSchema("namespace-deep-2", "RootNamespace.SubNamespace.HelperA");

typescript-json-schema.ts

+23-21
Original file line numberDiff line numberDiff line change
@@ -543,29 +543,31 @@ export class JsonSchemaGenerator {
543543

544544
definition.oneOf = oneOf;
545545
} else {
546-
const indexSignatures = clazz.members.filter(x => x.kind === ts.SyntaxKind.IndexSignature);
547-
if (indexSignatures.length === 1) {
548-
// for case "array-types"
549-
const indexSignature = indexSignatures[0] as ts.IndexSignatureDeclaration;
550-
if (indexSignature.parameters.length !== 1) {
551-
throw "Not supported: IndexSignatureDeclaration parameters.length != 1";
552-
}
553-
const indexSymbol: ts.Symbol = (<any>indexSignature.parameters[0]).symbol;
554-
const indexType = tc.getTypeOfSymbolAtLocation(indexSymbol, node);
555-
const isStringIndexed = (indexType.flags === ts.TypeFlags.String);
556-
if (indexType.flags !== ts.TypeFlags.Number && !isStringIndexed) {
557-
throw "Not supported: IndexSignatureDeclaration with index symbol other than a number or a string";
558-
}
546+
if (clazz.members) {
547+
const indexSignatures = clazz.members.filter(x => x.kind === ts.SyntaxKind.IndexSignature);
548+
if (indexSignatures.length === 1) {
549+
// for case "array-types"
550+
const indexSignature = indexSignatures[0] as ts.IndexSignatureDeclaration;
551+
if (indexSignature.parameters.length !== 1) {
552+
throw "Not supported: IndexSignatureDeclaration parameters.length != 1";
553+
}
554+
const indexSymbol: ts.Symbol = (<any>indexSignature.parameters[0]).symbol;
555+
const indexType = tc.getTypeOfSymbolAtLocation(indexSymbol, node);
556+
const isStringIndexed = (indexType.flags === ts.TypeFlags.String);
557+
if (indexType.flags !== ts.TypeFlags.Number && !isStringIndexed) {
558+
throw "Not supported: IndexSignatureDeclaration with index symbol other than a number or a string";
559+
}
559560

560-
const typ = tc.getTypeAtLocation(indexSignature.type!);
561-
const def = this.getTypeDefinition(typ, tc, undefined, "anyOf");
561+
const typ = tc.getTypeAtLocation(indexSignature.type!);
562+
const def = this.getTypeDefinition(typ, tc, undefined, "anyOf");
562563

563-
if (isStringIndexed) {
564-
definition.type = "object";
565-
definition.additionalProperties = def;
566-
} else {
567-
definition.type = "array";
568-
definition.items = def;
564+
if (isStringIndexed) {
565+
definition.type = "object";
566+
definition.additionalProperties = def;
567+
} else {
568+
definition.type = "array";
569+
definition.items = def;
570+
}
569571
}
570572
}
571573

0 commit comments

Comments
 (0)