Repository navigation
[.NET] Consume TCGC LRO status metadata and operation ID #12563
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
base: main
Are you sure you want to change the base?
Changes from all commits
c531ef5
8475016
1cd5d4f
7dac1f9
567ee7a
944b69e
cf52ec1
2a7b050
ffa8e1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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") { | ||
|
|
@@ -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 | ||
|
|
@@ -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( | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -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)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reserves the original client names, but
|
||
| 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`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.