Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix access for spread #1186

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading