Skip to content

Commit

Permalink
fix: return overrides schema in getSchemaForSymbol (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
lundibundi authored Feb 15, 2024
1 parent 0a963ca commit 010adce
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
46 changes: 46 additions & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,52 @@ describe("interfaces", () => {
assert.deepEqual(schema.definitions!["MySubObject"], schemaOverride);
}
});
it("should output the schemas set by setSchemaOverride with getSchemaForSymbol", () => {
const program = TJS.getProgramFromFiles([resolve(BASE + "interface-multi/main.ts")]);
const generator = TJS.buildGenerator(program);
assert(generator !== null);

const schemaOverride1: TJS.Definition = { type: "string" };
const schemaOverride2: TJS.Definition = { type: "integer" };

generator?.setSchemaOverride("MySubObject", schemaOverride1);
generator?.setSchemaOverride("MySubObject2", schemaOverride2);
const schema = generator?.getSchemaForSymbol("MySubObject");

// Should not change original schema object.
assert.deepEqual(schemaOverride1, { type: "string" });
assert.deepEqual(schemaOverride2, { type: "integer" });

assert.deepEqual(schema, { ...schemaOverride1, $schema: "http://json-schema.org/draft-07/schema#" });
});
it("should output the schemas set by setSchemaOverride with getSchemaForSymbol and other overrides", () => {
const program = TJS.getProgramFromFiles([resolve(BASE + "interface-multi/main.ts")]);
const generator = TJS.buildGenerator(program);
assert(generator !== null);
const schemaOverride1: TJS.Definition = { type: "string" };
const schemaOverride2: TJS.Definition = { type: "integer" };

generator?.setSchemaOverride("MySubObject1", schemaOverride1);
generator?.setSchemaOverride("MySubObject2", schemaOverride2);
const schema = generator?.getSchemaForSymbol("MySubObject1", true, true);

// Should not change original schema object.
assert.deepEqual(schemaOverride1, { type: "string" });
assert.deepEqual(schemaOverride2, { type: "integer" });

assert.deepEqual(schema, {
...schemaOverride1,
$schema: "http://json-schema.org/draft-07/schema#",
definitions: {
MySubObject1: {
type: "string"
},
MySubObject2: {
type: "integer"
}
}
});
});
it("should ignore type aliases that have schema overrides", () => {
const program = TJS.getProgramFromFiles([resolve(BASE + "type-alias-schema-override/main.ts")]);
const generator = TJS.buildGenerator(program);
Expand Down
24 changes: 15 additions & 9 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1518,20 +1518,26 @@ export class JsonSchemaGenerator {
}

public getSchemaForSymbol(symbolName: string, includeReffedDefinitions: boolean = true, includeAllOverrides: boolean = false): Definition {
if (!this.allSymbols[symbolName]) {
const overrideDefinition = this.schemaOverrides.get(symbolName);
if (!this.allSymbols[symbolName] && !overrideDefinition) {
throw new Error(`type ${symbolName} not found`);
}

this.resetSchemaSpecificProperties(includeAllOverrides);

const def = this.getTypeDefinition(
this.allSymbols[symbolName],
this.args.topRef,
undefined,
undefined,
undefined,
this.userSymbols[symbolName] || undefined
);
let def;
if (overrideDefinition) {
def = { ...overrideDefinition };
} else {
def = overrideDefinition ? overrideDefinition : this.getTypeDefinition(
this.allSymbols[symbolName],
this.args.topRef,
undefined,
undefined,
undefined,
this.userSymbols[symbolName] || undefined
);
}

if (this.args.ref && includeReffedDefinitions && Object.keys(this.reffedDefinitions).length > 0) {
def.definitions = this.reffedDefinitions;
Expand Down

0 comments on commit 010adce

Please sign in to comment.