From 354be86b91a1a8e1ab60513d323162435b92a286 Mon Sep 17 00:00:00 2001 From: Chenjie Shi Date: Wed, 18 Sep 2024 22:40:51 +0800 Subject: [PATCH] [tcgc] handle orphan types in nested namespaces (#1558) --- ...n-model-in-nested-ns-2024-8-18-15-54-46.md | 7 +++++ .../src/types.ts | 27 +++++++++++-------- .../test/decorators/usage.test.ts | 24 +++++++++++++++++ 3 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 .chronus/changes/handle-orphan-model-in-nested-ns-2024-8-18-15-54-46.md diff --git a/.chronus/changes/handle-orphan-model-in-nested-ns-2024-8-18-15-54-46.md b/.chronus/changes/handle-orphan-model-in-nested-ns-2024-8-18-15-54-46.md new file mode 100644 index 0000000000..343879fd8e --- /dev/null +++ b/.chronus/changes/handle-orphan-model-in-nested-ns-2024-8-18-15-54-46.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +handle orphan types in nested namespaces \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/types.ts b/packages/typespec-client-generator-core/src/types.ts index 8f583aec32..e872fb494b 100644 --- a/packages/typespec-client-generator-core/src/types.ts +++ b/packages/typespec-client-generator-core/src/types.ts @@ -1843,17 +1843,22 @@ export function getAllModelsWithDiagnostics( } // update for orphan models/enums/unions for (const client of listClients(context)) { - // orphan models - for (const model of client.service.models.values()) { - diagnostics.pipe(handleServiceOrphanType(context, model)); - } - // orphan enums - for (const enumType of client.service.enums.values()) { - diagnostics.pipe(handleServiceOrphanType(context, enumType)); - } - // orphan unions - for (const unionType of client.service.unions.values()) { - diagnostics.pipe(handleServiceOrphanType(context, unionType)); + const namespaces = [client.service]; + while (namespaces.length) { + const namespace = namespaces.pop()!; + // orphan models + for (const model of namespace.models.values()) { + diagnostics.pipe(handleServiceOrphanType(context, model)); + } + // orphan enums + for (const enumType of namespace.enums.values()) { + diagnostics.pipe(handleServiceOrphanType(context, enumType)); + } + // orphan unions + for (const unionType of namespace.unions.values()) { + diagnostics.pipe(handleServiceOrphanType(context, unionType)); + } + namespaces.push(...namespace.namespaces.values()); } } // update access diff --git a/packages/typespec-client-generator-core/test/decorators/usage.test.ts b/packages/typespec-client-generator-core/test/decorators/usage.test.ts index 745af36c0a..8ad5fec468 100644 --- a/packages/typespec-client-generator-core/test/decorators/usage.test.ts +++ b/packages/typespec-client-generator-core/test/decorators/usage.test.ts @@ -435,4 +435,28 @@ describe("typespec-client-generator-core: @usage", () => { code: "@azure-tools/typespec-client-generator-core/conflict-usage-override", }); }); + + it("orphan model in group", async () => { + await runner.compileWithBuiltInService( + ` + @access(Access.public) + @usage(Usage.output) + namespace Models { + model Model1 { + ref: Model2; + } + + model Model2 { + name: string; + } + } + ` + ); + const models = runner.context.sdkPackage.models; + strictEqual(models.length, 2); + strictEqual(models[0].usage, UsageFlags.Output); + strictEqual(models[0].access, "public"); + strictEqual(models[1].usage, UsageFlags.Output); + strictEqual(models[1].access, "public"); + }); });