Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 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,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,
Expand Down
44 changes: 43 additions & 1 deletion packages/http-client-python/emitter/test/emitter.test.ts
Original file line number Diff line number Diff line change
@@ -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(`
Expand Down Expand Up @@ -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<string, Record<string, unknown>> {
const result: Record<string, Record<string, unknown>> = {};
const visit = (node: unknown): void => {
if (Array.isArray(node)) {
node.forEach(visit);
} else if (node && typeof node === "object") {
const record = node as Record<string, unknown>;
if (typeof record.name === "string" && record.bodyParameter) {
result[record.name] = record.bodyParameter as Record<string, unknown>;
}
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" });
});