From 03bb10240655105502c437f61cc9d616159d1843 Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Tue, 4 Jul 2023 14:10:13 -0700 Subject: [PATCH 1/4] Fix emit of bundled schemas when has references and is used multiple times --- .../json-schema/src/json-schema-emitter.ts | 3 +- packages/json-schema/test/bundling.test.ts | 46 +++++++++++++++++++ packages/json-schema/test/utils.ts | 11 ++++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/packages/json-schema/src/json-schema-emitter.ts b/packages/json-schema/src/json-schema-emitter.ts index 9f7c55dfed..34bb425f8a 100644 --- a/packages/json-schema/src/json-schema-emitter.ts +++ b/packages/json-schema/src/json-schema-emitter.ts @@ -522,7 +522,6 @@ export class JsonSchemaEmitter extends TypeEmitter, JSONSche for (const sf of sourceFiles) { const emittedSf = this.emitter.emitSourceFile(sf); - if (sf.meta.shouldEmit) { toEmit.push(emittedSf); } @@ -558,7 +557,7 @@ export class JsonSchemaEmitter extends TypeEmitter, JSONSche throw new Error("Emit error - multiple decls in single schema per file mode"); } - content = decls[0].value; + content = { ...decls[0].value }; if (sourceFile.meta.bundledRefs.length > 0) { // bundle any refs, including refs of refs diff --git a/packages/json-schema/test/bundling.test.ts b/packages/json-schema/test/bundling.test.ts index 75197bc187..a429e9697c 100644 --- a/packages/json-schema/test/bundling.test.ts +++ b/packages/json-schema/test/bundling.test.ts @@ -48,4 +48,50 @@ describe("bundling", () => { assert.strictEqual(schemas["test.json"].$defs, undefined); assert.strictEqual(schemas["Bar.json"].$id, "Bar.json"); }); + + it.only("doesn't create duplicate defs for transitive references", async () => { + const schemas = await emitSchema( + ` + model A {} + + model B { + refA: A; + } + + @jsonSchema + model C { + refB: B; + } + + @jsonSchema + model D { + refB: B; + } + `, + {}, + { emitNamespace: false, emitTypes: ["C", "D"] } + ); + + const depSchemas = { + B: { + $schema: "https://json-schema.org/draft/2020-12/schema", + $id: "B.json", + type: "object", + properties: { + refA: { + $ref: "A.json", + }, + }, + required: ["refA"], + }, + A: { + $schema: "https://json-schema.org/draft/2020-12/schema", + $id: "A.json", + type: "object", + properties: {}, + }, + }; + assert.deepStrictEqual(schemas["C.json"].$defs, depSchemas); + assert.deepStrictEqual(schemas["D.json"].$defs, depSchemas); + }); }); diff --git a/packages/json-schema/test/utils.ts b/packages/json-schema/test/utils.ts index 01b8ab9159..fe573f809d 100644 --- a/packages/json-schema/test/utils.ts +++ b/packages/json-schema/test/utils.ts @@ -23,7 +23,7 @@ export async function getHostForCadlFile(contents: string, decorators?: Record = {}; const files = await emitter.getProgram().host.readDir("./cadl-output"); From a51cc5a64ab73259d85f788b9e8e080c8523f046 Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Tue, 4 Jul 2023 14:12:28 -0700 Subject: [PATCH 2/4] changes --- .../js-fix-duplicate-def_2023-07-04-21-12.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 common/changes/@typespec/json-schema/js-fix-duplicate-def_2023-07-04-21-12.json diff --git a/common/changes/@typespec/json-schema/js-fix-duplicate-def_2023-07-04-21-12.json b/common/changes/@typespec/json-schema/js-fix-duplicate-def_2023-07-04-21-12.json new file mode 100644 index 0000000000..9a80deaa09 --- /dev/null +++ b/common/changes/@typespec/json-schema/js-fix-duplicate-def_2023-07-04-21-12.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@typespec/json-schema", + "comment": "Fix a bug that could result in a schema being bundled more than once.", + "type": "none" + } + ], + "packageName": "@typespec/json-schema" +} \ No newline at end of file From f83e763533b2e85755e8ff06115d469323426982 Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Tue, 4 Jul 2023 14:15:10 -0700 Subject: [PATCH 3/4] no only --- packages/json-schema/test/bundling.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/json-schema/test/bundling.test.ts b/packages/json-schema/test/bundling.test.ts index a429e9697c..7303cddee6 100644 --- a/packages/json-schema/test/bundling.test.ts +++ b/packages/json-schema/test/bundling.test.ts @@ -49,7 +49,7 @@ describe("bundling", () => { assert.strictEqual(schemas["Bar.json"].$id, "Bar.json"); }); - it.only("doesn't create duplicate defs for transitive references", async () => { + it("doesn't create duplicate defs for transitive references", async () => { const schemas = await emitSchema( ` model A {} From 315e9499e7505f8700599b572cf7b9a84b539039 Mon Sep 17 00:00:00 2001 From: Brian Terlson Date: Tue, 4 Jul 2023 14:16:24 -0700 Subject: [PATCH 4/4] undo inadvertant change --- packages/json-schema/src/json-schema-emitter.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/json-schema/src/json-schema-emitter.ts b/packages/json-schema/src/json-schema-emitter.ts index 34bb425f8a..0da5a6cc8f 100644 --- a/packages/json-schema/src/json-schema-emitter.ts +++ b/packages/json-schema/src/json-schema-emitter.ts @@ -522,6 +522,7 @@ export class JsonSchemaEmitter extends TypeEmitter, JSONSche for (const sf of sourceFiles) { const emittedSf = this.emitter.emitSourceFile(sf); + if (sf.meta.shouldEmit) { toEmit.push(emittedSf); }