Skip to content

Commit

Permalink
[tcgc] Adjust sequence of properties calculation and discriminator ca…
Browse files Browse the repository at this point in the history
…lculation (#680)

fix: #676

cc: @archerzz
  • Loading branch information
tadelesh committed Apr 17, 2024
1 parent 610d9d8 commit 4e522ae
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .chronus/changes/fix_discriminator-2024-3-17-11-14-15.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

adjust sequence of properties calculation and discriminator calculation
3 changes: 2 additions & 1 deletion packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ export function getSdkModelWithDiagnostics(
);
sdkType.additionalPropertiesNullable = isNullable(type.sourceModel!.indexer!.value!);
}
// propreties should be generated first since base model'sdiscriminator handling is depend on derived model's properties
diagnostics.pipe(addPropertiesToModelType(context, type, sdkType, operation));
if (type.baseModel) {
sdkType.baseModel = context.modelsMap?.get(type.baseModel) as SdkModelType | undefined;
if (sdkType.baseModel === undefined) {
Expand All @@ -562,7 +564,6 @@ export function getSdkModelWithDiagnostics(
}
}
}
diagnostics.pipe(addPropertiesToModelType(context, type, sdkType, operation));
diagnostics.pipe(addDiscriminatorToModelType(context, type, sdkType));

updateModelsMap(context, type, sdkType, operation);
Expand Down
38 changes: 38 additions & 0 deletions packages/typespec-client-generator-core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,44 @@ describe("typespec-client-generator-core: types", () => {
strictEqual(shark.discriminatorProperty, sharktypeProperty);
});

it("handle derived model with discriminator first", async () => {
await runner.compileWithBuiltInService(`
model Salmon extends Fish {
kind: "salmon";
friends?: Fish[];
hate?: Record<Fish>;
partner?: Fish;
}
@discriminator("kind")
model Fish {
age: int32;
}
@get
op getSalmon(): Salmon;
`);
const models = runner.context.experimental_sdkPackage.models;
strictEqual(models.length, 2);
const fish = models.find((x) => x.name === "Fish");
ok(fish);
const kindProperty = fish.properties[0];
ok(kindProperty);
strictEqual(kindProperty.name, "kind");
strictEqual(kindProperty.kind, "property");
strictEqual(kindProperty.discriminator, true);
strictEqual(kindProperty.type.kind, "string");
strictEqual(kindProperty.__raw, undefined);
strictEqual(fish.discriminatorProperty, kindProperty);

const salmon = models.find((x) => x.name === "Salmon");
ok(salmon);
strictEqual(salmon.properties.length, 4);
strictEqual(salmon.properties[0].name, "kind");
strictEqual((salmon.properties[0] as SdkBodyModelPropertyType).discriminator, true);
strictEqual(salmon.discriminatorValue, "salmon");
});

it("single discriminated model", async () => {
await runner.compileWithBuiltInService(`
@discriminator("kind")
Expand Down

0 comments on commit 4e522ae

Please sign in to comment.