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 clientDefaultValue for endpoint template arguments #1266

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-client-generator-core"
---

expose default values for endpoint template arguments through `.clientDefaultValue`
7 changes: 6 additions & 1 deletion packages/typespec-client-generator-core/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,14 @@ function getSdkEndpointParameter(
if (sdkParam.kind === "path") {
templateArguments.push(sdkParam);
sdkParam.onClient = true;
if (param.defaultValue && "value" in param.defaultValue) {
sdkParam.clientDefaultValue = param.defaultValue.value;
}
const apiVersionInfo = updateWithApiVersionInformation(context, param, client.type);
sdkParam.clientDefaultValue = apiVersionInfo.clientDefaultValue;
sdkParam.isApiVersionParam = apiVersionInfo.isApiVersionParam;
if (sdkParam.isApiVersionParam) {
sdkParam.clientDefaultValue = apiVersionInfo.clientDefaultValue;
}
sdkParam.apiVersions = getAvailableApiVersions(context, param, client.type);
} else {
diagnostics.add(
Expand Down
32 changes: 32 additions & 0 deletions packages/typespec-client-generator-core/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,38 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(scheme.name, "x-ms-api-key");
});

it("endpoint with path param default value", async () => {
await runner.compile(`
@server(
"{endpoint}",
"Test server endpoint",
{
endpoint: string = "http://localhost:3000",
}
)
@service({})
namespace MyService;
`);
const sdkPackage = runner.context.sdkPackage;
strictEqual(sdkPackage.clients.length, 1);
const client = sdkPackage.clients[0];
strictEqual(client.initialization.properties.length, 1);

const endpointParam = client.initialization.properties.filter(
(p): p is SdkEndpointParameter => p.kind === "endpoint"
)[0];
strictEqual(endpointParam.type.kind, "endpoint");
strictEqual(endpointParam.type.serverUrl, "{endpoint}");

strictEqual(endpointParam.type.templateArguments.length, 1);
const endpointTemplateArg = endpointParam.type.templateArguments[0];
strictEqual(endpointTemplateArg.name, "endpoint");
strictEqual(endpointTemplateArg.onClient, true);
strictEqual(endpointTemplateArg.optional, false);
strictEqual(endpointTemplateArg.kind, "path");
strictEqual(endpointTemplateArg.clientDefaultValue, "http://localhost:3000");
});

it("single with core", async () => {
const runnerWithCore = await createSdkTestRunner({
librariesToAdd: [AzureCoreTestLibrary],
Expand Down
Loading