Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .chronus/changes/python-renamed-body-sample-values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-client-python"
---

Preserve REST example body values in generated samples when a body parameter is renamed with `@clientName`.
2 changes: 1 addition & 1 deletion packages/http-client-python/emitter/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ function emitHttpBodyParameter(
contentTypes: bodyParam.contentTypes,
location: bodyParam.kind,
clientName: bodyParam.isGeneratedName ? "body" : getClientName(bodyParam),
wireName: bodyParam.isGeneratedName ? "body" : bodyParam.name,
wireName: bodyParam.isGeneratedName ? "body" : bodyParam.serializedName,

@l0lawrence Libba Lawrence (l0lawrence) Nov 3, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ body params clientNames were using bodyParam.name was the tcgc renamed value, not the name on the wire

implementation: getImplementation(context, bodyParam),
clientDefaultValue: bodyParam.clientDefaultValue,
defaultContentType: bodyParam.defaultContentType,
Expand Down
35 changes: 35 additions & 0 deletions packages/http-client-python/emitter/test/emitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,41 @@ import { expectDiagnostics, t } from "@typespec/compiler/testing";
import { expect, it } from "vitest";
import { EmitterTester, emitCodeModel } from "./test-host.js";

it.each([
["@body resource: Topic", "resource", "resource"],
['@body @clientName("topic", "python") resource: Topic', "topic", "resource"],
["...Topic", "body", "body"],
])("preserves body parameter names for %s", async (parameters, clientName, wireName) => {
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;
state: string;
}
@post op createTopic(${parameters}): void;
`);

expectDiagnostics(diagnostics, []);
expect(codeModel.clients).toContainEqual(
expect.objectContaining({
operationGroups: expect.arrayContaining([
expect.objectContaining({
operations: expect.arrayContaining([
expect.objectContaining({
bodyParameter: expect.objectContaining({ clientName, wireName }),
}),
]),
}),
]),
}),
);
});

it("preserves numeric property names while fixing numeric enum members", async () => {
const { codeModel, diagnostics } = await emitCodeModel(`
import "@typespec/http";
Expand Down
70 changes: 70 additions & 0 deletions packages/http-client-python/emitter/test/samples.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { expectDiagnostics } from "@typespec/compiler/testing";
import { mkdtemp, readFile, rm } from "fs/promises";
import { tmpdir } from "os";
import { join } from "path";
import { expect, it } from "vitest";
import { EmitterTester } from "./test-host.js";

it.each([
["", "resource"],
['@clientName("topic", "python")', "topic"],
])(
"preserves REST body example values with %s",
async (decorator, clientName) => {
const outputDir = await mkdtemp(join(tmpdir(), "typespec-python-sample-"));
try {
const [, diagnostics] = await EmitterTester.files({
"examples/create-topic.json": JSON.stringify({
operationId: "createTopic",
title: "Create a topic",
parameters: {
resource: { title: "Product updates", state: "Normal" },
},
responses: { "204": {} },
}),
}).compileAndDiagnose(
`
import "@typespec/http";
import "@azure-tools/typespec-client-generator-core";
using TypeSpec.Http;
using Azure.ClientGenerator.Core;

@service
@server("https://example.org", "Example endpoint")
namespace Test;
model Topic {
title: string;
state: string;
}
@route("/topics")
@post
op createTopic(@body ${decorator} resource: Topic): void;
`,
{
compilerOptions: {
options: {
"@typespec/http-client-python": {
"emitter-output-dir": outputDir,
"examples-dir": "./examples",
"generate-sample": true,
"generate-test": false,
flavor: "azure",
},
},
},
},
);

expectDiagnostics(diagnostics, []);
const sample = await readFile(
join(outputDir, "generated_samples", "createtopic.py"),
"utf-8",
);
expect(sample).toContain(`${clientName}={"state": "Normal", "title": "Product updates"}`);
expect(sample).toContain("# x-ms-original-file: create-topic.json");
} finally {
await rm(outputDir, { recursive: true, force: true });
}
},
30_000,
);
Loading