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] make endpoint overridable #1155

Merged
merged 15 commits into from
Aug 6, 2024
7 changes: 7 additions & 0 deletions .chronus/changes/default_endpoint-2024-6-11-17-47-42.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

Make literal endpoints overridable
28 changes: 27 additions & 1 deletion packages/typespec-client-generator-core/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,33 @@ function getSdkEndpointParameter(
);
}
}
optional = Boolean(servers[0].url.length && templateArguments.every((param) => param.optional));
if (templateArguments.length === 0) {
templateArguments.push({
kind: "path",
name: "baseUrl",
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
isGeneratedName: true,
description: "Service host",
onClient: true,
urlEncode: false,
optional: false,
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
serializedName: "baseUrl",
correspondingMethodParams: [],
type: {
kind: "string",
encode: "string",
decorators: [],
},
clientDefaultValue: servers[0].url,
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
isApiVersionParam: false,
apiVersions: context.__tspTypeToApiVersions.get(client.type)!,
crossLanguageDefinitionId: `${getCrossLanguageDefinitionId(context, client.service)}.baseUrl`,
decorators: [],
});
}
optional = Boolean(
servers[0].url.length &&
templateArguments.every((param) => param.clientDefaultValue !== undefined)
);
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
}
return diagnostics.wrap({
kind: "endpoint",
Expand Down
25 changes: 22 additions & 3 deletions packages/typespec-client-generator-core/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,16 @@ describe("typespec-client-generator-core: package", () => {
strictEqual(endpointParam.type.kind, "endpoint");
strictEqual(endpointParam.type.serverUrl, "http://localhost:3000");
strictEqual(endpointParam.urlEncode, false);
strictEqual(endpointParam.type.templateArguments.length, 0);
strictEqual(endpointParam.type.templateArguments.length, 1);
const templateArg = endpointParam.type.templateArguments[0];
strictEqual(templateArg.kind, "path");
strictEqual(templateArg.name, "baseUrl");
strictEqual(templateArg.serializedName, "baseUrl");
strictEqual(templateArg.urlEncode, false);
strictEqual(templateArg.type.kind, "string");
strictEqual(templateArg.optional, false);
strictEqual(templateArg.onClient, true);
strictEqual(templateArg.clientDefaultValue, "http://localhost:3000");
});

it("initialization default endpoint with apikey auth", async () => {
Expand All @@ -156,7 +165,11 @@ describe("typespec-client-generator-core: package", () => {
)[0];
strictEqual(endpointParam.type.kind, "endpoint");
strictEqual(endpointParam.type.serverUrl, "http://localhost:3000");
strictEqual(endpointParam.type.templateArguments.length, 0);
strictEqual(endpointParam.type.templateArguments.length, 1);
const templateArg = endpointParam.type.templateArguments[0];
strictEqual(templateArg.kind, "path");
strictEqual(templateArg.type.kind, "string");
strictEqual(templateArg.clientDefaultValue, "http://localhost:3000");

const credentialParam = client.initialization.properties.filter(
(p): p is SdkCredentialParameter => p.kind === "credential"
Expand Down Expand Up @@ -195,7 +208,13 @@ describe("typespec-client-generator-core: package", () => {
)[0];
strictEqual(endpointParam.type.kind, "endpoint");
strictEqual(endpointParam.type.serverUrl, "http://localhost:3000");
strictEqual(endpointParam.type.templateArguments.length, 0);
strictEqual(endpointParam.type.templateArguments.length, 1);
const templateArg = endpointParam.type.templateArguments[0];
strictEqual(templateArg.kind, "path");
strictEqual(templateArg.type.kind, "string");
strictEqual(templateArg.optional, false);
strictEqual(templateArg.onClient, true);
strictEqual(templateArg.clientDefaultValue, "http://localhost:3000");

const credentialParam = client.initialization.properties.filter(
(p): p is SdkCredentialParameter => p.kind === "credential"
Expand Down
Loading