diff --git a/.chronus/changes/fix_example_mapping-2024-8-24-14-45-21.md b/.chronus/changes/fix_example_mapping-2024-8-24-14-45-21.md new file mode 100644 index 0000000000..881a34e7bb --- /dev/null +++ b/.chronus/changes/fix_example_mapping-2024-8-24-14-45-21.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +change example mapping logic to allow operation id with/without renaming \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/example.ts b/packages/typespec-client-generator-core/src/example.ts index 71f01166b4..b63f952d80 100644 --- a/packages/typespec-client-generator-core/src/example.ts +++ b/packages/typespec-client-generator-core/src/example.ts @@ -139,7 +139,7 @@ async function loadExamples( return diagnostics.wrap(map); } -function resolveOperationId(context: TCGCContext, operation: Operation) { +function resolveOperationId(context: TCGCContext, operation: Operation, honorRenaming: boolean) { const { program } = context; // if @operationId was specified use that value const explicitOperationId = getOperationId(program, operation); @@ -147,9 +147,9 @@ function resolveOperationId(context: TCGCContext, operation: Operation) { return explicitOperationId; } - const operationName = getLibraryName(context, operation); + const operationName = honorRenaming ? getLibraryName(context, operation) : operation.name; if (operation.interface) { - return `${getLibraryName(context, operation.interface)}_${operationName}`; + return `${honorRenaming ? getLibraryName(context, operation.interface) : operation.interface.name}_${operationName}`; } const namespace = operation.namespace; if ( @@ -160,7 +160,7 @@ function resolveOperationId(context: TCGCContext, operation: Operation) { return operationName; } - return `${getLibraryName(context, namespace)}_${operationName}`; + return `${honorRenaming ? getLibraryName(context, namespace) : namespace.name}_${operationName}`; } export async function handleClientExamples( @@ -181,7 +181,14 @@ export async function handleClientExamples( // since operation could have customization in client.tsp, we need to handle all the original operation (exclude the templated operation) let operation = method.__raw; while (operation && operation.templateMapper === undefined) { - const operationId = resolveOperationId(context, operation).toLowerCase(); + // try operation id with renaming + let operationId = resolveOperationId(context, operation, true).toLowerCase(); + if (examples.has(operationId)) { + diagnostics.pipe(handleMethodExamples(context, method, examples.get(operationId)!)); + break; + } + // try operation id without renaming + operationId = resolveOperationId(context, operation, false).toLowerCase(); if (examples.has(operationId)) { diagnostics.pipe(handleMethodExamples(context, method, examples.get(operationId)!)); break; diff --git a/packages/typespec-client-generator-core/test/examples/load.test.ts b/packages/typespec-client-generator-core/test/examples/load.test.ts index 7b914f8593..b7392ea65e 100644 --- a/packages/typespec-client-generator-core/test/examples/load.test.ts +++ b/packages/typespec-client-generator-core/test/examples/load.test.ts @@ -211,4 +211,46 @@ describe("typespec-client-generator-core: load examples", () => { ok(operation); strictEqual(operation.examples?.length, 1); }); + + it("load multiple example of original operation id with @clientName", async () => { + await runner.host.addRealTypeSpecFile( + "./examples/clientNameOriginal.json", + `${__dirname}/load/clientNameOriginal.json` + ); + await runner.host.addRealTypeSpecFile( + "./examples/clientNameAnotherOriginal.json", + `${__dirname}/load/clientNameAnotherOriginal.json` + ); + await runner.compile(` + @service({}) + namespace TestClient { + @clientName("renamedNS") + namespace NS { + @route("/ns") + @clientName("renamedOP") + op get(): string; + } + + @clientName("renamedIF") + namespace IF { + @route("/if") + @clientName("renamedOP") + op get(): string; + } + } + `); + + let operation = ( + (runner.context.sdkPackage.clients[0].methods[0] as SdkClientAccessor) + .response.methods[0] as SdkServiceMethod + ).operation; + ok(operation); + strictEqual(operation.examples?.length, 1); + operation = ( + (runner.context.sdkPackage.clients[0].methods[1] as SdkClientAccessor) + .response.methods[0] as SdkServiceMethod + ).operation; + ok(operation); + strictEqual(operation.examples?.length, 1); + }); }); diff --git a/packages/typespec-client-generator-core/test/examples/load/clientNameAnotherOriginal.json b/packages/typespec-client-generator-core/test/examples/load/clientNameAnotherOriginal.json new file mode 100644 index 0000000000..2e9664d88c --- /dev/null +++ b/packages/typespec-client-generator-core/test/examples/load/clientNameAnotherOriginal.json @@ -0,0 +1,11 @@ +{ + "operationId": "IF_get", + "title": "renamedIF_renamedOP", + "parameters": {}, + "responses": { + "200": { + "description": "ARM operation completed successfully.", + "body": "test" + } + } +} diff --git a/packages/typespec-client-generator-core/test/examples/load/clientNameOriginal.json b/packages/typespec-client-generator-core/test/examples/load/clientNameOriginal.json new file mode 100644 index 0000000000..f82a37b922 --- /dev/null +++ b/packages/typespec-client-generator-core/test/examples/load/clientNameOriginal.json @@ -0,0 +1,11 @@ +{ + "operationId": "NS_get", + "title": "renamedNS_renamedOP", + "parameters": {}, + "responses": { + "200": { + "description": "ARM operation completed successfully.", + "body": "test" + } + } +}