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

Add usage calculation for additional properties with model type #301

Merged
merged 4 commits into from
Feb 23, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

Add usage calculation for additional properties with model type
12 changes: 12 additions & 0 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ function updateModelsMap(context: SdkContext, type: Type, sdkType: SdkType, oper
} else {
context.operationModelsMap.set(operation, new Map([[type, sdkType]]));
}
// TODO: it seems duplicate calculation, need to optimize later
if (sdkType.kind === "model") {
for (const prop of sdkType.properties) {
if (prop.type.kind === "model" || prop.type.kind === "enum") {
Expand All @@ -953,6 +954,14 @@ function updateModelsMap(context: SdkContext, type: Type, sdkType: SdkType, oper
if (sdkType.baseModel) {
updateModelsMap(context, sdkType.baseModel.__raw as any, sdkType.baseModel, operation);
}
if (sdkType.additionalProperties) {
updateModelsMap(
context,
sdkType.additionalProperties.__raw as any,
sdkType.additionalProperties,
operation
);
}
if (sdkType.discriminatedSubtypes) {
for (const subtype of Object.values(sdkType.discriminatedSubtypes)) {
updateModelsMap(context, subtype.__raw as any, subtype, operation);
Expand Down Expand Up @@ -1020,6 +1029,9 @@ function updateUsageOfModel(
updateUsageOfModel(context, discriminatedSubtype, usage, seenModelNames);
}
}
if (type.additionalProperties) {
updateUsageOfModel(context, type.additionalProperties, usage, seenModelNames);
}
for (const property of type.properties) {
updateUsageOfModel(context, property.type, usage, seenModelNames);
}
Expand Down
33 changes: 33 additions & 0 deletions packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,39 @@ describe("typespec-client-generator-core: types", () => {
strictEqual(AdditionalPropertiesModel2.baseModel, undefined);
strictEqual(NonAdditionalPropertiesModel.additionalProperties, undefined);
});
it("additionalProperties usage", async () => {
await runner.compileWithBuiltInService(`
@service({})
namespace MyService {
model AdditionalPropertiesModel extends Record<Test> {
}

model AdditionalPropertiesModel2 is Record<Test> {
}

model Test {
}

op test(@body input: AdditionalPropertiesModel): AdditionalPropertiesModel2;
}
`);
const models = getAllModelsAssertNoDiagnostics(runner.context);
strictEqual(models.length, 3);
const AdditionalPropertiesModel = models.find(
(x) => x.name === "AdditionalPropertiesModel"
)! as SdkModelType;
const AdditionalPropertiesModel2 = models.find(
(x) => x.name === "AdditionalPropertiesModel2"
)! as SdkModelType;
const Test = models.find((x) => x.name === "Test")! as SdkModelType;
strictEqual(AdditionalPropertiesModel.additionalProperties?.kind, "model");
strictEqual(AdditionalPropertiesModel.baseModel, undefined);
strictEqual(AdditionalPropertiesModel.usage, UsageFlags.Input);
strictEqual(AdditionalPropertiesModel2.additionalProperties?.kind, "model");
strictEqual(AdditionalPropertiesModel2.baseModel, undefined);
strictEqual(AdditionalPropertiesModel2.usage, UsageFlags.Output);
strictEqual(Test.usage, UsageFlags.Input | UsageFlags.Output);
});
it("crossLanguageDefinitionId", async () => {
await runner.compile(`
@service({})
Expand Down
Loading