Skip to content

Commit 8157a7b

Browse files
authored
Union types may be considered as reffedTypes (#482)
1 parent 7146019 commit 8157a7b

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

typescript-json-schema.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { JSONSchema7 } from "json-schema";
77
import { pathEqual } from "path-equal";
88
export { Program, CompilerOptions, Symbol } from "typescript";
99

10-
1110
const vm = require("vm");
1211

1312
const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"\)|".*?")\.| /g;
@@ -512,7 +511,6 @@ export class JsonSchemaGenerator {
512511
this.inheritingTypes = inheritingTypes;
513512
this.tc = tc;
514513
this.userValidationKeywords = args.validationKeywords.reduce((acc, word) => ({ ...acc, [word]: true }), {});
515-
516514
}
517515

518516
public get ReffedDefinitions(): { [key: string]: Definition } {
@@ -528,14 +526,14 @@ export class JsonSchemaGenerator {
528526
}
529527

530528
private resetSchemaSpecificProperties() {
531-
this.reffedDefinitions = {};
532-
this.typeIdsByName = {};
533-
this.typeNamesById = {};
534-
535-
// restore schema overrides
536-
this.schemaOverrides.forEach((value, key) => {
537-
this.reffedDefinitions[key] = value;
538-
});
529+
this.reffedDefinitions = {};
530+
this.typeIdsByName = {};
531+
this.typeNamesById = {};
532+
533+
// restore schema overrides
534+
this.schemaOverrides.forEach((value, key) => {
535+
this.reffedDefinitions[key] = value;
536+
});
539537
}
540538

541539
/**
@@ -564,7 +562,7 @@ export class JsonSchemaGenerator {
564562
jsdocs.forEach((doc) => {
565563
// if we have @TJS-... annotations, we have to parse them
566564
let name = doc.name;
567-
const originalText = doc.text ? doc.text.map(t => t.text).join("") : "";
565+
const originalText = doc.text ? doc.text.map((t) => t.text).join("") : "";
568566
let text = originalText;
569567
// In TypeScript versions prior to 3.7, it stops parsing the annotation
570568
// at the first non-alphanumeric character and puts the rest of the line as the
@@ -643,7 +641,6 @@ export class JsonSchemaGenerator {
643641
fixedTypes.splice(fixedTypes.length - 1, 1);
644642
} else {
645643
definition.maxItems = targetTupleType.fixedLength;
646-
647644
}
648645
} else {
649646
const propertyTypeString = this.tc.typeToString(
@@ -876,7 +873,8 @@ export class JsonSchemaGenerator {
876873
if (value !== undefined) {
877874
pushEnumValue(value);
878875
} else {
879-
const def = this.getTypeDefinition(valueType);
876+
const symbol = valueType.aliasSymbol;
877+
const def = this.getTypeDefinition(valueType, undefined, undefined, symbol, symbol);
880878
if (def.type === "undefined") {
881879
if (prop) {
882880
(<any>prop).mayBeUndefined = true;
@@ -994,7 +992,6 @@ export class JsonSchemaGenerator {
994992
return definition;
995993
}
996994

997-
998995
private getClassDefinition(clazzType: ts.Type, definition: Definition): Definition {
999996
const node = clazzType.getSymbol()!.getDeclarations()![0];
1000997

@@ -1476,7 +1473,7 @@ export class JsonSchemaGenerator {
14761473
if (onlyIncludeFiles === undefined) {
14771474
return !file.isDeclarationFile;
14781475
}
1479-
return onlyIncludeFiles.filter(f => pathEqual(f,file.fileName)).length > 0;
1476+
return onlyIncludeFiles.filter((f) => pathEqual(f, file.fileName)).length > 0;
14801477
}
14811478
const files = program.getSourceFiles().filter(includeFile);
14821479
if (files.length) {
@@ -1697,7 +1694,7 @@ export async function exec(filePattern: string, fullTypeName: string, args = get
16971694
onlyIncludeFiles = glob.sync(filePattern);
16981695
program = getProgramFromFiles(onlyIncludeFiles, {
16991696
strictNullChecks: args.strictNullChecks,
1700-
esModuleInterop: args.esModuleInterop
1697+
esModuleInterop: args.esModuleInterop,
17011698
});
17021699
onlyIncludeFiles = onlyIncludeFiles.map(normalizeFileName);
17031700
}

0 commit comments

Comments
 (0)