Skip to content

Commit

Permalink
[tcgc] ensure operation examples to be ordered (#2000)
Browse files Browse the repository at this point in the history
fix: #1986
  • Loading branch information
tadelesh authored Dec 19, 2024
1 parent 1c64037 commit 5ea9abb
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/fix_example_order-2024-11-18-20-58-12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

ensure operation examples to be ordered
3 changes: 3 additions & 0 deletions packages/typespec-client-generator-core/src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ function handleHttpOperationExamples(
operation.examples.push(operationExample);
}

// sort examples by file path
operation.examples.sort((a, b) => (a.filePath > b.filePath ? 1 : -1));

return diagnostics.wrap(undefined);
}

Expand Down
21 changes: 21 additions & 0 deletions packages/typespec-client-generator-core/test/examples/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,25 @@ describe("typespec-client-generator-core: load examples", () => {
ok(operation);
strictEqual(operation.examples?.length, 1);
});

it("ensure ordering for multiple examples", async () => {
await runner.host.addRealTypeSpecFile("./examples/a_b_c.json", `${__dirname}/load/a_b_c.json`);
await runner.host.addRealTypeSpecFile("./examples/a_b.json", `${__dirname}/load/a_b.json`);
await runner.host.addRealTypeSpecFile("./examples/a.json", `${__dirname}/load/a.json`);
await runner.compile(`
@service({})
namespace TestClient {
op get(): string;
}
`);

const operation = (
runner.context.sdkPackage.clients[0].methods[0] as SdkServiceMethod<SdkHttpOperation>
).operation;
ok(operation);
strictEqual(operation.examples?.length, 3);
strictEqual(operation.examples![0].filePath, "a.json");
strictEqual(operation.examples![1].filePath, "a_b.json");
strictEqual(operation.examples![2].filePath, "a_b_c.json");
});
});
11 changes: 11 additions & 0 deletions packages/typespec-client-generator-core/test/examples/load/a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"operationId": "get",
"title": "a",
"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": "get",
"title": "ab",
"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": "get",
"title": "abc",
"parameters": {},
"responses": {
"200": {
"description": "ARM operation completed successfully.",
"body": "test"
}
}
}

0 comments on commit 5ea9abb

Please sign in to comment.