From 29b9538ac9be5816addbf19eef29f87bdc215806 Mon Sep 17 00:00:00 2001 From: Yuchao Yan Date: Thu, 15 Aug 2024 13:40:20 +0800 Subject: [PATCH] [Tcgc] Fix getLibraryName for anonymous model which is derived from template (#1377) After https://github.com/microsoft/typespec/pull/4144, anonymous model type may also have templateMapper arguments. For this scenario, `getLibraryName` shall not try to generate name from arguments, otherwise may generate model with duplicated name. --- ...c-fix-get-library-name-2024-7-15-13-4-27.md | 7 +++++++ .../src/public-utils.ts | 7 ++++++- .../test/types/model-types.test.ts | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 .chronus/changes/tcgc-fix-get-library-name-2024-7-15-13-4-27.md diff --git a/.chronus/changes/tcgc-fix-get-library-name-2024-7-15-13-4-27.md b/.chronus/changes/tcgc-fix-get-library-name-2024-7-15-13-4-27.md new file mode 100644 index 0000000000..fce4339240 --- /dev/null +++ b/.chronus/changes/tcgc-fix-get-library-name-2024-7-15-13-4-27.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +Fix getLibraryName for anonymous model which is derived from template \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/public-utils.ts b/packages/typespec-client-generator-core/src/public-utils.ts index ac0bccf24f..f3633cd07b 100644 --- a/packages/typespec-client-generator-core/src/public-utils.ts +++ b/packages/typespec-client-generator-core/src/public-utils.ts @@ -170,7 +170,12 @@ export function getLibraryName( if (friendlyName) return friendlyName; // 5. if type is derived from template and name is the same as template, add template parameters' name as suffix - if (typeof type.name === "string" && type.kind === "Model" && type.templateMapper?.args) { + if ( + typeof type.name === "string" && + type.name !== "" && + type.kind === "Model" && + type.templateMapper?.args + ) { return ( type.name + type.templateMapper.args diff --git a/packages/typespec-client-generator-core/test/types/model-types.test.ts b/packages/typespec-client-generator-core/test/types/model-types.test.ts index 177976849c..f91ede158a 100644 --- a/packages/typespec-client-generator-core/test/types/model-types.test.ts +++ b/packages/typespec-client-generator-core/test/types/model-types.test.ts @@ -320,6 +320,24 @@ describe("typespec-client-generator-core: model types", () => { strictEqual(dog.discriminatorProperty, dogKindProperty); }); + it("anonymous model contains template", async () => { + await runner.compileWithBuiltInService(` + + model Name { + name: string; + } + model ModelTemplate { + prop: T + } + + op test(): {prop: ModelTemplate}; + `); + const models = runner.context.sdkPackage.models; + strictEqual(models.length, 3); + const modelNames = models.map((model) => model.name).sort(); + deepStrictEqual(modelNames, ["TestResponse", "Name", "ModelTemplateName"].sort()); + }); + it("union to extensible enum values", async () => { await runner.compileWithBuiltInService(` union PetKind {