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..d742b7b2f9 --- /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 and usage calculation for nested model/enum in spread model \ 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 deeb7ee94c..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); - } - 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 67d73a446a..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 @@ -784,4 +784,31 @@ 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", + } + + 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); + }); });