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] add @isApiVersion decorator #1978

Open
wants to merge 14 commits into
base: release/december-2024
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
add basic tests
iscai-msft committed Dec 11, 2024
commit 30f4f3dad4c89e93dfdc188133d191ead745472e
Original file line number Diff line number Diff line change
@@ -552,7 +552,7 @@ export type ClientNamespaceDecorator = (
export type IsApiVersionDecorator = (
context: DecoratorContext,
target: ModelProperty,
value: boolean,
value?: boolean,
scope?: string,
) => void;

Original file line number Diff line number Diff line change
@@ -530,4 +530,4 @@ extern dec clientNamespace(
* ): void;
* ```
*/
extern dec isApiVersion(target: ModelProperty, value: valueof boolean, scope?: valueof string);
extern dec isApiVersion(target: ModelProperty, value?: valueof boolean, scope?: valueof string);
4 changes: 2 additions & 2 deletions packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1032,10 +1032,10 @@ const isApiVersionKey = createStateSymbol("isApiVersion");
export const isApiVersionDecorator: IsApiVersionDecorator = (
context: DecoratorContext,
param: ModelProperty,
value: boolean,
value?: boolean,
scope?: LanguageScopes,
) => {
setScopedDecoratorData(context, isApiVersionDecorator, isApiVersionKey, param, value, scope);
setScopedDecoratorData(context, isApiVersionDecorator, isApiVersionKey, param, value ?? true, scope);
};

export function getIsApiVersionDecorator(
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { createSdkTestRunner, SdkTestRunner } from "../test-host.js";

describe("@isApiVersion", () => {
let runner: SdkTestRunner;

beforeEach(async () => {
runner = await createSdkTestRunner({ emitterName: "@azure-tools/typespec-python" });
});

describe("@isApiVersion", () => {
it("override parameter to be api version", async () => {
await runner.compile(`
@service({})
namespace MyService;
op get(
@isApiVersion
@header("x-ms-version")
version: string
): string;
`);
const sdkPackage = runner.context.sdkPackage;
const method = sdkPackage.clients[0].methods[0];
strictEqual(method.kind, "basic");
const apiVersionParam = method.parameters.find((x) => x.name === "version");
tadelesh marked this conversation as resolved.
Show resolved Hide resolved
ok(apiVersionParam);
strictEqual(apiVersionParam.isApiVersionParam, true);
});
it("override parameter to not be api version", async () => {
await runner.compile(`
@service({})
namespace MyService;
op get(
@isApiVersion(false)
@query "api-version": string
): string;
`);
const sdkPackage = runner.context.sdkPackage;
const method = sdkPackage.clients[0].methods[0];
strictEqual(method.kind, "basic");
const apiVersionParam = method.parameters.find((x) => x.name === "api-version");
ok(apiVersionParam);
strictEqual(apiVersionParam.isApiVersionParam, false);
});
});
});