Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

Extend `isExactName` to additional SDK types whose names can be changed by `@clientName`: `SdkClientType`, `SdkServiceMethodBase` (and its derived method kinds), and `SdkEnumValueType`. Also fixed `SdkClientType.name` to strip the internal `exact()` marker.
10 changes: 4 additions & 6 deletions packages/typespec-client-generator-core/src/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from "./internal-utils.js";
import { createDiagnostic } from "./lib.js";
import { createSdkMethods, getSdkMethodParameter } from "./methods.js";
import { getCrossLanguageDefinitionId } from "./public-utils.js";
import { getCrossLanguageDefinitionId, getLibraryName, isExactClientName } from "./public-utils.js";
import { getSdkBuiltInType, getSdkCredentialParameter, getTypeSpecBuiltInType } from "./types.js";

function getEndpointTypeFromSingleServer<
Expand Down Expand Up @@ -190,17 +190,15 @@ export function createSdkClientType<TServiceOperation extends SdkServiceOperatio
): [SdkClientType<TServiceOperation>, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
let name = client.name;
if (client.type) {
const override = getClientNameOverride(context, client.type);
if (override) {
name = override;
}
if (client.type && getClientNameOverride(context, client.type)) {
name = getLibraryName(context, client.type);
}
const clientType = getActualClientType(client);
const sdkClientType: SdkClientType<TServiceOperation> = {
__raw: client,
kind: "client",
name,
isExactName: client.type ? isExactClientName(context, client.type) : false,
doc: client.type ? getClientDoc(context, client.type) : undefined,
summary: client.type ? getSummary(context.program, client.type) : undefined,
methods: [],
Expand Down
6 changes: 6 additions & 0 deletions packages/typespec-client-generator-core/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ export interface SdkClientType<
kind: "client";
/** Name of the client. */
name: string;
/** Whether name should be used exactly as-is, without casing transformations. */
isExactName: boolean;
/** Full qualified namespace. */
namespace: string;
/** Document for the type. */
Expand Down Expand Up @@ -480,6 +482,8 @@ export interface SdkEnumValueType<
> extends SdkTypeBase {
kind: "enumvalue";
name: string;
/** Whether name should be used exactly as-is, without casing transformations. */
isExactName: boolean;
value: string | number;
enumType: SdkEnumType;
valueType: TValueType;
Expand Down Expand Up @@ -1000,6 +1004,8 @@ interface SdkServiceMethodBase<
> extends DecoratedType {
__raw?: Operation;
name: string;
/** Whether name should be used exactly as-is, without casing transformations. */
isExactName: boolean;
/** Whether the type has public or private accessibility */
access: AccessFlags;
/** API versions supported for current type. */
Expand Down
2 changes: 2 additions & 0 deletions packages/typespec-client-generator-core/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
getCrossLanguageDefinitionId,
getHttpOperationWithCache,
getLibraryName,
isExactClientName,
} from "./public-utils.js";
import {
getClientTypeWithDiagnostics,
Expand Down Expand Up @@ -736,6 +737,7 @@ export function getSdkBasicServiceMethod<TServiceOperation extends SdkServiceOpe
__raw: operation,
kind: "basic",
name,
isExactName: isExactClientName(context, operation),
access: getAccess(context, operation) ?? "public",
parameters: methodParameters,
doc: getClientDoc(context, operation),
Expand Down
2 changes: 2 additions & 0 deletions packages/typespec-client-generator-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ function getSdkEnumValueWithDiagnostics(
return diagnostics.wrap({
...diagnostics.pipe(getSdkTypeBaseHelper(context, type, "enumvalue")),
name: getLibraryName(context, type),
isExactName: isExactClientName(context, type),
value: type.value ?? type.name,
enumType,
valueType: enumType.valueType,
Expand Down Expand Up @@ -1081,6 +1082,7 @@ function getSdkUnionEnumValues(
values.push({
...diagnostics.pipe(getSdkTypeBaseHelper(context, member.type, "enumvalue")),
name: name ? name : `${member.value}`,
isExactName: isExactClientName(context, member.type),
value: member.value,
valueType: enumType.valueType,
enumType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,65 @@ describe("exact", () => {
const client = sdkPackage.clients[0];
const method = client.methods[0];
strictEqual(method.name, "my_exact_op");
strictEqual(method.isExactName, true);
});
});

describe("exact naming on enum values", () => {
it("marks enum value name as exact via exact()", async () => {
const { program } = await SimpleBaseTester.compile(
createClientCustomizationInput(
`
@service
namespace MyService;

enum Status {
Active,
Inactive,
}

op get(@query status: Status): void;
`,
`
#suppress "experimental-feature" "testing exact"
@@clientName(MyService.Status.Active, exact("my_active_value"));
`,
),
);

const context = await createSdkContextForTester(program);
const sdkPackage = context.sdkPackage;
const enumType = sdkPackage.enums[0];
const activeValue = enumType.values.find((v) => v.name === "my_active_value");
strictEqual(activeValue?.isExactName, true);
const inactiveValue = enumType.values.find((v) => v.name === "Inactive");
strictEqual(inactiveValue?.isExactName, false);
});
});

describe("exact naming on clients", () => {
it("marks client name as exact via exact()", async () => {
const { program } = await SimpleBaseTester.compile(
createClientCustomizationInput(
`
@service
namespace MyService;

@route("/test")
op testOp(): void;
`,
`
#suppress "experimental-feature" "testing exact"
@@clientName(MyService, exact("my_exact_client"));
`,
),
);

const context = await createSdkContextForTester(program);
const sdkPackage = context.sdkPackage;
const client = sdkPackage.clients[0];
strictEqual(client.name, "my_exact_client");
strictEqual(client.isExactName, true);
});
});
});
Loading