diff --git a/.chronus/changes/python-renamed-body-sample-values.md b/.chronus/changes/python-renamed-body-sample-values.md new file mode 100644 index 00000000000..f720b9ed194 --- /dev/null +++ b/.chronus/changes/python-renamed-body-sample-values.md @@ -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`. diff --git a/packages/http-client-python/emitter/src/http.ts b/packages/http-client-python/emitter/src/http.ts index e1fe7efb351..5450aa9d57d 100644 --- a/packages/http-client-python/emitter/src/http.ts +++ b/packages/http-client-python/emitter/src/http.ts @@ -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, 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..f3f4d152806 100644 --- a/packages/http-client-python/emitter/test/emitter.test.ts +++ b/packages/http-client-python/emitter/test/emitter.test.ts @@ -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"; diff --git a/packages/http-client-python/emitter/test/samples.test.ts b/packages/http-client-python/emitter/test/samples.test.ts new file mode 100644 index 00000000000..8e15a662c16 --- /dev/null +++ b/packages/http-client-python/emitter/test/samples.test.ts @@ -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, +);