From 09efaf360ca6fc3266c5a256be5f026ddebe8aaa Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Wed, 17 Jul 2024 16:56:07 +0800 Subject: [PATCH 1/4] init --- .../src/types.ts | 2 +- .../test/types/enum-types.test.ts | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/typespec-client-generator-core/src/types.ts b/packages/typespec-client-generator-core/src/types.ts index c58f5b6037..63ab427dec 100644 --- a/packages/typespec-client-generator-core/src/types.ts +++ b/packages/typespec-client-generator-core/src/types.ts @@ -1434,7 +1434,7 @@ function updateAccessOfModel(context: TCGCContext): void { function updateSpreadModelUsageAndAccess(context: TCGCContext): void { for (const sdkType of context.spreadModels?.values() ?? []) { - updateUsageOfModel(context, UsageFlags.Spread, sdkType); + updateUsageOfModel(context, UsageFlags.Spread, sdkType, { propagation: false }); } for (const sdkType of context.modelsMap?.values() ?? []) { // if a type only has spread usage, then it could be internal diff --git a/packages/typespec-client-generator-core/test/types/enum-types.test.ts b/packages/typespec-client-generator-core/test/types/enum-types.test.ts index 67d73a446a..7afbb2df8b 100644 --- a/packages/typespec-client-generator-core/test/types/enum-types.test.ts +++ b/packages/typespec-client-generator-core/test/types/enum-types.test.ts @@ -784,4 +784,24 @@ describe("typespec-client-generator-core: enum types", () => { strictEqual(enums[1].crossLanguageDefinitionId, "N.UD"); strictEqual(enums[1].usage, UsageFlags.Input | UsageFlags.Json); }); + + it("spread and union as enum", async () => { + await runner.compile( + ` + @service({}) + namespace N { + union StringExtensibleNamedUnion { + string, + OptionB: "b", + "c", + } + + op read(prop: StringExtensibleNamedUnion): void; + } + ` + ); + const enums = runner.context.sdkPackage.enums; + strictEqual(enums.length, 1); + strictEqual(enums[0].access, "public"); + }); }); From e310d3bb3bd5d120ba87641d604542ec55130621 Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:18:31 +0800 Subject: [PATCH 2/4] fix spread and access --- .../typespec-client-generator-core/src/types.ts | 13 ++++--------- .../test/types/enum-types.test.ts | 11 +++++++++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/typespec-client-generator-core/src/types.ts b/packages/typespec-client-generator-core/src/types.ts index cfd3bfb768..c75abe4aed 100644 --- a/packages/typespec-client-generator-core/src/types.ts +++ b/packages/typespec-client-generator-core/src/types.ts @@ -1330,9 +1330,8 @@ function updateTypesFromOperation( if (!context.spreadModels?.has(httpBody.type)) { context.spreadModels?.set(httpBody.type as Model, sdkType as SdkModelType); } - } else { - updateUsageOfModel(context, UsageFlags.Input, sdkType); } + updateUsageOfModel(context, UsageFlags.Input, sdkType); if (httpBody.contentTypes.some((x) => isJsonContentType(x))) { updateUsageOfModel(context, UsageFlags.Json, sdkType); } @@ -1435,13 +1434,9 @@ function updateAccessOfModel(context: TCGCContext): void { function updateSpreadModelUsageAndAccess(context: TCGCContext): void { for (const sdkType of context.spreadModels?.values() ?? []) { - updateUsageOfModel(context, UsageFlags.Spread, sdkType, { propagation: false }); - } - for (const sdkType of context.modelsMap?.values() ?? []) { - // if a type only has spread usage, then it could be internal - if ((sdkType.usage & UsageFlags.Spread) > 0) { - sdkType.access = "internal"; - } + // if a type has spread usage, then it must be internal + sdkType.access = "internal"; + sdkType.usage = (sdkType.usage & ~UsageFlags.Input) | UsageFlags.Spread; } } diff --git a/packages/typespec-client-generator-core/test/types/enum-types.test.ts b/packages/typespec-client-generator-core/test/types/enum-types.test.ts index 7afbb2df8b..96c1c76c06 100644 --- a/packages/typespec-client-generator-core/test/types/enum-types.test.ts +++ b/packages/typespec-client-generator-core/test/types/enum-types.test.ts @@ -795,13 +795,20 @@ describe("typespec-client-generator-core: enum types", () => { OptionB: "b", "c", } - - op read(prop: StringExtensibleNamedUnion): void; + + model Test { name: string; } + op read(prop1: StringExtensibleNamedUnion; prop2: Test): void; } ` ); const enums = runner.context.sdkPackage.enums; strictEqual(enums.length, 1); strictEqual(enums[0].access, "public"); + strictEqual(enums[0].usage, UsageFlags.Input | UsageFlags.Json); + const models = runner.context.sdkPackage.models; + const testModel = models.find((x) => x.name === "Test"); + ok(testModel); + strictEqual(testModel.access, "public"); + strictEqual(testModel.usage, UsageFlags.Input | UsageFlags.Json); }); }); From dfe3f0707d5689869709b698aeaa012a7eacd38e Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:19:57 +0800 Subject: [PATCH 3/4] changelog --- .chronus/changes/fix-spread-access-2024-6-17-17-19-45.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/fix-spread-access-2024-6-17-17-19-45.md diff --git a/.chronus/changes/fix-spread-access-2024-6-17-17-19-45.md b/.chronus/changes/fix-spread-access-2024-6-17-17-19-45.md new file mode 100644 index 0000000000..8d9f5d96c7 --- /dev/null +++ b/.chronus/changes/fix-spread-access-2024-6-17-17-19-45.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +Fix access of properties in spreaded model \ No newline at end of file From 14f01d1b72dbe2dcc2f706beae6f614ed3e495fc Mon Sep 17 00:00:00 2001 From: Chenjie Shi Date: Wed, 17 Jul 2024 17:24:57 +0800 Subject: [PATCH 4/4] Update .chronus/changes/fix-spread-access-2024-6-17-17-19-45.md --- .chronus/changes/fix-spread-access-2024-6-17-17-19-45.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chronus/changes/fix-spread-access-2024-6-17-17-19-45.md b/.chronus/changes/fix-spread-access-2024-6-17-17-19-45.md index 8d9f5d96c7..d742b7b2f9 100644 --- a/.chronus/changes/fix-spread-access-2024-6-17-17-19-45.md +++ b/.chronus/changes/fix-spread-access-2024-6-17-17-19-45.md @@ -4,4 +4,4 @@ packages: - "@azure-tools/typespec-client-generator-core" --- -Fix access of properties in spreaded model \ No newline at end of file +fix access and usage calculation for nested model/enum in spread model \ No newline at end of file