From 7abb7bb72d2a86cc4a5e161b18f12ba01c18073e Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 13 Nov 2024 16:30:40 -0500 Subject: [PATCH 1/3] have wire name take into account xml name --- .../src/public-utils.ts | 10 +++++++++- .../test/types/general-decorators-list.test.ts | 17 +++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/typespec-client-generator-core/src/public-utils.ts b/packages/typespec-client-generator-core/src/public-utils.ts index c32fcfcbb5..6780fb621a 100644 --- a/packages/typespec-client-generator-core/src/public-utils.ts +++ b/packages/typespec-client-generator-core/src/public-utils.ts @@ -41,6 +41,7 @@ import { getClientNamespaceStringHelper, getHttpBodySpreadModel, getHttpOperationResponseHeaders, + getTypeDecorators, isAzureCoreTspModel, isHttpBodySpread, parseEmitterName, @@ -206,7 +207,14 @@ export function getLibraryName( * @returns */ export function getWireName(context: TCGCContext, type: Type & { name: string }) { - // 1. Check if there's an encoded name + const xmlNameDecorator = ignoreDiagnostics(getTypeDecorators(context, type)).filter( + (x) => x.name === "TypeSpec.Xml.@name", + ); + // 1. Check xml name encoding + if (xmlNameDecorator.length > 0) { + return xmlNameDecorator[0].arguments["name"]; + } + // 2. Check if there's an encoded name const encodedName = resolveEncodedName(context.program, type, "application/json"); if (encodedName !== type.name) return encodedName; // 2. Check if there's deprecated language projection diff --git a/packages/typespec-client-generator-core/test/types/general-decorators-list.test.ts b/packages/typespec-client-generator-core/test/types/general-decorators-list.test.ts index 40c467b450..98cd8df571 100644 --- a/packages/typespec-client-generator-core/test/types/general-decorators-list.test.ts +++ b/packages/typespec-client-generator-core/test/types/general-decorators-list.test.ts @@ -1,8 +1,8 @@ import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing"; import { expectDiagnostics } from "@typespec/compiler/testing"; import { XmlTestLibrary } from "@typespec/xml/testing"; -import { deepStrictEqual, ok, strictEqual } from "assert"; -import { afterEach, beforeEach, describe, it } from "vitest"; +import { deepStrictEqual, strictEqual } from "assert"; +import { beforeEach, describe, it } from "vitest"; import { SdkEnumValueType } from "../../src/interfaces.js"; import { SdkTestRunner, createSdkTestRunner } from "../test-host.js"; @@ -12,16 +12,6 @@ describe("typespec-client-generator-core: general decorators list", () => { beforeEach(async () => { runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-java" }); }); - afterEach(async () => { - for (const modelsOrEnums of [ - runner.context.sdkPackage.models, - runner.context.sdkPackage.enums, - ]) { - for (const item of modelsOrEnums) { - ok(item.name !== ""); - } - } - }); it("no arg", async function () { runner = await createSdkTestRunner({}, { additionalDecorators: ["TypeSpec\\.@error"] }); @@ -194,6 +184,9 @@ describe("typespec-client-generator-core: general decorators list", () => { arguments: { name: "XmlId" }, }, ]); + const idProp = models[0].properties[0]; + strictEqual(idProp.kind, "property"); + strictEqual(idProp.serializedName, "XmlId"); }); it("@ns", async function () { From c5b9c48af50254742cce4c706c5c2ce77e3729c3 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 13 Nov 2024 16:31:08 -0500 Subject: [PATCH 2/3] add changeset --- .chronus/changes/tcgc-xmlEncodedName-2024-10-13-16-31-1.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/tcgc-xmlEncodedName-2024-10-13-16-31-1.md diff --git a/.chronus/changes/tcgc-xmlEncodedName-2024-10-13-16-31-1.md b/.chronus/changes/tcgc-xmlEncodedName-2024-10-13-16-31-1.md new file mode 100644 index 0000000000..7969d67b33 --- /dev/null +++ b/.chronus/changes/tcgc-xmlEncodedName-2024-10-13-16-31-1.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +have `.serializedName` take into account `@Xml.name` decorators \ No newline at end of file From 3a6253fadcd24601e423177ddde2cd29874f56fc Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 13 Nov 2024 16:32:17 -0500 Subject: [PATCH 3/3] add back removed test part --- .../test/types/general-decorators-list.test.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/typespec-client-generator-core/test/types/general-decorators-list.test.ts b/packages/typespec-client-generator-core/test/types/general-decorators-list.test.ts index 98cd8df571..e9a1af6763 100644 --- a/packages/typespec-client-generator-core/test/types/general-decorators-list.test.ts +++ b/packages/typespec-client-generator-core/test/types/general-decorators-list.test.ts @@ -1,8 +1,8 @@ import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing"; import { expectDiagnostics } from "@typespec/compiler/testing"; import { XmlTestLibrary } from "@typespec/xml/testing"; -import { deepStrictEqual, strictEqual } from "assert"; -import { beforeEach, describe, it } from "vitest"; +import { deepStrictEqual, ok, strictEqual } from "assert"; +import { afterEach, beforeEach, describe, it } from "vitest"; import { SdkEnumValueType } from "../../src/interfaces.js"; import { SdkTestRunner, createSdkTestRunner } from "../test-host.js"; @@ -12,6 +12,16 @@ describe("typespec-client-generator-core: general decorators list", () => { beforeEach(async () => { runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-java" }); }); + afterEach(async () => { + for (const modelsOrEnums of [ + runner.context.sdkPackage.models, + runner.context.sdkPackage.enums, + ]) { + for (const item of modelsOrEnums) { + ok(item.name !== ""); + } + } + }); it("no arg", async function () { runner = await createSdkTestRunner({}, { additionalDecorators: ["TypeSpec\\.@error"] });