Skip to content

Commit

Permalink
[Tcgc] Fix getLibraryName for anonymous model which is derived from t…
Browse files Browse the repository at this point in the history
…emplate (#1377)

After microsoft/typespec#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.
  • Loading branch information
msyyc authored Aug 15, 2024
1 parent e019c34 commit 29b9538
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

Fix getLibraryName for anonymous model which is derived from template
7 changes: 6 additions & 1 deletion packages/typespec-client-generator-core/src/public-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
prop: T
}
op test(): {prop: ModelTemplate<Name>};
`);
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 {
Expand Down

0 comments on commit 29b9538

Please sign in to comment.