Skip to content

Commit fa81e79

Browse files
afonsofdomoritz
andauthored
FIX: Issue YousefED#511: Does not generate definition for Unions and Intersection Types (YousefED#512)
Co-authored-by: Dominik Moritz <[email protected]>
1 parent cc5075c commit fa81e79

File tree

7 files changed

+95
-50
lines changed

7 files changed

+95
-50
lines changed

Diff for: test/programs/default-properties/schema.json

+10-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
3-
"properties": {
4-
"varBoolean": {
3+
"definitions": {
4+
"Foo": {
55
"anyOf": [
66
{
77
"enum": [
@@ -15,41 +15,20 @@
1515
{
1616
"type": "number"
1717
}
18-
],
18+
]
19+
}
20+
},
21+
"properties": {
22+
"varBoolean": {
23+
"$ref": "#/definitions/Foo",
1924
"default": false
2025
},
2126
"varInteger": {
22-
"anyOf": [
23-
{
24-
"enum": [
25-
"a",
26-
"b",
27-
"c",
28-
false,
29-
true
30-
]
31-
},
32-
{
33-
"type": "number"
34-
}
35-
],
27+
"$ref": "#/definitions/Foo",
3628
"default": 123
3729
},
3830
"varString": {
39-
"anyOf": [
40-
{
41-
"enum": [
42-
"a",
43-
"b",
44-
"c",
45-
false,
46-
true
47-
]
48-
},
49-
{
50-
"type": "number"
51-
}
52-
],
31+
"$ref": "#/definitions/Foo",
5332
"default": "123"
5433
}
5534
},

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

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
interface A {}
2+
interface B {}
3+
4+
type C = A | B;
5+
6+
interface MyObject {
7+
c: C;
8+
}

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

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"definitions": {
4+
"A": {
5+
"type": "object"
6+
},
7+
"B": {
8+
"type": "object"
9+
},
10+
"C": {
11+
"anyOf": [
12+
{
13+
"$ref": "#/definitions/A"
14+
},
15+
{
16+
"$ref": "#/definitions/B"
17+
}
18+
]
19+
}
20+
},
21+
"properties": {
22+
"c": {
23+
"$ref": "#/definitions/C"
24+
}
25+
},
26+
"required": [
27+
"c"
28+
],
29+
"type": "object"
30+
}

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

+27-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"definitions": {
4+
"MyType2": {
5+
"type": [
6+
"string",
7+
"number"
8+
]
9+
},
10+
"MyType3": {
11+
"anyOf": [
12+
{
13+
"items": {
14+
"type": "number"
15+
},
16+
"type": "array"
17+
},
18+
{
19+
"type": "string"
20+
}
21+
]
22+
},
423
"MyType6": {
524
"type": "object"
625
}
@@ -13,22 +32,19 @@
1332
]
1433
},
1534
"var2": {
16-
"type": [
17-
"string",
18-
"number",
19-
"null"
35+
"anyOf": [
36+
{
37+
"$ref": "#/definitions/MyType2"
38+
},
39+
{
40+
"type": "null"
41+
}
2042
]
2143
},
2244
"var3": {
2345
"anyOf": [
2446
{
25-
"items": {
26-
"type": "number"
27-
},
28-
"type": "array"
29-
},
30-
{
31-
"type": "string"
47+
"$ref": "#/definitions/MyType3"
3248
},
3349
{
3450
"type": "null"
@@ -91,4 +107,3 @@
91107
],
92108
"type": "object"
93109
}
94-

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
3-
"properties": {
4-
"var1": {
3+
"definitions": {
4+
"MyType1": {
55
"type": [
66
"string",
77
"number"
88
]
99
},
10-
"var2": {
10+
"MyType2": {
1111
"anyOf": [
1212
{
13+
"type": "array",
1314
"items": {
1415
"type": "number"
15-
},
16-
"type": "array"
16+
}
1717
},
1818
{
1919
"type": "string"
2020
}
2121
]
2222
}
2323
},
24+
"properties": {
25+
"var1": {
26+
"$ref": "#/definitions/MyType1"
27+
},
28+
"var2": {
29+
"$ref": "#/definitions/MyType2"
30+
}
31+
},
2432
"required": [
2533
"var1",
2634
"var2"

Diff for: test/schema.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ describe("schema", () => {
215215
aliasRef: true,
216216
topRef: true,
217217
});
218+
assertSchema("type-alias-or", "MyObject");
218219
// disabled because of #80
219220
// assertSchema("type-aliases-recursive-alias-topref", "MyAlias", {
220221
// useTypeAliasRef: true,

Diff for: typescript-json-schema.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1220,14 +1220,18 @@ export class JsonSchemaGenerator {
12201220

12211221
const symbol = typ.getSymbol();
12221222
// FIXME: We can't just compare the name of the symbol - it ignores the namespace
1223-
const isRawType =
1224-
!symbol ||
1223+
let isRawType =
1224+
!symbol ||
12251225
// Window is incorrectly marked as rawType here for some reason
12261226
(this.tc.getFullyQualifiedName(symbol) !== "Window" &&
12271227
(this.tc.getFullyQualifiedName(symbol) === "Date" ||
12281228
symbol.name === "integer" ||
12291229
this.tc.getIndexInfoOfType(typ, ts.IndexKind.Number) !== undefined));
12301230

1231+
if (isRawType && reffedType?.escapedName && (typ as any).types) {
1232+
isRawType = false;
1233+
}
1234+
12311235
// special case: an union where all child are string literals -> make an enum instead
12321236
let isStringEnum = false;
12331237
if (typ.flags & ts.TypeFlags.Union) {

0 commit comments

Comments
 (0)