diff --git a/.chronus/changes/fix_discriminator_name-2024-3-2-17-50-27.md b/.chronus/changes/fix_discriminator_name-2024-3-2-17-50-27.md new file mode 100644 index 0000000000..5652fa67f7 --- /dev/null +++ b/.chronus/changes/fix_discriminator_name-2024-3-2-17-50-27.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +fix discriminator propert renaming issue \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/types.ts b/packages/typespec-client-generator-core/src/types.ts index 397190c9c6..2c3645cdd3 100644 --- a/packages/typespec-client-generator-core/src/types.ts +++ b/packages/typespec-client-generator-core/src/types.ts @@ -416,7 +416,7 @@ function addDiscriminatorToModelType( updateModelsMap(context, childModel, childModelSdkType, operation); for (const property of childModelSdkType.properties) { if (property.kind === "property") { - if (property.serializedName === discriminator?.propertyName) { + if (property.__raw?.name === discriminator?.propertyName) { if (property.type.kind !== "constant" && property.type.kind !== "enumvalue") { diagnostics.add( createDiagnostic({ @@ -451,7 +451,7 @@ function addDiscriminatorToModelType( } for (let i = 0; i < model.properties.length; i++) { const property = model.properties[i]; - if (property.kind === "property" && property.serializedName === discriminator.propertyName) { + if (property.kind === "property" && property.__raw?.name === discriminator.propertyName) { property.discriminator = true; model.discriminatorProperty = property; return diagnostics.wrap(undefined); @@ -471,18 +471,22 @@ function addDiscriminatorToModelType( encode: "string", }; } - const name = discriminator.propertyName; + const name = discriminatorProperty ? discriminatorProperty.name : discriminator.propertyName; model.properties.splice(0, 0, { kind: "property", optional: false, discriminator: true, - serializedName: discriminator.propertyName, + serializedName: discriminatorProperty + ? discriminatorProperty.serializedName + : discriminator.propertyName, type: discriminatorType!, nameInClient: name, name, isGeneratedName: false, onClient: false, - apiVersions: getAvailableApiVersions(context, type), + apiVersions: discriminatorProperty + ? getAvailableApiVersions(context, discriminatorProperty.__raw!) + : getAvailableApiVersions(context, type), isApiVersionParam: false, isMultipartFileInput: false, // discriminator property cannot be a file flatten: false, // discriminator properties can not be flattened diff --git a/packages/typespec-client-generator-core/test/types.test.ts b/packages/typespec-client-generator-core/test/types.test.ts index 6977f894b9..a935deab2e 100644 --- a/packages/typespec-client-generator-core/test/types.test.ts +++ b/packages/typespec-client-generator-core/test/types.test.ts @@ -2200,6 +2200,39 @@ describe("typespec-client-generator-core: types", () => { strictEqual(kindType.isFixed, false); }); + it("discriminator rename", async () => { + await runner.compileWithBuiltInService(` + @discriminator("kind") + model Fish { + @clientName("type") + @encodedName("application/json", "@data.kind") + kind: string; + age: int32; + } + + model Salmon extends Fish { + kind: "salmon"; + friends?: Fish[]; + hate?: Record; + partner?: Fish; + } + + @get + op getModel(): Fish; + `); + const models = runner.context.experimental_sdkPackage.models; + strictEqual(models.length, 2); + const fish = models.find((x) => x.name === "Fish"); + ok(fish); + strictEqual(fish.properties.length, 2); + const discriminatorProperty = fish.properties.find((x) => x.name === "type"); + ok(discriminatorProperty); + strictEqual(discriminatorProperty.kind, "property"); + strictEqual(discriminatorProperty.discriminator, true); + strictEqual(discriminatorProperty.type.kind, "string"); + strictEqual(discriminatorProperty.serializedName, "@data.kind"); + }); + it("filterOutCoreModels true", async () => { const runnerWithCore = await createSdkTestRunner({ librariesToAdd: [AzureCoreTestLibrary],