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] enhance versioning and add tests #713

Merged
merged 3 commits into from
Apr 22, 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/versioning_test-2024-3-22-18-14-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"
---

enhance versioning and add tests
29 changes: 16 additions & 13 deletions packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ export function getClient(
type: Namespace | Interface
): SdkClient | undefined {
if (hasExplicitClientOrOperationGroup(context)) {
return getScopedDecoratorData(context, clientKey, type);
let client = getScopedDecoratorData(context, clientKey, type);
if (client && (client.type as Type).kind === "Intrinsic") client = undefined;
return client;
}

// if there is no explicit client or operation group,
Expand Down Expand Up @@ -259,6 +261,15 @@ function serviceVersioningProjection(context: TCGCContext, client: SdkClient) {
client.service = projectedService;
}

function getClientsWithVersioning(context: TCGCContext, clients: SdkClient[]): SdkClient[] {
if (context.apiVersion !== "all") {
clients.map((client) => serviceVersioningProjection(context, client));
// filter all the clients not existed in the current version
return clients.filter((client) => (client.type as Type).kind !== "Intrinsic");
}
return clients;
}

/**
* List all the clients.
*
Expand All @@ -270,13 +281,8 @@ export function listClients(context: TCGCContext): SdkClient[] {

const explicitClients = [...listScopedDecoratorData(context, clientKey)];
if (explicitClients.length > 0) {
if (context.apiVersion !== "all") {
for (const client of explicitClients as SdkClient[]) {
serviceVersioningProjection(context, client);
}
}
context.__rawClients = explicitClients;
return explicitClients;
context.__rawClients = getClientsWithVersioning(context, explicitClients);
return context.__rawClients;
}

// if there is no explicit client, we will treat namespaces with service decorator as clients
Expand All @@ -302,11 +308,8 @@ export function listClients(context: TCGCContext): SdkClient[] {
} as SdkClient;
});

if (context.apiVersion !== "all") {
clients.map((client) => serviceVersioningProjection(context, client));
}
context.__rawClients = clients;
return clients;
context.__rawClients = getClientsWithVersioning(context, clients);
return context.__rawClients;
}

const operationGroupKey = createStateSymbol("operationGroup");
Expand Down
211 changes: 211 additions & 0 deletions packages/typespec-client-generator-core/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3044,4 +3044,215 @@ describe("typespec-client-generator-core: decorators", () => {
);
});
});

describe("versioning impact for apis", () => {
it("multiple clients", async () => {
const tsp = `
@service({
title: "Contoso Widget Manager",
})
@versioned(Contoso.WidgetManager.Versions)
namespace Contoso.WidgetManager;

enum Versions {
v1,
v2,
v3,
}

@client({name: "AClient"})
@test
interface A {
@route("/aa")
op aa(): void;

@added(Versions.v2)
@removed(Versions.v3)
@route("/ab")
op ab(): void;
}

@client({name: "BClient"})
@added(Versions.v2)
@test
interface B {
@route("/ba")
op ba(): void;

@route("/bb")
op bb(): void;
}
`;

let runnerWithVersion = await createSdkTestRunner({
"api-version": "v1",
emitterName: "@azure-tools/typespec-python",
});

let { A, B } = (await runnerWithVersion.compile(tsp)) as { A: Interface; B: Interface };
ok(getClient(runnerWithVersion.context, A));
strictEqual(getClient(runnerWithVersion.context, B), undefined);

let clients = listClients(runnerWithVersion.context);
strictEqual(clients.length, 1);
let aClient = clients.find((x) => x.name === "AClient");
ok(aClient);
let aOps = listOperationsInOperationGroup(runnerWithVersion.context, aClient);
strictEqual(aOps.length, 1);
let aa = aOps.find((x) => x.name === "aa");
ok(aa);

runnerWithVersion = await createSdkTestRunner({
"api-version": "v2",
emitterName: "@azure-tools/typespec-python",
});

let result = (await runnerWithVersion.compile(tsp)) as { A: Interface; B: Interface };
A = result.A;
B = result.B;
ok(getClient(runnerWithVersion.context, A));
ok(getClient(runnerWithVersion.context, B));

clients = listClients(runnerWithVersion.context);
strictEqual(clients.length, 2);
aClient = clients.find((x) => x.name === "AClient");
ok(aClient);
aOps = listOperationsInOperationGroup(runnerWithVersion.context, aClient);
strictEqual(aOps.length, 2);
aa = aOps.find((x) => x.name === "aa");
ok(aa);
const ab = aOps.find((x) => x.name === "ab");
ok(ab);
let bClient = clients.find((x) => x.name === "BClient");
ok(bClient);
let bOps = listOperationsInOperationGroup(runnerWithVersion.context, bClient);
strictEqual(bOps.length, 2);
let ba = bOps.find((x) => x.name === "ba");
ok(ba);
let bb = bOps.find((x) => x.name === "bb");
ok(bb);

runnerWithVersion = await createSdkTestRunner({
"api-version": "v3",
emitterName: "@azure-tools/typespec-python",
});

result = (await runnerWithVersion.compile(tsp)) as { A: Interface; B: Interface };
A = result.A;
B = result.B;
ok(getClient(runnerWithVersion.context, A));
ok(getClient(runnerWithVersion.context, B));

clients = listClients(runnerWithVersion.context);
strictEqual(clients.length, 2);
aClient = clients.find((x) => x.name === "AClient");
ok(aClient);
aOps = listOperationsInOperationGroup(runnerWithVersion.context, aClient);
strictEqual(aOps.length, 1);
aa = aOps.find((x) => x.name === "aa");
ok(aa);
bClient = clients.find((x) => x.name === "BClient");
ok(bClient);
bOps = listOperationsInOperationGroup(runnerWithVersion.context, bClient);
strictEqual(bOps.length, 2);
ba = bOps.find((x) => x.name === "ba");
ok(ba);
bb = bOps.find((x) => x.name === "bb");
ok(bb);
});

it("multiple operation groups", async () => {
const tsp = `
@service({
title: "Contoso Widget Manager",
})
@versioned(Contoso.WidgetManager.Versions)
namespace Contoso.WidgetManager;

enum Versions {
v1,
v2,
v3,
}

namespace A {
@route("/a")
op a(): void;
}

@added(Versions.v2)
@removed(Versions.v3)
interface B {
@route("/b")
op b(): void;
}
`;

let runnerWithVersion = await createSdkTestRunner({
"api-version": "v1",
emitterName: "@azure-tools/typespec-python",
});

await runnerWithVersion.compile(tsp);

let clients = listClients(runnerWithVersion.context);
strictEqual(clients.length, 1);
let client = clients.find((x) => x.name === "WidgetManagerClient");
ok(client);
let ops = listOperationGroups(runnerWithVersion.context, client);
strictEqual(ops.length, 1);
let aOp = ops.find((x) => x.type.name === "A");
ok(aOp);
let aOps = listOperationsInOperationGroup(runnerWithVersion.context, aOp);
strictEqual(aOps.length, 1);
let a = aOps.find((x) => x.name === "a");
ok(a);

runnerWithVersion = await createSdkTestRunner({
"api-version": "v2",
emitterName: "@azure-tools/typespec-python",
});

await runnerWithVersion.compile(tsp);

clients = listClients(runnerWithVersion.context);
strictEqual(clients.length, 1);
client = clients.find((x) => x.name === "WidgetManagerClient");
ok(client);
ops = listOperationGroups(runnerWithVersion.context, client);
strictEqual(ops.length, 2);
aOp = ops.find((x) => x.type.name === "A");
ok(aOp);
aOps = listOperationsInOperationGroup(runnerWithVersion.context, aOp);
strictEqual(aOps.length, 1);
a = aOps.find((x) => x.name === "a");
ok(a);
const bOp = ops.find((x) => x.type.name === "B");
ok(bOp);
const bOps = listOperationsInOperationGroup(runnerWithVersion.context, bOp);
strictEqual(bOps.length, 1);
const b = bOps.find((x) => x.name === "b");
ok(b);

runnerWithVersion = await createSdkTestRunner({
"api-version": "v3",
emitterName: "@azure-tools/typespec-python",
});

await runnerWithVersion.compile(tsp);

clients = listClients(runnerWithVersion.context);
strictEqual(clients.length, 1);
client = clients.find((x) => x.name === "WidgetManagerClient");
ok(client);
ops = listOperationGroups(runnerWithVersion.context, client);
strictEqual(ops.length, 1);
aOp = ops.find((x) => x.type.name === "A");
ok(aOp);
aOps = listOperationsInOperationGroup(runnerWithVersion.context, aOp);
strictEqual(aOps.length, 1);
a = aOps.find((x) => x.name === "a");
ok(a);
});
});
});
Loading