Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@ import type {
SdkEnumType,
SdkHttpOperation,
} from "@azure-tools/typespec-client-generator-core";
import { UsageFlags } from "@azure-tools/typespec-client-generator-core";
import type { Diagnostic } from "@typespec/compiler";
import { createDiagnosticCollector, NoTarget } from "@typespec/compiler";
import type { CSharpEmitterContext } from "../sdk-context.js";
import type { CodeModel } from "../type/code-model.js";
import type {
InputClient,
InputEnumType,
InputLiteralType,
InputModelType,
InputType,
InputUnionType,
} from "../type/input-type.js";
import { fromSdkClients } from "./client-converter.js";
import { createDiagnostic } from "./lib.js";
import { updateModelConstructorRequirements } from "./model-constructor.js";
import { fromSdkNamespaces } from "./namespace-converter.js";
import { processServiceAuthentication } from "./service-authentication.js";
import { fromSdkType } from "./type-converter.js";
import { firstLetterToUpperCase, getClientNamespaceString } from "./utils.js";
import { fromSdkType, normalizeLiteralTypeForProperty } from "./type-converter.js";
import { getClientNamespaceString } from "./utils.js";

/**
* Creates the code model from the SDK context.
Expand Down Expand Up @@ -82,7 +85,9 @@ export function createModel(sdkContext: CSharpEmitterContext): [CodeModel, reado
// operation-parameter enums) where the namespace can be inconsistent across emission
// paths. Distinct named types that share a name across different namespaces still have
// distinct crossLanguageDefinitionIds and are preserved.
// Metadata selections retain distinct enum identities even if their anonymous names collide.
const existingEnumKeys = new Set(enums.map((e) => typeDedupeKey(e)));
const existingEnums = new Set(enums);
for (const type of sdkContext.__typeCache.types.values()) {
if (typesBeforeClients.has(type)) continue;
if (type.kind === "model") {
Expand All @@ -94,17 +99,36 @@ export function createModel(sdkContext: CSharpEmitterContext): [CodeModel, reado
} else if (type.kind === "enum") {
const enumType = type as InputEnumType;
const key = typeDedupeKey(enumType);
if (existingEnumKeys.has(key)) continue;
if (
existingEnums.has(enumType) ||
(existingEnumKeys.has(key) && !sdkContext.__typeCache.lroMetadataEnums.has(enumType))
) {
continue;
}
enums.push(enumType);
existingEnumKeys.add(key);
existingEnums.add(enumType);
if (!sdkContext.__typeCache.lroMetadataEnums.has(enumType)) {
existingEnumKeys.add(key);
}
}
}

// TODO -- TCGC now does not have constants field in its sdkPackage, they might add it in the future.
const constants = Array.from(sdkContext.__typeCache.constants.values());

// Fix naming conflicts for constants, enums, and models
fixNamingConflicts(models, constants);
fixNamingConflicts(
models,
enums,
constants,
sdkContext.__typeCache.lroMetadataEnums,
typesBeforeClients,
sdkContext.__typeCache.clients.values(),
);
// Name normalization copies the owning model's usage; LRO metadata enums are always deserialized.
for (const type of sdkContext.__typeCache.lroMetadataEnums) {
type.usage |= UsageFlags.Output | UsageFlags.Json;
}

// Resolve the root namespace. When this cannot be determined the generated code model
// has no name, which the .NET generator rejects with an opaque root-level deserialization
Expand Down Expand Up @@ -166,24 +190,29 @@ function parseApiVersions(
* @param enums - Array of input enum types
* @param constants - Array of input literal types (constants)
*/
function fixNamingConflicts(models: InputModelType[], constants: InputLiteralType[]): void {
function fixNamingConflicts(
Comment thread
iscai-msft marked this conversation as resolved.
Resolved
models: InputModelType[],
enums: InputEnumType[],
constants: InputLiteralType[],
lroMetadataEnums: Set<InputEnumType>,
typesBeforeClients: Set<InputType>,
clients: Iterable<InputClient>,
): void {
const modelEnums = new Set<InputEnumType>();
// First, fix names for constants and constant-derived enums in model properties
for (const model of models) {
for (const property of model.properties) {
const type = property.type;
let nestedType = type;
while (nestedType.kind === "nullable") nestedType = nestedType.type;
if (nestedType.kind === "enum") modelEnums.add(nestedType);

if (type.kind === "constant") {
// Fix constant property names
type.name = `${model.name}${firstLetterToUpperCase(property.name)}`;
type.namespace = model.namespace;
type.access = model.access;
type.usage = model.usage;
normalizeLiteralTypeForProperty(type, model, property.name);
} else if (type.kind === "enum" && type.crossLanguageDefinitionId === "") {
// Fix enum names for enums created from constants
type.name = `${model.name}${firstLetterToUpperCase(property.name)}`;
type.namespace = model.namespace;
type.access = model.access;
type.usage = model.usage;
normalizeLiteralTypeForProperty(type, model, property.name);
}
}
}
Expand Down Expand Up @@ -216,6 +245,48 @@ function fixNamingConflicts(models: InputModelType[], constants: InputLiteralTyp
modelNameMap.set(key, 1);
}
}

// LRO metadata enums are synthesized by this emitter from TCGC constants, so TCGC does not reserve their names.
// They must have distinct C# type and output file names without renaming existing types.
const usedTypeNames = new Set([...clients].map(csharpTypeNameKey));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reserves the original client names, but TypeSpecSerialization.BuildClientName (TypeSpecSerialization.cs:124-140) renames a deeper Root -> Group -> Child client to GroupChild; a hidden Group monitor selecting child?: "ready" can therefore emit a metadata enum with the same fully qualified name when both use a shared @clientNamespace. Could we resolve this using the final C# client names, ideally in the generator, and add a deeper-subclient regression?

🤖 Generated by Jorge's Copilot

for (const model of models) {
const name = csharpTypeNameKey(model);
usedTypeNames.add(name);
if (!model.isExactName) {
// ModelProvider may project Request/Response suffixes to Content/Result.
if (name.endsWith("REQUEST")) usedTypeNames.add(`${name.slice(0, -7)}CONTENT`);

@jorgerangel-msft Jorge Rangel (jorgerangel-msft) Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should just let the generator handle the rename here. A custom visitor can override our rename rules without using the isExactName function.

if (name.endsWith("RESPONSE")) usedTypeNames.add(`${name.slice(0, -8)}RESULT`);
}
}
for (const type of enums) {
if (!lroMetadataEnums.has(type) || typesBeforeClients.has(type)) {
usedTypeNames.add(csharpTypeNameKey(type));
}
}
const metadataEnums = enums
.filter((type) => lroMetadataEnums.has(type) && !typesBeforeClients.has(type))
.sort((a, b) => Number(modelEnums.has(b)) - Number(modelEnums.has(a)));
for (const type of metadataEnums) {
const baseName = type.name;
let suffix = 1;
while (usedTypeNames.has(csharpTypeNameKey(type))) {
type.name = `${baseName}${suffix++}`;
}
usedTypeNames.add(csharpTypeNameKey(type));
}
}

function csharpTypeNameKey(type: { name: string; isExactName?: boolean }): string {
if (type.isExactName) return type.name.toUpperCase();
// C# checks UTF-16 code units and removes separators (including underscores).
const codeUnits = type.name.replace(/[\uD800-\uDFFF]/g, " ");
const startsWithDigit = /^\p{Nd}/u.test(codeUnits);
const start = startsWithDigit ? 0 : codeUnits.search(/[\p{L}\p{Nl}_]/u);
const name = start < 0 ? codeUnits : codeUnits.slice(start);
return `${startsWithDigit ? "_" : ""}${name.replace(
/_|[^\p{L}\p{Nl}\p{Nd}\p{Mn}\p{Mc}\p{Pc}\p{Cf}]/gu,
"",
)}`.toUpperCase();
}

/**
Expand Down
Loading
Loading