Skip to content

Commit 067915e

Browse files
committed
Clean up tests, add test for alias ref, fixes YousefED#77
1 parent d8a2465 commit 067915e

File tree

7 files changed

+76
-15
lines changed

7 files changed

+76
-15
lines changed

Diff for: test/programs/interface-recursion/schema.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
"properties":{
88
"propA":{"type":"number"},
99
"propB":{"$ref": "#/definitions/MyObject"}
10-
}
10+
},
11+
"required": [
12+
"propA",
13+
"propB"
14+
]
1115
}
1216
}
1317
}

Diff for: test/programs/type-alias-single/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type MyString = string;

Diff for: test/programs/type-alias-single/schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"type": "string"
4+
}

Diff for: test/programs/type-aliases/main.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
type MyString = string;
2+
3+
interface MySubObject {
4+
propA: number;
5+
propB: number;
6+
}
7+
8+
interface MyObject {
9+
primitive: MyString;
10+
object: MySubObject;
11+
}

Diff for: test/programs/type-aliases/schema.json

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
11
{
22
"$schema": "http://json-schema.org/draft-04/schema#",
3-
"type": "string"
3+
"definitions": {
4+
"MyString": {
5+
"type": "string"
6+
},
7+
"MySubObject": {
8+
"properties": {
9+
"propA": {
10+
"type": "number"
11+
},
12+
"propB": {
13+
"type": "number"
14+
}
15+
},
16+
"type": "object",
17+
"required": [
18+
"propA",
19+
"propB"
20+
]
21+
}
22+
},
23+
"properties": {
24+
"object": {
25+
"$ref": "#/definitions/MySubObject"
26+
},
27+
"primitive": {
28+
"$ref": "#/definitions/MyString"
29+
}
30+
},
31+
"required": [
32+
"primitive",
33+
"object"
34+
],
35+
"type": "object"
436
}

Diff for: test/schema.test.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ const ajv = new Ajv();
99

1010
const base = "test/programs/";
1111

12-
export function assertSchema(group: string, name: string, type: string, settings?: any, compilerOptions?: CompilerOptions) {
12+
export function assertSchema(group: string, name: string, type: string, settings: any = {}, compilerOptions?: CompilerOptions) {
1313
it(group + " should create correct schema", function() {
14-
if (!settings) {
15-
settings = TJS.getDefaultArgs();
16-
settings.generateRequired = true;
14+
const defaults = TJS.getDefaultArgs();
15+
defaults.generateRequired = true;
16+
17+
for (let pref in defaults) {
18+
if (!(pref in settings)) {
19+
settings[pref] = defaults[pref];
20+
}
1721
}
1822

1923
const actual = TJS.generateSchema(TJS.getProgramFromFiles([resolve(base + group + "/" + name)], compilerOptions), type, settings);
@@ -37,9 +41,9 @@ describe("schema", function () {
3741
assertSchema("interface-single", "main.ts", "MyObject");
3842
assertSchema("interface-multi", "main.ts", "MyObject");
3943

40-
let settings = TJS.getDefaultArgs();
41-
settings.useRootRef = true;
42-
assertSchema("interface-recursion", "main.ts", "MyObject", settings); // this sample needs rootRef
44+
assertSchema("interface-recursion", "main.ts", "MyObject", {
45+
useRootRef: true
46+
});
4347

4448
assertSchema("module-interface-single", "main.ts", "MyObject");
4549

@@ -62,7 +66,10 @@ describe("schema", function () {
6266
assertSchema("type-union", "main.ts", "MyObject");
6367
assertSchema("type-intersection", "main.ts", "MyObject");
6468

65-
assertSchema("type-aliases", "main.ts", "MyString");
69+
assertSchema("type-alias-single", "main.ts", "MyString");
70+
assertSchema("type-aliases", "main.ts", "MyObject", {
71+
useTypeAliasRef: true
72+
});
6673
assertSchema("type-aliases-fixed-size-array", "main.ts", "MyFixedSizeArray");
6774
assertSchema("type-aliases-multitype-array", "main.ts", "MyArray");
6875
assertSchema("type-anonymous", "main.ts", "MyObject");

Diff for: typescript-json-schema.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import * as stringify from "json-stable-stringify";
66

77
const vm = require("vm");
88

9+
const REGEX_FILE_NAME = /".*"\./;
10+
const REGEX_TJS_JSDOC = /^-([\w]+)\s([\w]+)/g;
11+
912
export function getDefaultArgs() {
1013
return {
1114
useRef: true,
@@ -119,7 +122,7 @@ export class JsonSchemaGenerator {
119122
const jsdocs = symbol.getJsDocTags();
120123
jsdocs.forEach(doc => {
121124
// 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];
125+
const [name, text] = doc.name === "TJS" ? new RegExp(REGEX_TJS_JSDOC).exec(doc.text).slice(1,3) : [doc.name, doc.text];
123126
if (JsonSchemaGenerator.validationKeywords[name]) {
124127
definition[name] = this.parseValue(text);
125128
} else {
@@ -577,16 +580,16 @@ export class JsonSchemaGenerator {
577580
const asTypeAliasRef = asRef && reffedType && (this.args.useTypeAliasRef || isStringEnum);
578581
if (!asTypeAliasRef) {
579582
if (isRawType || typ.getFlags() & ts.TypeFlags.Object && (<ts.ObjectType>typ).objectFlags & ts.ObjectFlags.Anonymous) {
580-
asRef = false; // raw types and inline types cannot be reffed,
583+
asRef = false; // raw types and inline types cannot be reffed,
581584
// unless we are handling a type alias
582585
}
583586
}
584587

585588
let fullTypeName = "";
586-
if (asRef) {
589+
if (asTypeAliasRef) {
590+
fullTypeName = tc.getFullyQualifiedName(reffedType).replace(REGEX_FILE_NAME, "");
591+
} else if (asRef) {
587592
fullTypeName = tc.typeToString(typ, undefined, ts.TypeFormatFlags.UseFullyQualifiedType);
588-
} else if (asTypeAliasRef) {
589-
fullTypeName = tc.getFullyQualifiedName(reffedType);
590593
}
591594

592595
if (asRef) {

0 commit comments

Comments
 (0)