Skip to content

Commit

Permalink
Add usage calculation for additional properties with model type (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh authored Feb 23, 2024
1 parent bb538c0 commit a61b351
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
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

0 comments on commit a61b351

Please sign in to comment.