Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tcgc] change example mapping logic to allow operation id with/without renaming #1592

Merged
merged 2 commits into from
Sep 24, 2024
Merged
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/fix_example_mapping-2024-8-24-14-45-21.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

change example mapping logic to allow operation id with/without renaming
17 changes: 12 additions & 5 deletions packages/typespec-client-generator-core/src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ 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);
if (explicitOperationId) {
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 (
Expand All @@ -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(
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SdkHttpOperation>)
.response.methods[0] as SdkServiceMethod<SdkHttpOperation>
).operation;
ok(operation);
strictEqual(operation.examples?.length, 1);
operation = (
(runner.context.sdkPackage.clients[0].methods[1] as SdkClientAccessor<SdkHttpOperation>)
.response.methods[0] as SdkServiceMethod<SdkHttpOperation>
).operation;
ok(operation);
strictEqual(operation.examples?.length, 1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"operationId": "IF_get",
"title": "renamedIF_renamedOP",
"parameters": {},
"responses": {
"200": {
"description": "ARM operation completed successfully.",
"body": "test"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"operationId": "NS_get",
"title": "renamedNS_renamedOP",
"parameters": {},
"responses": {
"200": {
"description": "ARM operation completed successfully.",
"body": "test"
}
}
}
Loading