From 9cc84d71a6a8b12975f9a25c20110f636612f4ce Mon Sep 17 00:00:00 2001 From: Minh Triet Nguyen <751920nguyenminhtriet@gmail.com> Date: Mon, 30 Nov 2026 17:05:56 -0500 Subject: [PATCH] [http-client-python] Keep REST example values for client-renamed body parameters An explicitly named request body used TCGC's `name` as its wire name, which already includes client-only renames such as `@clientName`. Sample generation looks up REST example values by wire name, so a renamed body missed its example entry and fell back to placeholder values. Use `serializedName` instead; the SDK-facing client name is unchanged. Fixes #12433 --- ...-rename-sample-values-2026-10-30-17-0-0.md | 7 +++ .../http-client-python/emitter/src/http.ts | 3 +- .../emitter/test/emitter.test.ts | 44 ++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 .chronus/changes/python-body-rename-sample-values-2026-10-30-17-0-0.md diff --git a/.chronus/changes/python-body-rename-sample-values-2026-10-30-17-0-0.md b/.chronus/changes/python-body-rename-sample-values-2026-10-30-17-0-0.md new file mode 100644 index 00000000000..7ade7dde5bd --- /dev/null +++ b/.chronus/changes/python-body-rename-sample-values-2026-10-30-17-0-0.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Keep REST example values in generated samples when a request body parameter is renamed only for the client (for example with `@clientName`). The body's wire name now stays the REST name, so sample generation finds the example values instead of falling back to placeholders. diff --git a/packages/http-client-python/emitter/src/http.ts b/packages/http-client-python/emitter/src/http.ts index e1fe7efb351..c9e81144705 100644 --- a/packages/http-client-python/emitter/src/http.ts +++ b/packages/http-client-python/emitter/src/http.ts @@ -778,7 +778,8 @@ function emitHttpBodyParameter( contentTypes: bodyParam.contentTypes, location: bodyParam.kind, clientName: bodyParam.isGeneratedName ? "body" : getClientName(bodyParam), - wireName: bodyParam.isGeneratedName ? "body" : bodyParam.name, + // `name` carries any client-only rename; REST examples are keyed by the serialized name. + wireName: bodyParam.isGeneratedName ? "body" : bodyParam.serializedName, implementation: getImplementation(context, bodyParam), clientDefaultValue: bodyParam.clientDefaultValue, defaultContentType: bodyParam.defaultContentType, diff --git a/packages/http-client-python/emitter/test/emitter.test.ts b/packages/http-client-python/emitter/test/emitter.test.ts index 772ed45f2a7..c0046f6028d 100644 --- a/packages/http-client-python/emitter/test/emitter.test.ts +++ b/packages/http-client-python/emitter/test/emitter.test.ts @@ -1,6 +1,6 @@ import { expectDiagnostics, t } from "@typespec/compiler/testing"; import { expect, it } from "vitest"; -import { EmitterTester, emitCodeModel } from "./test-host.js"; +import { EmitterTester, emitCodeModel, type CodeModel } from "./test-host.js"; it("preserves numeric property names while fixing numeric enum members", async () => { const { codeModel, diagnostics } = await emitCodeModel(` @@ -131,3 +131,45 @@ it("generates models when no service exists", async () => { true, ); }); + +/** Collect every operation's `bodyParameter` in the emitted code model, keyed by operation name. */ +function bodyParametersByOperation(codeModel: CodeModel): Record> { + const result: Record> = {}; + const visit = (node: unknown): void => { + if (Array.isArray(node)) { + node.forEach(visit); + } else if (node && typeof node === "object") { + const record = node as Record; + if (typeof record.name === "string" && record.bodyParameter) { + result[record.name] = record.bodyParameter as Record; + } + Object.values(record).forEach(visit); + } + }; + visit(codeModel.clients); + return result; +} + +it("keeps the REST name as the body wire name when the body is renamed only for the client", async () => { + const { codeModel, diagnostics } = await emitCodeModel(` + import "@typespec/http"; + import "@azure-tools/typespec-client-generator-core"; + using TypeSpec.Http; + using Azure.ClientGenerator.Core; + + @service namespace Test; + model Topic { + title: string; + } + @route("/renamed") @put op renamed(@body @clientName("topic") resource: Topic): void; + @route("/explicit") @put op explicit(@body resource: Topic): void; + @route("/spread") @put op spread(...Topic): void; + `); + + expectDiagnostics(diagnostics, []); + const bodies = bodyParametersByOperation(codeModel); + // Samples look up REST example values by wire name, so it must stay the REST name. + expect(bodies["renamed"]).toMatchObject({ clientName: "topic", wireName: "resource" }); + expect(bodies["explicit"]).toMatchObject({ clientName: "resource", wireName: "resource" }); + expect(bodies["spread"]).toMatchObject({ clientName: "body", wireName: "body" }); +});