From f14e021bc1ea936b3d8b36fddc498603d18d6188 Mon Sep 17 00:00:00 2001 From: iscai-msft <43154838+iscai-msft@users.noreply.github.com> Date: Wed, 31 Jul 2024 22:36:02 -0400 Subject: [PATCH] [tcgc] add clientDefaultValue for endpoint template arguments (#1266) fixes #1030 --------- Co-authored-by: iscai-msft Co-authored-by: Chenjie Shi --- ...point_client_default-2024-6-30-14-27-25.md | 7 ++++ .../src/package.ts | 7 +++- .../test/package.test.ts | 32 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 .chronus/changes/endpoint_client_default-2024-6-30-14-27-25.md diff --git a/.chronus/changes/endpoint_client_default-2024-6-30-14-27-25.md b/.chronus/changes/endpoint_client_default-2024-6-30-14-27-25.md new file mode 100644 index 0000000000..6f1df13109 --- /dev/null +++ b/.chronus/changes/endpoint_client_default-2024-6-30-14-27-25.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-client-generator-core" +--- + +expose default values for endpoint template arguments through `.clientDefaultValue` \ No newline at end of file diff --git a/packages/typespec-client-generator-core/src/package.ts b/packages/typespec-client-generator-core/src/package.ts index c18f078fce..d8c284986b 100644 --- a/packages/typespec-client-generator-core/src/package.ts +++ b/packages/typespec-client-generator-core/src/package.ts @@ -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( diff --git a/packages/typespec-client-generator-core/test/package.test.ts b/packages/typespec-client-generator-core/test/package.test.ts index 2a0ecfe515..9987fee7a4 100644 --- a/packages/typespec-client-generator-core/test/package.test.ts +++ b/packages/typespec-client-generator-core/test/package.test.ts @@ -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],