Skip to content

Commit

Permalink
Fix access for spread (#1186)
Browse files Browse the repository at this point in the history
Co-authored-by: Chenjie Shi <[email protected]>
  • Loading branch information
msyyc and tadelesh committed Jul 17, 2024
1 parent d635570 commit 868a47b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/fix-spread-access-2024-6-17-17-19-45.md
Original file line number Diff line number Diff line change
@@ -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
13 changes: 4 additions & 9 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 868a47b

Please sign in to comment.