Skip to content

Commit

Permalink
FIX: Issue #511: Does not generate definition for Unions and Intersec…
Browse files Browse the repository at this point in the history
…tion Types (#512)

Co-authored-by: Dominik Moritz <domoritz@gmail.com>
  • Loading branch information
afonsof and domoritz authored Nov 7, 2022
1 parent cc5075c commit fa81e79
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 50 deletions.
41 changes: 10 additions & 31 deletions test/programs/default-properties/schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"varBoolean": {
"definitions": {
"Foo": {
"anyOf": [
{
"enum": [
Expand All @@ -15,41 +15,20 @@
{
"type": "number"
}
],
]
}
},
"properties": {
"varBoolean": {
"$ref": "#/definitions/Foo",
"default": false
},
"varInteger": {
"anyOf": [
{
"enum": [
"a",
"b",
"c",
false,
true
]
},
{
"type": "number"
}
],
"$ref": "#/definitions/Foo",
"default": 123
},
"varString": {
"anyOf": [
{
"enum": [
"a",
"b",
"c",
false,
true
]
},
{
"type": "number"
}
],
"$ref": "#/definitions/Foo",
"default": "123"
}
},
Expand Down
8 changes: 8 additions & 0 deletions test/programs/type-alias-or/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interface A {}
interface B {}

type C = A | B;

interface MyObject {
c: C;
}
30 changes: 30 additions & 0 deletions test/programs/type-alias-or/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"A": {
"type": "object"
},
"B": {
"type": "object"
},
"C": {
"anyOf": [
{
"$ref": "#/definitions/A"
},
{
"$ref": "#/definitions/B"
}
]
}
},
"properties": {
"c": {
"$ref": "#/definitions/C"
}
},
"required": [
"c"
],
"type": "object"
}
39 changes: 27 additions & 12 deletions test/programs/type-nullable/schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyType2": {
"type": [
"string",
"number"
]
},
"MyType3": {
"anyOf": [
{
"items": {
"type": "number"
},
"type": "array"
},
{
"type": "string"
}
]
},
"MyType6": {
"type": "object"
}
Expand All @@ -13,22 +32,19 @@
]
},
"var2": {
"type": [
"string",
"number",
"null"
"anyOf": [
{
"$ref": "#/definitions/MyType2"
},
{
"type": "null"
}
]
},
"var3": {
"anyOf": [
{
"items": {
"type": "number"
},
"type": "array"
},
{
"type": "string"
"$ref": "#/definitions/MyType3"
},
{
"type": "null"
Expand Down Expand Up @@ -91,4 +107,3 @@
],
"type": "object"
}

18 changes: 13 additions & 5 deletions test/programs/type-union/schema.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"var1": {
"definitions": {
"MyType1": {
"type": [
"string",
"number"
]
},
"var2": {
"MyType2": {
"anyOf": [
{
"type": "array",
"items": {
"type": "number"
},
"type": "array"
}
},
{
"type": "string"
}
]
}
},
"properties": {
"var1": {
"$ref": "#/definitions/MyType1"
},
"var2": {
"$ref": "#/definitions/MyType2"
}
},
"required": [
"var1",
"var2"
Expand Down
1 change: 1 addition & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ describe("schema", () => {
aliasRef: true,
topRef: true,
});
assertSchema("type-alias-or", "MyObject");
// disabled because of #80
// assertSchema("type-aliases-recursive-alias-topref", "MyAlias", {
// useTypeAliasRef: true,
Expand Down
8 changes: 6 additions & 2 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,14 +1220,18 @@ export class JsonSchemaGenerator {

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

if (isRawType && reffedType?.escapedName && (typ as any).types) {
isRawType = false;
}

// special case: an union where all child are string literals -> make an enum instead
let isStringEnum = false;
if (typ.flags & ts.TypeFlags.Union) {
Expand Down

0 comments on commit fa81e79

Please sign in to comment.