diff --git a/packages/http-client-csharp/emitter/src/lib/client-model-builder.ts b/packages/http-client-csharp/emitter/src/lib/client-model-builder.ts index 4ff0c75bb43..5167dc6b61a 100644 --- a/packages/http-client-csharp/emitter/src/lib/client-model-builder.ts +++ b/packages/http-client-csharp/emitter/src/lib/client-model-builder.ts @@ -6,14 +6,17 @@ 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"; @@ -21,8 +24,8 @@ 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,9 +99,17 @@ 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); + } } } @@ -104,7 +117,18 @@ export function createModel(sdkContext: CSharpEmitterContext): [CodeModel, reado 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, + typesBeforeClients: Set, + clients: Iterable, +): void { + const modelEnums = new Set(); // 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)); + 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`); + 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(); } /** diff --git a/packages/http-client-csharp/emitter/src/lib/operation-converter.ts b/packages/http-client-csharp/emitter/src/lib/operation-converter.ts index 4ecca8158a9..e4e691276c9 100644 --- a/packages/http-client-csharp/emitter/src/lib/operation-converter.ts +++ b/packages/http-client-csharp/emitter/src/lib/operation-converter.ts @@ -27,9 +27,12 @@ import { getHttpOperationParameter, getParamAlias, getResponseAsBool, + getSdkModelWithDiagnostics, isHttpMetadata, shouldGenerateConvenient, shouldGenerateProtocol, + updateUsageOrAccess, + UsageFlags, } from "@azure-tools/typespec-client-generator-core"; import type { Diagnostic, Union } from "@typespec/compiler"; import { @@ -41,6 +44,7 @@ import { } from "@typespec/compiler"; import { unsafe_getEventDefinitions } from "@typespec/events/experimental"; import type { HttpStatusCodeRange } from "@typespec/http"; +import { getHeaderFieldName, isBody, isBodyRoot } from "@typespec/http"; import { isTerminalEvent } from "@typespec/sse"; import type { CSharpEmitterContext } from "../sdk-context.js"; import { collectionFormatToDelimMap } from "../type/collection-format.js"; @@ -54,17 +58,21 @@ import type { InputLongRunningPagingServiceMethod, InputLongRunningServiceMetadata, InputLongRunningServiceMethod, + InputLroOperationIdMetadata, + InputLroStatusMetadata, InputNextLink, InputPagingServiceMetadata, InputPagingServiceMethod, InputServiceMethod, InputServiceMethodResponse, } from "../type/input-service-method.js"; +import { InputOperationLinkLocation } from "../type/input-service-method.js"; import type { InputBodyParameter, InputHeaderParameter, InputHttpParameter, InputMethodParameter, + InputModelProperty, InputPathParameter, InputQueryParameter, InputStreamingType, @@ -79,7 +87,11 @@ import { getExternalDocs } from "./decorators.js"; import { fromSdkHttpExamples } from "./example-converter.js"; import { getExperimentalDetails } from "./experimental.js"; import { createDiagnostic } from "./lib.js"; -import { fromSdkType } from "./type-converter.js"; +import { + fromSdkModelProperty, + fromSdkType, + normalizeLiteralTypeForProperty, +} from "./type-converter.js"; import { getClientNamespaceString, isReadOnly } from "./utils.js"; export function fromSdkServiceMethod( @@ -135,7 +147,7 @@ export function fromSdkServiceMethod( ), ); lroServiceMethod.lroMetadata = diagnostics.pipe( - loadLongRunningMetadata(sdkContext, sdkMethod), + loadLongRunningMetadata(sdkContext, sdkMethod, rootApiVersions), ); method = lroServiceMethod; break; @@ -150,7 +162,7 @@ export function fromSdkServiceMethod( ), ); lroPagingMethod.lroMetadata = diagnostics.pipe( - loadLongRunningMetadata(sdkContext, sdkMethod), + loadLongRunningMetadata(sdkContext, sdkMethod, rootApiVersions), ); lroPagingMethod.pagingMetadata = diagnostics.pipe( loadPagingServiceMetadata(sdkContext, sdkMethod, rootApiVersions, uri, namespace), @@ -723,8 +735,226 @@ export function fromMethodParameter( function loadLongRunningMetadata( sdkContext: CSharpEmitterContext, method: SdkLroServiceMethod | SdkLroPagingServiceMethod, + rootApiVersions: string[], ): [InputLongRunningServiceMetadata, readonly Diagnostic[]] { const diagnostics = createDiagnosticCollector(); + const metadata = method.lroMetadata; + const step = metadata.statusMonitorStep; + const pollingOperation = + step?.kind === "nextOperationLink" ? step.operation : step?.target.operation; + let pollingMethod: SdkServiceMethod | undefined; + if (pollingOperation && (metadata.statusMetadata?.length || metadata.operationId?.length)) { + const pollingClient = sdkContext.getClientForOperation(pollingOperation.__raw.operation); + const clients = [...sdkContext.sdkPackage.clients]; + for (let index = 0; index < clients.length; index++) { + const client = clients[index]; + if (client.__raw === pollingClient) { + pollingMethod = client.methods.find( + (candidate) => candidate.operation.__raw.operation === pollingOperation.__raw.operation, + ); + break; + } + clients.push(...(client.children ?? [])); + } + } + const reportInvalidSelection = (message: string) => { + diagnostics.add( + createDiagnostic({ + code: "general-error", + format: { message: `Invalid LRO metadata for '${method.name}': ${message}` }, + target: method.__raw ?? NoTarget, + }), + ); + }; + + const convertSelectedProperty = (sdkProperty: SdkModelPropertyType) => { + let sdkType = sdkProperty.type; + while (sdkType.kind === "nullable") sdkType = sdkType.type; + const alreadyConverted = sdkContext.__typeCache.types.has(sdkType); + const property = diagnostics.pipe( + fromSdkModelProperty(sdkContext, sdkProperty, metadata.pollingInfo.responseModel), + ); + if (!property) return undefined; + + const selectedTypes = new Set(); + diagnostics.pipe( + updateUsageOrAccess(sdkContext, UsageFlags.Output, sdkProperty.type, { + seenTypes: selectedTypes, + }), + ); + for (const selectedType of selectedTypes) { + if (selectedType.kind === "model" && selectedType.baseModel) { + selectedTypes.add(selectedType.baseModel); + } + if ( + (selectedType.kind === "model" || selectedType.kind === "enum") && + selectedType.access === "internal" + ) { + const input = sdkContext.__typeCache.types.get(selectedType); + if (input?.kind === "model" || input?.kind === "enum") { + input.access ??= "internal"; + } + } + } + + let inputType = property.type; + while (inputType.kind === "nullable") inputType = inputType.type; + if ( + sdkType.kind === "constant" && + inputType.kind === "enum" && + inputType.crossLanguageDefinitionId === "" + ) { + if (!alreadyConverted) { + const owner = sdkProperty.__raw?.model + ? diagnostics.pipe(getSdkModelWithDiagnostics(sdkContext, sdkProperty.__raw.model)) + : metadata.pollingInfo.responseModel; + normalizeLiteralTypeForProperty( + inputType, + { + name: owner.name, + namespace: owner.namespace, + access: + owner.access === "internal" || sdkProperty.access === "internal" + ? "internal" + : undefined, + usage: owner.usage, + }, + property.name, + ); + } + inputType.usage |= UsageFlags.Output | UsageFlags.Json; + sdkContext.__typeCache.lroMetadataEnums.add(inputType); + } + return property; + }; + + const convertSelection = (entry: { + property: SdkModelPropertyType; + propertySegments: SdkModelPropertyType[]; + }): InputLroStatusMetadata | undefined => { + if (!entry.property || !entry.propertySegments?.length) { + reportInvalidSelection("a selected property must have a nonempty property path."); + return undefined; + } + + const property = convertSelectedProperty(entry.property); + if (!property) { + reportInvalidSelection("a selected property could not be converted."); + return undefined; + } + + const propertySegments: InputModelProperty[] = []; + for (const segment of entry.propertySegments) { + const converted = convertSelectedProperty(segment); + if (!converted) { + reportInvalidSelection("a selected property path could not be converted."); + return undefined; + } + propertySegments.push(converted); + } + return { property, propertySegments }; + }; + + const statusMetadata: InputLroStatusMetadata[] = []; + for (const entry of metadata.statusMetadata ?? []) { + const selection = convertSelection(entry); + if (selection) { + statusMetadata.push(selection); + } + } + + const operationId: InputLroOperationIdMetadata[] = []; + for (const entry of metadata.operationId ?? []) { + if (entry.kind === "property") { + const selection = convertSelection(entry); + if (selection) { + operationId.push({ kind: "property", ...selection }); + } + continue; + } + + if (entry.kind !== "path") { + reportInvalidSelection("the operation ID has an unsupported selection kind."); + continue; + } + + if (!pollingOperation?.path || !entry.pathParameter || entry.pathParameter.kind !== "path") { + reportInvalidSelection( + "the operation ID requires a linked polling operation and path parameter.", + ); + continue; + } + + // TCGC constructs a separate polling operation for LRO metadata. Use the actual + // client method's parameter when it is exposed so its cached input is shared. + const sdkPollingOperation = pollingMethod?.operation ?? pollingOperation; + const sdkPathParameters = sdkPollingOperation.parameters.filter( + (parameter) => + parameter.kind === "path" && + parameter.crossLanguageDefinitionId === entry.pathParameter.crossLanguageDefinitionId, + ); + if (sdkPathParameters.length !== 1) { + reportInvalidSelection( + "the operation ID path parameter must uniquely match a polling operation parameter.", + ); + continue; + } + const pathParameter = diagnostics.pipe( + fromParameter(sdkContext, sdkPathParameters[0], rootApiVersions), + ); + if (!pathParameter || pathParameter.kind !== "path") { + reportInvalidSelection("the operation ID path parameter could not be converted."); + continue; + } + + if (!entry.source?.property) { + reportInvalidSelection("the operation ID polling link must have a source property."); + continue; + } + + const source = entry.source.property; + const rawSource = source.__raw; + if (!rawSource?.model) { + reportInvalidSelection("the operation ID polling link must have a source model property."); + continue; + } + + let location: InputOperationLinkLocation; + switch (entry.source.location) { + case "ResponseHeader": + location = InputOperationLinkLocation.ResponseHeader; + break; + case "ResponseBody": + location = + isBody(sdkContext.program, rawSource) || isBodyRoot(sdkContext.program, rawSource) + ? InputOperationLinkLocation.Self + : InputOperationLinkLocation.ResponseBody; + break; + case "Self": + location = InputOperationLinkLocation.Self; + break; + default: + reportInvalidSelection("the operation ID has an unsupported polling link location."); + continue; + } + + const sourceModel = diagnostics.pipe(getSdkModelWithDiagnostics(sdkContext, rawSource.model)); + const convertedSource = diagnostics.pipe(fromSdkModelProperty(sdkContext, source, sourceModel)); + if (!convertedSource) { + reportInvalidSelection("the operation ID polling link property could not be converted."); + continue; + } + const sourceProperty = + location === InputOperationLinkLocation.ResponseHeader + ? { ...convertedSource, serializedName: getHeaderFieldName(sdkContext.program, rawSource) } + : convertedSource; + operationId.push({ + kind: "path", + source: { location, property: sourceProperty }, + pathParameter, + pollingPath: pollingOperation.path, + }); + } return diagnostics.wrap({ finalStateVia: convertLroFinalStateVia(method.lroMetadata.finalStateVia), @@ -740,6 +970,14 @@ function loadLongRunningMetadata( : undefined, } as OperationResponse, resultPath: method.lroMetadata.finalResultPath, + get pollingResponseModel() { + if (statusMetadata.length === 0 && operationId.length === 0) return undefined; + // Other client methods may expose this model after the LRO is converted. + const model = sdkContext.__typeCache.types.get(metadata.pollingInfo.responseModel); + return model?.kind === "model" ? model : undefined; + }, + statusMetadata: metadata.statusMetadata ? statusMetadata : undefined, + operationId: metadata.operationId ? operationId : undefined, }); } diff --git a/packages/http-client-csharp/emitter/src/lib/type-converter.ts b/packages/http-client-csharp/emitter/src/lib/type-converter.ts index eea761b987b..46a74cf0fca 100644 --- a/packages/http-client-csharp/emitter/src/lib/type-converter.ts +++ b/packages/http-client-csharp/emitter/src/lib/type-converter.ts @@ -52,7 +52,7 @@ import type { } from "../type/input-type.js"; import { getExperimentalDetails } from "./experimental.js"; import { createDiagnostic } from "./lib.js"; -import { isReadOnly } from "./utils.js"; +import { firstLetterToUpperCase, isReadOnly } from "./utils.js"; // we have this complicated type here to let the caller of fromSdkType could infer the real return type of this function. type InputReturnType = T extends { kind: "nullable" } @@ -288,7 +288,7 @@ function fromSdkModelType( return diagnostics.wrap(inputModelType); } -function fromSdkModelProperty( +export function fromSdkModelProperty( sdkContext: CSharpEmitterContext, sdkProperty: SdkModelPropertyType, sdkModel: SdkModelType, @@ -322,6 +322,7 @@ function fromSdkModelProperty( ), optional: sdkProperty.optional, readOnly: isReadOnly(sdkProperty), + access: sdkProperty.__raw ? getAccessOverride(sdkContext, sdkProperty.__raw) : undefined, discriminator: sdkProperty.discriminator, flatten: sdkProperty.flatten, decorators: sdkProperty.decorators, @@ -418,6 +419,17 @@ function createEnumType( return diagnostics.wrap(inputEnumType); } +export function normalizeLiteralTypeForProperty( + type: InputLiteralType | InputEnumType, + model: Pick, + propertyName: string, +): void { + type.name = `${model.name}${firstLetterToUpperCase(propertyName)}`; + type.namespace = model.namespace; + type.access = model.access; + type.usage = model.usage; +} + function fromSdkDateTimeType( sdkContext: CSharpEmitterContext, dateTimeType: SdkDateTimeType, diff --git a/packages/http-client-csharp/emitter/src/sdk-context.ts b/packages/http-client-csharp/emitter/src/sdk-context.ts index d5c96429f60..16094ebfa6e 100644 --- a/packages/http-client-csharp/emitter/src/sdk-context.ts +++ b/packages/http-client-csharp/emitter/src/sdk-context.ts @@ -21,6 +21,7 @@ import type { InputOperation } from "./type/input-operation.js"; import type { InputServiceMethod } from "./type/input-service-method.js"; import type { InputClient, + InputEnumType, InputHttpParameter, InputLiteralType, InputMethodParameter, @@ -65,6 +66,7 @@ class SdkTypeCache { properties: Map; responses: Map; types: Map; + lroMetadataEnums: Set; constants: Map; crossLanguageDefinitionIds: Map; namespaces: Map; @@ -78,6 +80,7 @@ class SdkTypeCache { this.properties = new Map(); this.responses = new Map(); this.types = new Map(); + this.lroMetadataEnums = new Set(); this.constants = new Map(); this.crossLanguageDefinitionIds = new Map(); this.namespaces = new Map(); diff --git a/packages/http-client-csharp/emitter/src/type/input-service-method.ts b/packages/http-client-csharp/emitter/src/type/input-service-method.ts index 26c49336197..14af4bf2d40 100644 --- a/packages/http-client-csharp/emitter/src/type/input-service-method.ts +++ b/packages/http-client-csharp/emitter/src/type/input-service-method.ts @@ -2,7 +2,14 @@ // Licensed under the MIT License. See License.txt in the project root for license information. import type { InputOperation } from "./input-operation.js"; -import type { InputMethodParameter, InputParameter, InputType } from "./input-type.js"; +import type { + InputMethodParameter, + InputModelProperty, + InputModelType, + InputParameter, + InputPathParameter, + InputType, +} from "./input-type.js"; import type { OperationFinalStateVia } from "./operation-final-state-via.js"; import type { OperationResponse } from "./operation-response.js"; import type { ResponseLocation } from "./response-location.js"; @@ -75,8 +82,46 @@ export interface InputLongRunningServiceMetadata { finalStateVia: OperationFinalStateVia; finalResponse: OperationResponse; resultPath?: string; + /** Present when the polling response model is otherwise emitted. */ + pollingResponseModel?: InputModelType; + statusMetadata?: InputLroStatusMetadata[]; + operationId?: InputLroOperationIdMetadata[]; } +export interface InputLroStatusMetadata { + property: InputModelProperty; + /** Typed path relative to the polling response model, not the final result. */ + propertySegments: InputModelProperty[]; +} + +export enum InputOperationLinkLocation { + Unknown = "Unknown", + ResponseHeader = "ResponseHeader", + ResponseBody = "ResponseBody", + Self = "Self", +} + +export interface InputOperationLink { + location: InputOperationLinkLocation; + property: InputModelProperty; +} + +export interface InputLroOperationIdPathMetadata { + kind: "path"; + source: InputOperationLink; + pathParameter: InputPathParameter; + pollingPath: string; +} + +export interface InputLroOperationIdPropertyMetadata { + kind: "property"; + property: InputModelProperty; + propertySegments: InputModelProperty[]; +} + +export type InputLroOperationIdMetadata = + InputLroOperationIdPathMetadata | InputLroOperationIdPropertyMetadata; + export interface InputLongRunningPagingServiceMethod extends InputServiceMethodBase { kind: "lropaging"; lroMetadata: InputLongRunningServiceMetadata; diff --git a/packages/http-client-csharp/emitter/test/Unit/operation-converter.test.ts b/packages/http-client-csharp/emitter/test/Unit/operation-converter.test.ts index 86f691376fd..d2f9d8af538 100644 --- a/packages/http-client-csharp/emitter/test/Unit/operation-converter.test.ts +++ b/packages/http-client-csharp/emitter/test/Unit/operation-converter.test.ts @@ -1,5 +1,7 @@ -import type { TestHost } from "@typespec/compiler/testing"; -import { deepStrictEqual, ok, strictEqual } from "assert"; +import { UsageFlags } from "@azure-tools/typespec-client-generator-core"; +import { expectDiagnostics, type TestHost } from "@typespec/compiler/testing"; +import { deepStrictEqual, notStrictEqual, ok, strictEqual } from "assert"; +import { readFileSync } from "node:fs"; import { beforeEach, describe, it } from "vitest"; import { serializeCodeModel } from "../../src/code-model-writer.js"; import { createModel } from "../../src/lib/client-model-builder.js"; @@ -12,6 +14,68 @@ import { typeSpecCompile, } from "./utils/test-util.js"; +// Source for generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/tspCodeModel.json. +const lroMetadataFixtureSource = ` +import "@typespec/http"; +import "@typespec/rest"; +import "@azure-tools/typespec-azure-core"; +import "@azure-tools/typespec-client-generator-core"; + +using TypeSpec.Http; +using TypeSpec.Rest; +using Azure.Core; +using Azure.ClientGenerator.Core; + +@service +namespace LroFixture; + +model Statistics { + completed: int32; +} + +model BaseMonitor { + @lroStatusMetadata + @clientName("updatedAt") + @encodedName("application/json", "last_updated") + updated?: utcDateTime; +} + +model Monitor extends BaseMonitor { + @lroStatus + status: "Running" | "Succeeded" | "Failed" | "Canceled"; + + @lroResult + result?: string; + + @lroOperationId + @clientName("jobId") + @encodedName("application/json", "job_id") + id: string; + + @lroStatusMetadata + @access(Access.internal, "csharp") + @clientName("progress") + @encodedName("application/json", "percent_complete") + percent?: int32; + + @lroStatusMetadata + statistics?: Statistics; +} + +@get +@route("/jobs/{jobId}/status") +op getStatus(@lroOperationId @path("jobId") id: string): Monitor; + +@post +@route("/jobs") +@access(Access.internal, "csharp") +@pollingOperation(getStatus) +op begin(): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; +}; +`; + describe("Operation Converter", () => { let runner: TestHost; @@ -19,6 +83,1375 @@ describe("Operation Converter", () => { runner = await createEmitterTestHost(); }); + it("emits the LRO metadata fixture consumed by the C# deserializer", async () => { + const fixture = new URL( + "../../../generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/", + import.meta.url, + ); + runner.addTypeSpecFile("main.tsp", lroMetadataFixtureSource); + await runner.compile("./", { noEmit: true }); + const sdkContext = await createCSharpSdkContext(createEmitterContext(runner.program)); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const actual = JSON.parse(serializeCodeModel(sdkContext, root)); + const expected = JSON.parse(readFileSync(new URL("tspCodeModel.json", fixture), "utf8")); + deepStrictEqual(actual, expected); + }); + + it.each( + ["both", "body", "path", "none"].flatMap((selection) => + [true, false].map((pollingFirst) => ({ selection, pollingFirst })), + ), + )( + "preserves LRO selections: $selection, polling operation declared first: $pollingFirst", + async ({ selection, pollingFirst }) => { + const selected = selection !== "none"; + const pollingOperation = ` + @get @route("/jobs/{jobId}/status") + op getStatus( + ${["both", "path"].includes(selection) ? "@lroOperationId" : ""} + @path("jobId") id: string + ): Monitor; + `; + const startOperation = ` + @post @route("/jobs") + @pollingOperation(getStatus) + op begin(): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + }; + `; + const program = await typeSpecCompile( + ` + model Statistics { completed: int32; } + model BaseMonitor { + ${selected ? "@lroStatusMetadata" : ""} + @clientName("updatedAt") + @encodedName("application/json", "last_updated") + updated?: utcDateTime; + } + model Monitor extends BaseMonitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroResult result?: string; + ${["both", "body"].includes(selection) ? "@lroOperationId" : ""} + @clientName("jobId") + @encodedName("application/json", "job_id") + id: string; + ${selected ? "@lroStatusMetadata" : ""} + @clientName("progress") + @encodedName("application/json", "percent_complete") + percent?: int32; + ${selected ? "@lroStatusMetadata" : ""} + statistics?: Statistics; + } + ${pollingFirst ? `${pollingOperation}\n${startOperation}` : `${startOperation}\n${pollingOperation}`} + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const method = root.clients[0].methods.find((m) => m.name === "begin"); + ok(method?.kind === "lro"); + const metadata = method.lroMetadata; + if (!selected) { + strictEqual(metadata.statusMetadata, undefined); + strictEqual(metadata.operationId, undefined); + strictEqual(metadata.pollingResponseModel, undefined); + const json = serializeCodeModel(sdkContext, root); + strictEqual(json.includes('"statusMetadata"'), false); + strictEqual(json.includes('"operationId"'), false); + strictEqual(json.includes('"pollingResponseModel"'), false); + return; + } + strictEqual( + metadata.pollingResponseModel, + root.models.find((m) => m.name === "Monitor"), + ); + deepStrictEqual( + metadata.statusMetadata?.map((entry) => [ + entry.property.name, + entry.property.optional, + entry.propertySegments.map((p) => p.serializedName), + ]), + [ + ["progress", true, ["percent_complete"]], + ["statistics", true, ["statistics"]], + ["updatedAt", true, ["last_updated"]], + ], + ); + for (const entry of metadata.statusMetadata!) { + strictEqual(entry.propertySegments[0], entry.property); + } + const baseMonitor = root.models.find((m) => m.name === "BaseMonitor"); + ok(baseMonitor); + strictEqual( + metadata.statusMetadata![2].property, + baseMonitor.properties.find((p) => p.name === "updatedAt"), + ); + const statistics = metadata.statusMetadata![1].property.type; + strictEqual( + statistics, + root.models.find((m) => m.name === "Statistics"), + ); + ok(statistics.kind === "model"); + strictEqual(statistics.properties[0].name, "completed"); + deepStrictEqual( + metadata.operationId?.map((entry) => entry.kind), + selection === "both" ? ["path", "property"] : [selection === "body" ? "property" : "path"], + ); + for (const entry of metadata.operationId!) { + if (entry.kind === "path") { + strictEqual(entry.pollingPath, "/jobs/{jobId}/status"); + strictEqual(entry.pathParameter.serializedName, "jobId"); + strictEqual( + entry.pathParameter, + root.clients[0].methods + .find((m) => m.name === "getStatus")! + .operation.parameters.find((p) => p.kind === "path"), + ); + strictEqual(entry.source.location, "ResponseHeader"); + strictEqual(entry.source.property.serializedName, "Operation-Location"); + const sdkMethod = sdkContext.sdkPackage.clients[0].methods.find( + (m) => m.name === "begin", + ); + ok(sdkMethod?.kind === "lro"); + const sdkPath = sdkMethod.lroMetadata.operationId?.find((id) => id.kind === "path"); + ok(sdkPath?.kind === "path"); + const cachedSourceProperty = sdkContext.__typeCache.properties.get( + sdkPath.source.property, + ); + ok(cachedSourceProperty); + strictEqual( + cachedSourceProperty.serializedName, + sdkPath.source.property.serializationOptions.json?.name ?? + sdkPath.source.property.serializationOptions.xml?.name ?? + sdkPath.source.property.serializationOptions.multipart?.name, + ); + notStrictEqual(entry.source.property, cachedSourceProperty); + } else { + strictEqual(entry.property.name, "jobId"); + strictEqual(entry.propertySegments[0], entry.property); + strictEqual(entry.property.serializedName, "job_id"); + } + } + const serialized = JSON.parse(serializeCodeModel(sdkContext, root)); + const serializedMetadata = serialized.clients[0].methods.find( + (m: { name: string }) => m.name === "begin", + ).lroMetadata; + const serializedMonitor = + serialized.models[root.models.indexOf(metadata.pollingResponseModel!)]; + strictEqual( + serializedMetadata.pollingResponseModel.$ref ?? serializedMetadata.pollingResponseModel.$id, + serializedMonitor.$ref ?? serializedMonitor.$id, + ); + deepStrictEqual( + serializedMetadata.statusMetadata.map( + (entry: { property: object; propertySegments: object[] }) => entry.propertySegments[0], + ), + serializedMetadata.statusMetadata.map( + (entry: { property: { $ref: string; $id: string } }) => ({ + $ref: entry.property.$ref ?? entry.property.$id, + }), + ), + ); + deepStrictEqual( + serializedMetadata.operationId.map((entry: { kind: string }) => entry.kind), + metadata.operationId!.map((entry) => entry.kind), + ); + if (["both", "path"].includes(selection)) { + const serializedPath = serializedMetadata.operationId.find( + (entry: { kind: string }) => entry.kind === "path", + ).pathParameter; + const pollingMethod = root.clients[0].methods.find((m) => m.name === "getStatus")!; + const parameterIndex = pollingMethod.operation.parameters.findIndex( + (p) => p.kind === "path" && p.serializedName === "jobId", + ); + ok(parameterIndex >= 0); + const serializedPollingParameter = serialized.clients[0].methods.find( + (m: { name: string }) => m.name === "getStatus", + ).operation.parameters[parameterIndex]; + strictEqual( + serializedPath.$id ?? serializedPath.$ref, + serializedPollingParameter.$id ?? serializedPollingParameter.$ref, + ); + ok(serializedPath.$ref || serializedPollingParameter.$ref); + } + }, + ); + + it.each([ + "missing polling operation", + "missing path parameter", + "missing header source", + "unknown link location", + ])("reports a diagnostic for an LRO operation ID with %s", async (malformation) => { + const program = await typeSpecCompile( + ` + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroResult result?: string; + @lroOperationId id: string; + } + @get @route("/jobs/{jobId}/status") + op getStatus(@lroOperationId @path("jobId") id: string): Monitor; + @post @route("/jobs") @pollingOperation(getStatus) + op begin(): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + }; + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const sdkMethod = sdkContext.sdkPackage.clients[0].methods.find((m) => m.name === "begin"); + ok(sdkMethod?.kind === "lro"); + const path = sdkMethod.lroMetadata.operationId?.find((entry) => entry.kind === "path"); + ok(path?.kind === "path"); + switch (malformation) { + case "missing polling operation": + sdkMethod.lroMetadata.statusMonitorStep = undefined; + break; + case "missing path parameter": + path.pathParameter.crossLanguageDefinitionId = "missing.parameter"; + break; + case "missing header source": + path.source.property.__raw = undefined; + break; + case "unknown link location": + // Simulate an unrecognized location from a future TCGC version. + // @ts-expect-error The current TCGC type lists only known locations. + path.source.location = "FutureLocation"; + break; + } + const [root, diagnostics] = createModel(sdkContext); + const method = root.clients[0].methods.find((m) => m.name === "begin"); + ok(method?.kind === "lro"); + deepStrictEqual( + method.lroMetadata.operationId?.map((entry) => entry.kind), + ["property"], + ); + expectDiagnostics(diagnostics, { + severity: "error", + code: "@typespec/http-client-csharp/general-error", + message: /Invalid LRO metadata for 'begin': .*operation ID/, + }); + }); + + it("reports an empty status metadata path without dropping valid selections", async () => { + const program = await typeSpecCompile( + ` + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroResult result?: string; + @lroStatusMetadata progress?: int32; + @lroOperationId id: string; + } + @get @route("/jobs/{jobId}/status") + op getStatus(@lroOperationId @path("jobId") id: string): Monitor; + @post @route("/jobs") @pollingOperation(getStatus) + op begin(): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + }; + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const sdkMethod = sdkContext.sdkPackage.clients[0].methods.find((m) => m.name === "begin"); + ok(sdkMethod?.kind === "lro"); + ok(sdkMethod.lroMetadata.statusMetadata?.[0]); + sdkMethod.lroMetadata.statusMetadata[0].propertySegments = []; + + const [root, diagnostics] = createModel(sdkContext); + const method = root.clients[0].methods.find((m) => m.name === "begin"); + ok(method?.kind === "lro"); + deepStrictEqual(method.lroMetadata.statusMetadata, []); + deepStrictEqual( + method.lroMetadata.operationId?.map((entry) => entry.kind), + ["path", "property"], + ); + expectDiagnostics(diagnostics, { + severity: "error", + code: "@typespec/http-client-csharp/general-error", + message: + "Invalid LRO metadata for 'begin': a selected property must have a nonempty property path.", + }); + }); + + it("shares the selected parameter with a polling operation in another client", async () => { + const program = await typeSpecCompile( + ` + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroResult result?: string; + } + interface Start { + @post @route("/jobs") @pollingOperation(Polling.getStatus) + begin(): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + }; + } + interface Polling { + @get @route("/jobs/{jobId}/status") + getStatus(@lroOperationId @path("jobId") id: string): Monitor; + } + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const clients = [...root.clients]; + for (let index = 0; index < clients.length; index++) { + clients.push(...(clients[index].children ?? [])); + } + const methods = clients.flatMap((client) => client.methods); + const start = methods.find((method) => method.name === "begin"); + const poll = methods.find((method) => method.name === "getStatus"); + ok(start?.kind === "lro"); + ok(poll); + const path = start.lroMetadata.operationId?.find((entry) => entry.kind === "path"); + ok(path?.kind === "path"); + strictEqual( + path.pathParameter, + poll.operation.parameters.find((parameter) => parameter.kind === "path"), + ); + }); + + it("preserves a path selection when the polling operation is not exposed as a client method", async () => { + const program = await typeSpecCompile( + ` + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroResult result?: string; + } + @get @route("/jobs/{jobId}/status") + op getStatus(@lroOperationId @path("jobId") id: string): Monitor; + @post @route("/jobs") @pollingOperation(getStatus) + op begin(): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + }; + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const client = sdkContext.sdkPackage.clients[0]; + client.methods = client.methods.filter((method) => method.name !== "getStatus"); + + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const method = root.clients[0].methods.find((m) => m.name === "begin"); + ok(method?.kind === "lro"); + const path = method.lroMetadata.operationId?.find((entry) => entry.kind === "path"); + ok(path?.kind === "path"); + strictEqual(path.pathParameter.serializedName, "jobId"); + strictEqual(path.pollingPath, "/jobs/{jobId}/status"); + }); + + it.each([ + { expansion: "{jobId}", style: "simple" }, + { expansion: "{.jobId}", style: "label" }, + { expansion: "{;jobId}", style: "matrix" }, + { expansion: "{/jobId}", style: "path" }, + { expansion: "{#jobId}", style: "fragment" }, + ])( + "preserves the $style style and shared identity of a spread polling ID", + async ({ expansion, style }) => { + const program = await typeSpecCompile( + ` + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroResult result?: string; + } + model PollingParameters { + @lroOperationId @path("jobId") id: string; + } + @get @route("/jobs${expansion}/status") + op getStatus(...PollingParameters): Monitor; + @post @route("/jobs") @pollingOperation(getStatus) + op begin(): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + }; + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const start = root.clients[0].methods.find((method) => method.name === "begin"); + const poll = root.clients[0].methods.find((method) => method.name === "getStatus"); + ok(start?.kind === "lro"); + ok(poll); + const selected = start.lroMetadata.operationId?.find((entry) => entry.kind === "path"); + ok(selected?.kind === "path"); + const parameter = poll.operation.parameters.find((parameter) => parameter.kind === "path"); + ok(parameter?.kind === "path"); + strictEqual(selected.pathParameter, parameter); + strictEqual(selected.pollingPath, "/jobs{jobId}/status"); + strictEqual(parameter.serializedName, "jobId"); + strictEqual(parameter.style, style); + + const serialized = JSON.parse(serializeCodeModel(sdkContext, root)); + const serializedStart = serialized.clients[0].methods.find( + (method: { name: string }) => method.name === "begin", + ); + const serializedPoll = serialized.clients[0].methods.find( + (method: { name: string }) => method.name === "getStatus", + ); + const serializedSelection = serializedStart.lroMetadata.operationId.find( + (entry: { kind: string }) => entry.kind === "path", + ); + const serializedParameter = serializedPoll.operation.parameters.find( + (entry: { kind: string }) => entry.kind === "path", + ); + strictEqual( + serializedSelection.pathParameter.$id ?? serializedSelection.pathParameter.$ref, + serializedParameter.$id ?? serializedParameter.$ref, + ); + ok(serializedSelection.pathParameter.$ref || serializedParameter.$ref); + strictEqual( + (serializedParameter.$ref ? serializedSelection.pathParameter : serializedParameter).style, + style, + ); + }, + ); + + it.each([true, false])( + "preserves selected properties without converting an unexposed polling model, path ID: %s", + async (hasPathId) => { + const program = await typeSpecCompile( + ` + model Details { value: string; } + model FinalResult { value: string; } + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroStatusMetadata details?: Details; + unrelated: [string, int32]; + } + @get @route("/jobs/{jobId}/status") + @convenientAPI(false, "csharp") + op getStatus(${hasPathId ? "@lroOperationId" : ""} @path("jobId") id: string): Monitor; + @get @route("/jobs/{jobId}") + @convenientAPI(false, "csharp") + op getResult(@path("jobId") id: string): FinalResult; + @client({ service: Azure.Csharp.Testing }) + interface PublicOperations { + @post @route("/jobs/{jobId}") @pollingOperation(getStatus) @finalOperation(getResult) + @useFinalStateVia("original-uri") + @convenientAPI(false, "csharp") + op begin(@path("jobId") id: string): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + @finalLocation @header("Location") finalLocation: ResourceLocation; + }; + } + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const client = sdkContext.sdkPackage.clients[0]; + strictEqual( + client.methods.some((method) => method.name === "getStatus"), + false, + ); + strictEqual( + sdkContext.sdkPackage.models.some((model) => model.name === "Monitor"), + false, + ); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual( + diagnostics.map((diagnostic) => diagnostic.code), + [], + ); + const method = root.clients[0].methods.find((m) => m.name === "begin"); + ok(method?.kind === "lro"); + strictEqual(method.lroMetadata.pollingResponseModel, undefined); + strictEqual(method.lroMetadata.operationId?.length ?? 0, hasPathId ? 1 : 0); + strictEqual( + root.models.some((model) => model.name === "Monitor"), + false, + ); + const selection = method.lroMetadata.statusMetadata?.[0]; + ok(selection); + strictEqual(selection.propertySegments[0], selection.property); + ok(selection.property.type.kind === "model"); + strictEqual(selection.property.type.properties[0].name, "value"); + strictEqual( + selection.property.type, + root.models.find((model) => model.name === "Details"), + ); + const serialized = JSON.parse(serializeCodeModel(sdkContext, root)); + const serializedMethod = serialized.clients[0].methods.find( + (m: { name: string }) => m.name === "begin", + ); + const serializedSelection = serializedMethod.lroMetadata.statusMetadata[0]; + strictEqual(serializedMethod.lroMetadata.pollingResponseModel, undefined); + strictEqual(serializedSelection.propertySegments[0].$ref, serializedSelection.property.$id); + strictEqual( + serializedSelection.property.type.$ref, + serialized.models.find((model: { name: string }) => model.name === "Details").$id, + ); + }, + ); + + it.each([true, false])( + "retains an exposed protocol-only polling model when declared first: %s", + async (pollingFirst) => { + const pollingOperation = ` + @get @route("/jobs/{jobId}/status") + @convenientAPI(false, "csharp") + op getStatus(@lroOperationId @path("jobId") id: string): Monitor; + `; + const startOperation = ` + @post @route("/jobs") @pollingOperation(getStatus) + @convenientAPI(false, "csharp") + op begin(): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + }; + `; + const program = await typeSpecCompile( + ` + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroResult result?: string; + @lroStatusMetadata progress?: int32; + } + ${pollingFirst ? `${pollingOperation}\n${startOperation}` : `${startOperation}\n${pollingOperation}`} + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + strictEqual( + sdkContext.sdkPackage.models.some((model) => model.name === "Monitor"), + false, + ); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const method = root.clients[0].methods.find((m) => m.name === "begin"); + ok(method?.kind === "lro"); + const monitor = method.lroMetadata.pollingResponseModel; + ok(monitor); + strictEqual( + monitor, + root.models.find((model) => model.name === "Monitor"), + ); + strictEqual( + method.lroMetadata.statusMetadata?.[0].property, + monitor.properties.find((property) => property.name === "progress"), + ); + }, + ); + + it.each([true, false])( + "uses a monitor exposed by a different protocol-only method regardless of its order: %s", + async (readerFirst) => { + const reader = ` + @get @route("/inspect") + @convenientAPI(false, "csharp") + op inspect(): Monitor; + `; + const start = ` + @post @route("/jobs/{jobId}") @pollingOperation(getStatus) @finalOperation(getResult) + @useFinalStateVia("original-uri") + @convenientAPI(false, "csharp") + op begin(@path("jobId") id: string): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + @finalLocation @header("Location") finalLocation: ResourceLocation; + }; + `; + const program = await typeSpecCompile( + ` + model FinalResult { value: string; } + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroStatusMetadata progress?: int32; + } + @get @route("/jobs/{jobId}/status") + op getStatus(@path("jobId") id: string): Monitor; + @get @route("/jobs/{jobId}") + op getResult(@path("jobId") id: string): FinalResult; + @client({ service: Azure.Csharp.Testing }) + interface PublicOperations { + ${readerFirst ? `${reader}\n${start}` : `${start}\n${reader}`} + } + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + strictEqual( + sdkContext.sdkPackage.models.some((model) => model.name === "Monitor"), + false, + ); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const method = root.clients[0].methods.find((method) => method.name === "begin"); + ok(method?.kind === "lro"); + const monitor = root.models.find((model) => model.name === "Monitor"); + ok(monitor); + strictEqual(method.lroMetadata.pollingResponseModel === monitor, true); + strictEqual( + method.lroMetadata.statusMetadata?.[0].property, + monitor.properties.find((property) => property.name === "progress"), + ); + const serialized = JSON.parse(serializeCodeModel(sdkContext, root)); + const serializedMethod = serialized.clients[0].methods.find( + (method: { name: string }) => method.name === "begin", + ); + const serializedMonitor = serialized.models.find( + (model: { name: string }) => model.name === "Monitor", + ); + strictEqual(serializedMethod.lroMetadata.pollingResponseModel.$ref, serializedMonitor.$id); + }, + ); + + it("does not convert the original polling model when the exposed response uses an alternate type", async () => { + const program = await typeSpecCompile( + ` + model FinalResult { value: string; } + model Replacement { value: string; } + @alternateType(Replacement, "csharp") + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroStatusMetadata progress?: int32; + unrelated: [string, int32]; + } + @get @route("/jobs/{jobId}/status") + @convenientAPI(false, "csharp") + op getStatus(@path("jobId") id: string): Monitor; + @get @route("/jobs/{jobId}") + @convenientAPI(false, "csharp") + op getResult(@path("jobId") id: string): FinalResult; + @post @route("/jobs/{jobId}") @pollingOperation(getStatus) @finalOperation(getResult) + @useFinalStateVia("original-uri") + @convenientAPI(false, "csharp") + op begin(@path("jobId") id: string): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + @finalLocation @header("Location") finalLocation: ResourceLocation; + }; + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const sdkStart = sdkContext.sdkPackage.clients[0].methods.find( + (method) => method.name === "begin", + ); + const sdkPoll = sdkContext.sdkPackage.clients[0].methods.find( + (method) => method.name === "getStatus", + ); + ok(sdkStart?.kind === "lro"); + ok(sdkPoll); + strictEqual(sdkStart.lroMetadata.pollingInfo.responseModel.name, "Monitor"); + strictEqual( + sdkPoll.operation.responses.some( + (response) => response.type?.kind === "model" && response.type.name === "Replacement", + ), + true, + ); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual( + diagnostics.map((diagnostic) => diagnostic.code), + [], + ); + const method = root.clients[0].methods.find((method) => method.name === "begin"); + ok(method?.kind === "lro"); + strictEqual(method.lroMetadata.pollingResponseModel, undefined); + strictEqual( + root.models.some((model) => model.name === "Monitor"), + false, + ); + strictEqual(method.lroMetadata.statusMetadata?.[0].property.name, "progress"); + }); + + it("preserves effective scoped access on the type graph of a hidden monitor selection", async () => { + const program = await typeSpecCompile( + ` + enum State { Ready: "ready", Waiting: "waiting" } + model BaseDetails { state: State; } + model NestedDetails extends BaseDetails { value: string; } + model Details { nested: NestedDetails; } + model PublicDetails { value: string; } + model FinalResult { value: string; } + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroStatusMetadata @access(Access.internal, "csharp") + details?: Details; + @lroStatusMetadata @access(Access.internal, "csharp") + publicDetails?: PublicDetails; + } + @get @route("/jobs/{jobId}/status") + @convenientAPI(false, "csharp") + op getStatus(@path("jobId") id: string): Monitor; + @get @route("/jobs/{jobId}") + @convenientAPI(false, "csharp") + op getResult(@path("jobId") id: string): FinalResult; + @client({ service: Azure.Csharp.Testing }) + interface PublicOperations { + @get @route("/publicDetails") op readPublicDetails(): PublicDetails; + @post @route("/jobs/{jobId}") @pollingOperation(getStatus) @finalOperation(getResult) + @useFinalStateVia("original-uri") @access(Access.internal, "csharp") + @convenientAPI(false, "csharp") + op begin(@path("jobId") id: string): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + @finalLocation @header("Location") finalLocation: ResourceLocation; + }; + } + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + deepStrictEqual(sdkContext.diagnostics, []); + strictEqual( + sdkContext.sdkPackage.models.find((model) => model.name === "Details")?.access, + "internal", + ); + strictEqual( + sdkContext.sdkPackage.models.find((model) => model.name === "NestedDetails")?.access, + "internal", + ); + strictEqual( + sdkContext.sdkPackage.models.find((model) => model.name === "BaseDetails")?.access, + "internal", + ); + strictEqual( + sdkContext.sdkPackage.models.find((model) => model.name === "PublicDetails")?.access, + "public", + ); + strictEqual( + sdkContext.sdkPackage.enums.find((type) => type.name === "State")?.access, + "internal", + ); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + strictEqual( + root.models.some((model) => model.name === "Monitor"), + false, + ); + const method = root.clients[0].methods.find((method) => method.name === "begin"); + ok(method?.kind === "lro"); + strictEqual(method.lroMetadata.statusMetadata?.[0].property.access, "internal"); + const serialized = JSON.parse(serializeCodeModel(sdkContext, root)); + const serializedMethod = serialized.clients[0].methods.find( + (method: { name: string }) => method.name === "begin", + ); + strictEqual(serializedMethod.lroMetadata.statusMetadata[0].property.access, "internal"); + strictEqual(root.models.find((model) => model.name === "Details")?.access, "internal"); + strictEqual(root.models.find((model) => model.name === "NestedDetails")?.access, "internal"); + strictEqual(root.models.find((model) => model.name === "BaseDetails")?.access, "internal"); + strictEqual(root.models.find((model) => model.name === "PublicDetails")?.access, undefined); + strictEqual(root.enums.find((type) => type.name === "State")?.access, "internal"); + }); + + it("does not apply a selection access override scoped to another emitter", async () => { + const program = await typeSpecCompile( + ` + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroResult result?: string; + @lroStatusMetadata @access(Access.internal, "python") + progress?: int32; + } + @get @route("/jobs/{jobId}/status") + op getStatus(@path("jobId") id: string): Monitor; + @post @route("/jobs") @pollingOperation(getStatus) + op begin(): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + }; + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const method = root.clients[0].methods.find((method) => method.name === "begin"); + ok(method?.kind === "lro"); + strictEqual(method.lroMetadata.statusMetadata?.[0].property.access, undefined); + }); + + it.each( + ['"ready"', '"ready" | null'].flatMap((literal) => [ + { literal, ownerAccess: true, propertyAccess: false }, + { literal, ownerAccess: false, propertyAccess: true }, + ]), + )( + "normalizes a hidden enum selection: $literal, internal owner: $ownerAccess", + async ({ literal, ownerAccess, propertyAccess }) => { + const program = await typeSpecCompile( + ` + model FinalResult { value: string; } + ${ownerAccess ? '@access(Access.internal, "csharp")' : ""} + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroStatusMetadata ${propertyAccess ? '@access(Access.internal, "csharp")' : ""} + mode?: ${literal}; + } + @get @route("/jobs/{jobId}/status") + @convenientAPI(false, "csharp") + op getStatus(@path("jobId") id: string): Monitor; + @get @route("/jobs/{jobId}") + @convenientAPI(false, "csharp") + op getResult(@path("jobId") id: string): FinalResult; + @client({ service: Azure.Csharp.Testing }) + interface PublicOperations { + @post @route("/jobs/{jobId}") @pollingOperation(getStatus) @finalOperation(getResult) + @useFinalStateVia("original-uri") @access(Access.internal, "csharp") + @convenientAPI(false, "csharp") + op begin(@path("jobId") id: string): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + @finalLocation @header("Location") finalLocation: ResourceLocation; + }; + } + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const sdkStart = sdkContext.sdkPackage.clients[0].methods.find( + (method) => method.name === "begin", + ); + ok(sdkStart?.kind === "lro"); + strictEqual( + sdkStart.lroMetadata.pollingInfo.responseModel.access, + ownerAccess ? "internal" : "public", + ); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const method = root.clients[0].methods.find((method) => method.name === "begin"); + ok(method?.kind === "lro"); + let selected = method.lroMetadata.statusMetadata?.[0].property.type; + if (selected?.kind === "nullable") selected = selected.type; + ok(selected?.kind === "enum"); + strictEqual(selected.name, "MonitorMode"); + strictEqual(selected.access, "internal"); + ok(selected.usage & UsageFlags.Output); + strictEqual(root.enums.includes(selected), true); + strictEqual( + root.models.some((model) => model.name === "Monitor"), + false, + ); + }, + ); + + it.each(["waiting", "ready"])( + "emits distinct literal enums for same-named hidden monitors: %s", + async (secondValue) => { + const program = await typeSpecCompile( + ` + namespace First { + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroStatusMetadata mode?: "ready"; + } + } + namespace Second { + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroStatusMetadata mode?: "${secondValue}"; + } + } + enum MonitorMode { Known: "known" } + model MonitorMode1 { value: string; } + model FinalResult { value: string; } + @get @route("/first/{jobId}/status") + @convenientAPI(false, "csharp") + op getFirstStatus(@path("jobId") id: string): First.Monitor; + @get @route("/second/{jobId}/status") + @convenientAPI(false, "csharp") + op getSecondStatus(@path("jobId") id: string): Second.Monitor; + @get @route("/jobs/{jobId}") + @convenientAPI(false, "csharp") + op getResult(@path("jobId") id: string): FinalResult; + @client({ service: Azure.Csharp.Testing }) + interface PublicOperations { + @get @route("/known") op readKnown(): MonitorMode; + @get @route("/known-model") op readKnownModel(): MonitorMode1; + @post @route("/first/{jobId}") @pollingOperation(getFirstStatus) + @finalOperation(getResult) @useFinalStateVia("original-uri") + @convenientAPI(false, "csharp") + op beginFirst(@path("jobId") id: string): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + @finalLocation @header("Location") finalLocation: ResourceLocation; + }; + @post @route("/second/{jobId}") @pollingOperation(getSecondStatus) + @finalOperation(getResult) @useFinalStateVia("original-uri") + @convenientAPI(false, "csharp") + op beginSecond(@path("jobId") id: string): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + @finalLocation @header("Location") finalLocation: ResourceLocation; + }; + } + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const first = root.clients[0].methods.find((method) => method.name === "beginFirst"); + const second = root.clients[0].methods.find((method) => method.name === "beginSecond"); + ok(first?.kind === "lro"); + ok(second?.kind === "lro"); + const firstEnum = first.lroMetadata.statusMetadata?.[0].property.type; + const secondEnum = second.lroMetadata.statusMetadata?.[0].property.type; + ok(firstEnum?.kind === "enum"); + ok(secondEnum?.kind === "enum"); + notStrictEqual(firstEnum, secondEnum); + deepStrictEqual( + firstEnum.values.map((value) => value.value), + ["ready"], + ); + deepStrictEqual( + secondEnum.values.map((value) => value.value), + [secondValue], + ); + strictEqual(root.enums.includes(firstEnum), true); + strictEqual(root.enums.includes(secondEnum), true); + const knownEnum = root.enums.find((type) => + type.crossLanguageDefinitionId.endsWith(".MonitorMode"), + ); + const knownModel = root.models.find((type) => type.name === "MonitorMode1"); + ok(knownEnum); + ok(knownModel); + strictEqual(knownEnum.name, "MonitorMode"); + strictEqual( + new Set([knownEnum.name, knownModel.name, firstEnum.name, secondEnum.name]).size, + 4, + ); + strictEqual( + root.models.some((model) => model.name === "Monitor"), + false, + ); + const serialized = JSON.parse(serializeCodeModel(sdkContext, root)); + for (const [methodName, enumType] of [ + ["beginFirst", firstEnum], + ["beginSecond", secondEnum], + ] as const) { + const method = serialized.clients[0].methods.find( + (candidate: { name: string }) => candidate.name === methodName, + ); + const propertyType = method.lroMetadata.statusMetadata[0].property.type; + const serializedEnum = serialized.enums[root.enums.indexOf(enumType)]; + strictEqual( + propertyType.$ref ?? propertyType.$id, + serializedEnum.$ref ?? serializedEnum.$id, + ); + } + }, + ); + + it.each([ + { + description: "client type", + monitor: "Testing", + property: "client", + client: "TestingClient", + knownType: "", + knownOperation: "", + expected: "TestingClient1", + }, + { + description: "normalized model type", + monitor: "Monitor", + property: "some_mode", + client: "PublicOperations", + knownType: "model MonitorSomeMode { value: string; }", + knownOperation: '@get @route("/known") op readKnown(): MonitorSomeMode;', + expected: "MonitorSome_mode1", + }, + { + description: "normalized enum type", + monitor: "Monitor", + property: "some_mode", + client: "PublicOperations", + knownType: 'enum MonitorSomeMode { Known: "known" }', + knownOperation: '@get @route("/known") op readKnown(): MonitorSomeMode;', + expected: "MonitorSome_mode1", + }, + { + description: "separator-normalized type", + monitor: "Monitor", + property: '@clientName("some-mode", "csharp") someMode', + client: "PublicOperations", + knownType: "model MonitorSomeMode { value: string; }", + knownOperation: '@get @route("/known") op readKnown(): MonitorSomeMode;', + expected: "MonitorSome-mode1", + }, + { + description: "Unicode-cased type", + monitor: "Monitor", + property: '@clientName("some_\u03c2_code", "csharp") someSigma', + client: "PublicOperations", + knownType: + '@clientName(Azure.ClientGenerator.Core.exact("MonitorSome\u03a3Code"), "csharp") model Known { value: string; }', + knownOperation: '@get @route("/known") op readKnown(): Known;', + expected: "MonitorSome_\u03c2_code1", + }, + { + description: "supplementary-character-normalized type", + monitor: "Monitor", + property: '@clientName("some_\u{10400}mode", "csharp") someMode', + client: "PublicOperations", + knownType: "model MonitorSomeMode { value: string; }", + knownOperation: '@get @route("/known") op readKnown(): MonitorSomeMode;', + expected: "MonitorSome_\u{10400}mode1", + }, + { + description: "request model projected to a content type", + monitor: "Monitor", + property: "someContent", + client: "PublicOperations", + knownType: "model MonitorSomeRequest { value: string; }", + knownOperation: '@get @route("/known") op readKnown(): MonitorSomeRequest;', + expected: "MonitorSomeContent1", + }, + { + description: "response model projected to a result type", + monitor: "Monitor", + property: "someResult", + client: "PublicOperations", + knownType: "model MonitorSomeResponse { value: string; }", + knownOperation: '@get @route("/known") op readKnown(): MonitorSomeResponse;', + expected: "MonitorSomeResult1", + }, + { + description: "exact response model type", + monitor: "Monitor", + property: "someResult", + client: "PublicOperations", + knownType: + '@clientName(Azure.ClientGenerator.Core.exact("MonitorSomeResponse"), "csharp") model Known { value: string; }', + knownOperation: '@get @route("/known") op readKnown(): Known;', + expected: "MonitorSomeResult", + }, + { + description: "distinct type", + monitor: "Monitor", + property: "other_mode", + client: "PublicOperations", + knownType: "model MonitorSomeMode { value: string; }", + knownOperation: '@get @route("/known") op readKnown(): MonitorSomeMode;', + expected: "MonitorOther_mode", + }, + { + description: "exact projected model type", + monitor: "Monitor", + property: "some_mode", + client: "PublicOperations", + knownType: + '@clientName(Azure.ClientGenerator.Core.exact("MonitorSomeMode"), "csharp") model Known { value: string; }', + knownOperation: '@get @route("/known") op readKnown(): Known;', + expected: "MonitorSome_mode1", + }, + { + description: "distinct exact model type", + monitor: "Monitor", + property: "some_mode", + client: "PublicOperations", + knownType: + '@clientName(Azure.ClientGenerator.Core.exact("MonitorSome_mode"), "csharp") model Known { value: string; }', + knownOperation: '@get @route("/known") op readKnown(): Known;', + expected: "MonitorSome_mode", + }, + ])( + "avoids a metadata literal enum collision with a $description", + async ({ monitor, property, client, knownType, knownOperation, expected }) => { + const program = await typeSpecCompile( + ` + ${knownType} + model FinalResult { value: string; } + model ${monitor} { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroStatusMetadata ${property}?: "ready"; + } + @get @route("/jobs/{jobId}/status") + @convenientAPI(false, "csharp") + op getStatus(@path("jobId") id: string): ${monitor}; + @get @route("/jobs/{jobId}") + @convenientAPI(false, "csharp") + op getResult(@path("jobId") id: string): FinalResult; + @client({ service: Azure.Csharp.Testing }) + interface ${client} { + ${knownOperation} + @post @route("/jobs/{jobId}") @pollingOperation(getStatus) @finalOperation(getResult) + @useFinalStateVia("original-uri") @convenientAPI(false, "csharp") + op begin(@path("jobId") id: string): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation<${monitor}>; + @finalLocation @header("Location") finalLocation: ResourceLocation; + }; + } + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + strictEqual(root.clients[0].name, client); + const method = root.clients[0].methods.find((candidate) => candidate.name === "begin"); + ok(method?.kind === "lro"); + const selected = method.lroMetadata.statusMetadata?.[0].property.type; + ok(selected?.kind === "enum"); + strictEqual(selected.name, expected); + strictEqual(root.enums.includes(selected), true); + }, + ); + + it("avoids a metadata literal enum collision with a nested client", async () => { + const program = await typeSpecCompile( + ` + model FinalResult { value: string; } + model Testing { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroStatusMetadata client?: "ready"; + } + @get @route("/jobs/{jobId}/status") + @convenientAPI(false, "csharp") + op getStatus(@path("jobId") id: string): Testing; + @get @route("/jobs/{jobId}") + @convenientAPI(false, "csharp") + op getResult(@path("jobId") id: string): FinalResult; + @post @route("/jobs/{jobId}") @pollingOperation(getStatus) @finalOperation(getResult) + @useFinalStateVia("original-uri") @convenientAPI(false, "csharp") + op begin(@path("jobId") id: string): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + @finalLocation @header("Location") finalLocation: ResourceLocation; + }; + interface TestingClient { + @get @route("/ping") op ping(): void; + } + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + ok(root.clients[0].children?.some((client) => client.name === "TestingClient")); + const method = root.clients[0].methods.find((candidate) => candidate.name === "begin"); + ok(method?.kind === "lro"); + const selected = method.lroMetadata.statusMetadata?.[0].property.type; + ok(selected?.kind === "enum"); + strictEqual(selected.name, "TestingClient1"); + }); + + it("avoids normalized-name collisions between selected literal enums", async () => { + const program = await typeSpecCompile( + ` + model FinalResult { value: string; } + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroStatusMetadata some_mode?: "ready"; + @lroStatusMetadata someMode?: "waiting"; + } + @get @route("/jobs/{jobId}/status") + @convenientAPI(false, "csharp") + op getStatus(@path("jobId") id: string): Monitor; + @get @route("/jobs/{jobId}") + @convenientAPI(false, "csharp") + op getResult(@path("jobId") id: string): FinalResult; + @client({ service: Azure.Csharp.Testing }) + interface PublicOperations { + @post @route("/jobs/{jobId}") @pollingOperation(getStatus) @finalOperation(getResult) + @useFinalStateVia("original-uri") @convenientAPI(false, "csharp") + op begin(@path("jobId") id: string): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + @finalLocation @header("Location") finalLocation: ResourceLocation; + }; + } + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const method = root.clients[0].methods.find((candidate) => candidate.name === "begin"); + ok(method?.kind === "lro"); + const selections = method.lroMetadata.statusMetadata; + ok(selections); + deepStrictEqual( + selections.map((entry) => { + const type = entry.property.type; + ok(type.kind === "enum"); + return type.name; + }), + ["MonitorSome_mode", "MonitorSomeMode1"], + ); + }); + + it.each([ + { optional: true, shareSdkType: false }, + { optional: false, shareSdkType: false }, + { optional: true, shareSdkType: true }, + ])( + "keeps a shared model's literal type, optional: $optional, shared SDK type: $shareSdkType", + async ({ optional, shareSdkType }) => { + const program = await typeSpecCompile( + ` + model Shared { value${optional ? "?" : ""}: "ready"; } + model FinalResult { value: string; } + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroStatusMetadata mode?: "ready"; + } + @get @route("/jobs/{jobId}/status") + op getStatus(@path("jobId") id: string): Monitor; + @get @route("/jobs/{jobId}") + op getResult(@path("jobId") id: string): FinalResult; + @client({ service: Azure.Csharp.Testing }) + interface PublicOperations { + @post @route("/shared") op useShared(@body input: Shared): void; + @post @route("/jobs/{jobId}") @pollingOperation(getStatus) @finalOperation(getResult) + @useFinalStateVia("original-uri") @convenientAPI(false, "csharp") + op begin(@path("jobId") id: string): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; + @finalLocation @header("Location") finalLocation: ResourceLocation; + }; + } + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + if (shareSdkType) { + const sharedModel = sdkContext.sdkPackage.models.find((model) => model.name === "Shared"); + const start = sdkContext.sdkPackage.clients[0].methods.find( + (method) => method.name === "begin", + ); + ok(sharedModel); + ok(start?.kind === "lro"); + ok(start.lroMetadata.statusMetadata?.[0]); + start.lroMetadata.statusMetadata[0].property.type = sharedModel.properties[0].type; + } + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual( + diagnostics.map((diagnostic) => diagnostic.code), + [], + ); + const method = root.clients[0].methods.find((method) => method.name === "begin"); + ok(method?.kind === "lro"); + const selected = method.lroMetadata.statusMetadata?.[0].property.type; + const shared = root.models.find((model) => model.name === "Shared")?.properties[0].type; + ok(selected?.kind === "enum"); + ok(selected.usage & UsageFlags.Output); + ok(shared); + strictEqual(shared.kind, optional ? "enum" : "constant"); + if (shared.kind === "enum") { + strictEqual(shared.name, "SharedValue"); + ok(shared.usage & UsageFlags.Input); + strictEqual(selected === shared, shareSdkType); + } + strictEqual(root.enums.includes(selected), true); + }, + ); + + it.each([ + { + decorators: '@pollingLocation @header("Operation-Location")', + expectedLocation: "ResponseHeader", + expectedName: "Operation-Location", + }, + { + decorators: "@pollingLocation", + expectedLocation: "ResponseBody", + expectedName: "status_url", + }, + { + decorators: "@pollingLocation @body", + expectedLocation: "Self", + expectedName: undefined, + }, + { + decorators: "@pollingLocation @bodyRoot", + expectedLocation: "Self", + expectedName: undefined, + }, + ])( + "preserves a polling link in $expectedLocation without changing its source property", + async ({ decorators, expectedLocation, expectedName }) => { + const program = await typeSpecCompile( + ` + namespace Tracking { + model Monitor { + @lroStatus status: "Running" | "Succeeded" | "Failed" | "Canceled"; + @lroResult result?: string; + } + } + namespace Kickoff { + model StartResponse { + @statusCode code: 202; + ${decorators} + @encodedName("application/json", "status_url") + location: ResourceLocation; + } + } + @get @route("/jobs/{jobId}/status") + op getStatus(@lroOperationId @path("jobId") id: string): Tracking.Monitor; + @put @route("/jobs/{jobId}/status") + @useFinalStateVia("original-uri") + @pollingOperation(getStatus) + op begin(@path("jobId") id: string): Kickoff.StartResponse; + `, + runner, + { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, + ); + const sdkContext = await createCSharpSdkContext(createEmitterContext(program)); + const sdkMethod = sdkContext.sdkPackage.clients[0].methods.find((m) => m.name === "begin"); + ok(sdkMethod?.kind === "lro"); + const sdkPath = sdkMethod.lroMetadata.operationId?.find((entry) => entry.kind === "path"); + ok(sdkPath?.kind === "path"); + + const [root, diagnostics] = createModel(sdkContext); + deepStrictEqual(diagnostics, []); + const method = root.clients[0].methods.find((m) => m.name === "begin"); + ok(method?.kind === "lro"); + const path = method.lroMetadata.operationId?.find((entry) => entry.kind === "path"); + ok(path?.kind === "path"); + strictEqual(path.source.location, expectedLocation); + strictEqual(path.source.property.serializedName, expectedName); + const cachedSource = sdkContext.__typeCache.properties.get(sdkPath.source.property); + ok(cachedSource); + strictEqual( + cachedSource.serializedName, + sdkPath.source.property.serializationOptions.json?.name ?? + sdkPath.source.property.serializationOptions.xml?.name ?? + sdkPath.source.property.serializationOptions.multipart?.name, + ); + if (expectedLocation === "ResponseHeader") { + notStrictEqual(path.source.property, cachedSource); + } else { + strictEqual(path.source.property, cachedSource); + } + }, + ); + it.each([undefined, "", 'Use "read" instead.\nSee C:\\sdk.'])( "preserves operation deprecation metadata: %s", async (message) => { diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ModelReaderWriterContextDefinition.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ModelReaderWriterContextDefinition.cs index c803920de00..05c4804bc42 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ModelReaderWriterContextDefinition.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ModelReaderWriterContextDefinition.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using Microsoft.TypeSpec.Generator.ClientModel.Utilities; using Microsoft.TypeSpec.Generator.Expressions; +using Microsoft.TypeSpec.Generator.Input; using Microsoft.TypeSpec.Generator.Primitives; using Microsoft.TypeSpec.Generator.Providers; using Microsoft.TypeSpec.Generator.Statements; @@ -45,6 +46,7 @@ protected override IReadOnlyList BuildAttributes() { var attributes = new Dictionary(); var customizedBuildableTypes = BuildCustomizedBuildableTypes(); + var lroMetadataTypes = CollectLroMetadataTypes(); // Add ModelReaderWriterBuildableAttribute for all IPersistableModel types (HashSet buildableTypes, HashSet buildableProviders) = CollectBuildableTypes(); @@ -69,7 +71,7 @@ protected override IReadOnlyList BuildAttributes() } foreach (var provider in buildableProviders) { - if (!IsPublicApi(provider) + if (!(IsPublicApi(provider) || IsInternalLroMetadataType(provider, lroMetadataTypes)) || !ShouldWriteProvider(provider) || customizedBuildableTypes.Contains(GetTypeIdentity(provider.Type))) { @@ -90,6 +92,52 @@ protected override IReadOnlyList BuildAttributes() return attributes.OrderBy(a => GetSimpleTypeName(a.Key)).Select(kvp => kvp.Value).ToList(); } + // LRO pollers read internal polling and selected models through this context at runtime. + private static bool IsInternalLroMetadataType(TypeProvider provider, HashSet lroMetadataTypes) + => provider.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal) + && !provider.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Private) + && lroMetadataTypes.Contains(provider.Type); + + private static HashSet CollectLroMetadataTypes() + { + var types = new HashSet(s_cSharpTypeNameComparer); + foreach (var client in ScmCodeModelGenerator.Instance.InputLibrary.InputNamespace.Clients) + { + foreach (var method in client.Methods) + { + var metadata = method switch + { + InputLongRunningServiceMethod lro => lro.LongRunningServiceMetadata, + InputLongRunningPagingServiceMethod lroPaging => lroPaging.LongRunningServiceMetadata, + _ => null + }; + if (metadata is null) + { + continue; + } + + if (metadata.PollingResponseModel is { } pollingModel + && ScmCodeModelGenerator.Instance.TypeFactory.CreateCSharpType(pollingModel) is { } pollingType) + { + types.Add(pollingType.WithNullable(false)); + } + + var properties = metadata.StatusMetadata.SelectMany(selection => selection.PropertySegments) + .Concat(metadata.OperationId.OfType() + .SelectMany(selection => selection.PropertySegments)); + foreach (var property in properties) + { + var type = ScmCodeModelGenerator.Instance.TypeFactory.CreateCSharpType(property.Type); + if (type is not null) + { + types.Add(type.GetNestedElementType().WithNullable(false)); + } + } + } + } + return types; + } + // Protected-only providers can also have the Public flag added by TypeProvider. private static bool IsPublicApi(TypeProvider provider) { diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/Definitions/ModelReaderWriterContextDefinitionTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/Definitions/ModelReaderWriterContextDefinitionTests.cs index 3b3b64c4efe..a49552ed075 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/Definitions/ModelReaderWriterContextDefinitionTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/Definitions/ModelReaderWriterContextDefinitionTests.cs @@ -87,6 +87,97 @@ public void BuildableAttributesRespectModelAccessibility(string access, int expe Assert.AreEqual(expectedCount, GetBuildableAttributes(contextDefinition).Count); } + [TestCase(false, false, false, false, false)] + [TestCase(true, false, false, false, false)] + [TestCase(false, true, false, false, false)] + [TestCase(false, false, true, false, false)] + [TestCase(false, false, false, true, false)] + [TestCase(false, false, false, false, true)] + public void InternalLroMetadataModelHasBuildableContext(bool paging, bool collection, bool operationId, bool publicModel, bool nested) + { + var details = InputFactory.Model("Details", access: "internal"); + var unrelated = InputFactory.Model("Unrelated", access: "internal"); + var exposed = InputFactory.Model("Exposed"); + var selectedType = collection ? InputFactory.Array(details.WithNullable(true)) : details.WithNullable(true); + var parent = InputFactory.Property("details", selectedType); + var selected = nested ? InputFactory.Property("value", InputPrimitiveType.String) : parent; + var segments = nested ? new[] { parent, selected } : new[] { selected }; + var operation = InputFactory.Operation("Start"); + var response = InputFactory.OperationResponse(); + var metadata = new InputLongRunningServiceMetadata(1, response, null, + operationId ? [] : [new InputLroStatusMetadata(selected, segments)], + operationId ? [new InputLroOperationIdPropertyMetadata(selected, segments)] : []); + var serviceResponse = InputFactory.ServiceMethodResponse(null, null); + InputServiceMethod method = paging + ? new InputLongRunningPagingServiceMethod("Start", "internal", [], null, null, operation, [], + serviceResponse, null, false, false, true, "", metadata, + InputFactory.PagingMetadata([], null, null)) + : new InputLongRunningServiceMethod("Start", "internal", [], null, null, operation, [], + serviceResponse, null, false, false, true, "", metadata); + + MockHelpers.LoadMockGenerator( + inputModels: () => publicModel ? [details, unrelated, exposed] : [details, unrelated], + clients: () => [InputFactory.Client("Client", methods: [method])], + createClientCore: _ => null); + + var attributes = GetBuildableAttributes(new ModelReaderWriterContextDefinition()); + var expected = publicModel + ? new[] { "typeof(global::Sample.Models.Details)", "typeof(global::Sample.Models.Exposed)" } + : new[] { "typeof(global::Sample.Models.Details)" }; + + CollectionAssert.AreEquivalent(expected, attributes.Select(attribute => attribute.Arguments[0].ToDisplayString())); + } + + [TestCase(false, false)] + [TestCase(true, true)] + public void InternalLroPollingResponseModelHasBuildableContext(bool paging, bool operationId) + { + var selected = InputFactory.Property("progress", InputPrimitiveType.Int32); + var monitor = InputFactory.Model("Monitor", access: "internal", properties: [selected]); + var unrelated = InputFactory.Model("Unrelated", access: "internal"); + var operation = InputFactory.Operation("Start"); + var metadata = new InputLongRunningServiceMetadata(1, InputFactory.OperationResponse(), null, + operationId ? [] : [new InputLroStatusMetadata(selected, [selected])], + operationId ? [new InputLroOperationIdPropertyMetadata(selected, [selected])] : [], + monitor); + var response = InputFactory.ServiceMethodResponse(null, null); + InputServiceMethod method = paging + ? new InputLongRunningPagingServiceMethod("Start", "internal", [], null, null, operation, [], + response, null, false, false, true, "", metadata, + InputFactory.PagingMetadata([], null, null)) + : new InputLongRunningServiceMethod("Start", "internal", [], null, null, operation, [], + response, null, false, false, true, "", metadata); + + MockHelpers.LoadMockGenerator( + inputModels: () => [monitor, unrelated], + clients: () => [InputFactory.Client("Client", methods: [method])], + createClientCore: _ => null); + + var attributes = GetBuildableAttributes(new ModelReaderWriterContextDefinition()); + CollectionAssert.AreEquivalent( + new[] { "typeof(global::Sample.Models.Monitor)" }, + attributes.Select(attribute => attribute.Arguments[0].ToDisplayString())); + } + + [Test] + public void PrimitiveLroMetadataDoesNotAddUnrelatedInternalModelsToContext() + { + var unrelated = InputFactory.Model("Unrelated", access: "internal"); + var selected = InputFactory.Property("progress", InputPrimitiveType.Int32); + var operation = InputFactory.Operation("Start"); + var metadata = new InputLongRunningServiceMetadata(1, InputFactory.OperationResponse(), null, + [new InputLroStatusMetadata(selected, [selected])], []); + var method = new InputLongRunningServiceMethod("Start", "internal", [], null, null, operation, [], + InputFactory.ServiceMethodResponse(null, null), null, false, false, true, "", metadata); + + MockHelpers.LoadMockGenerator( + inputModels: () => [unrelated], + clients: () => [InputFactory.Client("Client", methods: [method])], + createClientCore: _ => null); + + Assert.IsEmpty(GetBuildableAttributes(new ModelReaderWriterContextDefinition())); + } + [Test] public async Task BuildableAttributesRespectCustomizedAccessibility() { diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLongRunningServiceMetadata.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLongRunningServiceMetadata.cs index 143bac7cb69..df45845a894 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLongRunningServiceMetadata.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLongRunningServiceMetadata.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Collections.Generic; using System.Linq; namespace Microsoft.TypeSpec.Generator.Input @@ -8,10 +9,26 @@ namespace Microsoft.TypeSpec.Generator.Input public sealed class InputLongRunningServiceMetadata { public InputLongRunningServiceMetadata(int finalStateVia, InputOperationResponse finalResponse, string? resultPath) + : this(finalStateVia, finalResponse, resultPath, [], []) + { + } + + public InputLongRunningServiceMetadata(int finalStateVia, InputOperationResponse finalResponse, string? resultPath, + IReadOnlyList statusMetadata, IReadOnlyList operationId) + : this(finalStateVia, finalResponse, resultPath, statusMetadata, operationId, null) + { + } + + public InputLongRunningServiceMetadata(int finalStateVia, InputOperationResponse finalResponse, string? resultPath, + IReadOnlyList statusMetadata, IReadOnlyList operationId, + InputModelType? pollingResponseModel) { FinalStateVia = finalStateVia; FinalResponse = finalResponse; ResultPath = resultPath; + StatusMetadata = statusMetadata; + OperationId = operationId; + PollingResponseModel = pollingResponseModel; } public InputLongRunningServiceMetadata() : this(1, new InputOperationResponse(), null) { } @@ -19,6 +36,19 @@ public InputLongRunningServiceMetadata(int finalStateVia, InputOperationResponse public int FinalStateVia { get; } public InputOperationResponse FinalResponse { get; } public string? ResultPath { get; } + /// + /// The polling response model when it is otherwise emitted. Selections may be present even when this is null. + /// + public InputModelType? PollingResponseModel { get; } + /// + /// Auxiliary values from the latest polling response, in selection order. + /// + public IReadOnlyList StatusMetadata { get; } + /// + /// Server-issued ID sources in availability order: polling URL, then polling response body. + /// These selections do not replace the complete polling URL. + /// + public IReadOnlyList OperationId { get; } /// /// Meaningful return type of the long running operation. diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdMetadata.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdMetadata.cs new file mode 100644 index 00000000000..7c51aa0fb4f --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdMetadata.cs @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Microsoft.TypeSpec.Generator.Input +{ + public abstract class InputLroOperationIdMetadata + { + private protected InputLroOperationIdMetadata() { } + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdPathMetadata.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdPathMetadata.cs new file mode 100644 index 00000000000..cadf3225d5f --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdPathMetadata.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Microsoft.TypeSpec.Generator.Input +{ + public sealed class InputLroOperationIdPathMetadata : InputLroOperationIdMetadata + { + public InputLroOperationIdPathMetadata(InputOperationLink source, InputPathParameter pathParameter, string pollingPath) + { + Source = source; + PathParameter = pathParameter; + PollingPath = pollingPath; + } + + public InputOperationLink Source { get; } + public InputPathParameter PathParameter { get; } + /// + /// Modeled polling route used to locate the selected parameter in the complete polling URL. + /// + public string PollingPath { get; } + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdPropertyMetadata.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdPropertyMetadata.cs new file mode 100644 index 00000000000..4b27c482491 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdPropertyMetadata.cs @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Collections.Generic; + +namespace Microsoft.TypeSpec.Generator.Input +{ + public sealed class InputLroOperationIdPropertyMetadata : InputLroOperationIdMetadata + { + public InputLroOperationIdPropertyMetadata(InputModelProperty property, IReadOnlyList propertySegments) + { + Property = property; + PropertySegments = propertySegments; + } + + public InputModelProperty Property { get; } + public IReadOnlyList PropertySegments { get; } + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroStatusMetadata.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroStatusMetadata.cs new file mode 100644 index 00000000000..f18cd243469 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroStatusMetadata.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Collections.Generic; + +namespace Microsoft.TypeSpec.Generator.Input +{ + public sealed class InputLroStatusMetadata + { + public InputLroStatusMetadata(InputModelProperty property, IReadOnlyList propertySegments) + { + Property = property; + PropertySegments = propertySegments; + } + + public InputModelProperty Property { get; } + /// + /// Typed access path from the polling response model, not the final result. + /// + public IReadOnlyList PropertySegments { get; } + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputOperationLink.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputOperationLink.cs new file mode 100644 index 00000000000..5999d68e595 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputOperationLink.cs @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Microsoft.TypeSpec.Generator.Input +{ + public sealed class InputOperationLink + { + public InputOperationLink(InputOperationLinkLocation location, InputModelProperty property) + { + Location = location; + Property = property; + } + + public InputOperationLinkLocation Location { get; } + public InputModelProperty Property { get; } + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputOperationLinkLocation.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputOperationLinkLocation.cs new file mode 100644 index 00000000000..fa95ee8eb6b --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputOperationLinkLocation.cs @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Microsoft.TypeSpec.Generator.Input +{ + /// + /// Identifies where a polling operation link is found. + /// + public enum InputOperationLinkLocation + { + /// + /// The location is not recognized by this version of the input model. + /// + Unknown, + ResponseHeader, + ResponseBody, + Self, + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputPathParameter.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputPathParameter.cs index c3517cd9036..12744245b8b 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputPathParameter.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputPathParameter.cs @@ -30,6 +30,10 @@ public InputPathParameter( } public bool Explode { get; internal set; } + /// + /// The URI template expansion style, or when unavailable. + /// + public InputPathParameterStyle Style { get; internal set; } public bool AllowReserved { get; internal set; } public bool SkipUrlEncoding { get; internal set; } public string? ServerUrlTemplate { get; internal set; } diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputPathParameterStyle.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputPathParameterStyle.cs new file mode 100644 index 00000000000..4a9fe848511 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputPathParameterStyle.cs @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Microsoft.TypeSpec.Generator.Input +{ + /// + /// Identifies the URI template expansion style of a path parameter. + /// + public enum InputPathParameterStyle + { + /// + /// The style is missing or not recognized by this version of the input model. + /// + Unknown, + Simple, + Label, + Matrix, + Path, + Fragment, + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLongRunningServiceMetadataConverter.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLongRunningServiceMetadataConverter.cs index 4c7d6bf9d36..688f13030a7 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLongRunningServiceMetadataConverter.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLongRunningServiceMetadataConverter.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; @@ -29,19 +30,25 @@ private InputLongRunningServiceMetadata CreateOperationLongRunning(ref Utf8JsonR int finalStateVia = default; InputOperationResponse? finalResponse = null; string? resultPath = null; + InputModelType? pollingResponseModel = null; + IReadOnlyList? statusMetadata = null; + IReadOnlyList? operationId = null; while (reader.TokenType != JsonTokenType.EndObject) { var isKnownProperty = reader.TryReadInt32("finalStateVia", ref finalStateVia) || reader.TryReadComplexType("finalResponse", options, ref finalResponse) - || reader.TryReadString("resultPath", ref resultPath); + || reader.TryReadString("resultPath", ref resultPath) + || reader.TryReadComplexType("pollingResponseModel", options, ref pollingResponseModel) + || reader.TryReadComplexType("statusMetadata", options, ref statusMetadata) + || reader.TryReadComplexType("operationId", options, ref operationId); if (!isKnownProperty) { - continue; + reader.SkipProperty(); } } - var result = new InputLongRunningServiceMetadata(finalStateVia, finalResponse ?? new InputOperationResponse(), resultPath); + var result = new InputLongRunningServiceMetadata(finalStateVia, finalResponse ?? new InputOperationResponse(), resultPath, statusMetadata ?? [], operationId ?? [], pollingResponseModel); return result; } diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLroOperationIdMetadataConverter.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLroOperationIdMetadataConverter.cs new file mode 100644 index 00000000000..cbfcb7f11dd --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLroOperationIdMetadataConverter.cs @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Microsoft.TypeSpec.Generator.Input +{ + internal sealed class InputLroOperationIdMetadataConverter : JsonConverter + { + public override InputLroOperationIdMetadata Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.StartObject) + { + reader.Read(); + } + + string? kind = null; + InputOperationLink? source = null; + InputPathParameter? pathParameter = null; + string? pollingPath = null; + InputModelProperty? property = null; + IReadOnlyList? propertySegments = null; + while (reader.TokenType != JsonTokenType.EndObject) + { + if (!reader.TryReadString("kind", ref kind) + && !reader.TryReadComplexType("source", options, ref source) + && !reader.TryReadComplexType("pathParameter", options, ref pathParameter) + && !reader.TryReadString("pollingPath", ref pollingPath) + && !reader.TryReadComplexType("property", options, ref property) + && !reader.TryReadComplexType("propertySegments", options, ref propertySegments)) + { + reader.SkipProperty(); + } + } + + return kind switch + { + "path" => new InputLroOperationIdPathMetadata( + source ?? throw new JsonException("Path operation ID must have a source."), + pathParameter ?? throw new JsonException("Path operation ID must have a path parameter."), + pollingPath ?? throw new JsonException("Path operation ID must have a polling path.")), + "property" => new InputLroOperationIdPropertyMetadata( + property ?? throw new JsonException("Property operation ID must have a property."), + propertySegments ?? throw new JsonException("Property operation ID must have property segments.")), + _ => throw new JsonException($"Unknown LRO operation ID kind: '{kind}'.") + }; + } + + public override void Write(Utf8JsonWriter writer, InputLroOperationIdMetadata value, JsonSerializerOptions options) + => throw new NotSupportedException("Writing not supported"); + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLroStatusMetadataConverter.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLroStatusMetadataConverter.cs new file mode 100644 index 00000000000..f84a416a620 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLroStatusMetadataConverter.cs @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Microsoft.TypeSpec.Generator.Input +{ + internal sealed class InputLroStatusMetadataConverter : JsonConverter + { + public override InputLroStatusMetadata Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.StartObject) + { + reader.Read(); + } + + InputModelProperty? property = null; + IReadOnlyList? propertySegments = null; + while (reader.TokenType != JsonTokenType.EndObject) + { + if (!reader.TryReadComplexType("property", options, ref property) + && !reader.TryReadComplexType("propertySegments", options, ref propertySegments)) + { + reader.SkipProperty(); + } + } + + return new InputLroStatusMetadata( + property ?? throw new JsonException("LRO status metadata must have a property."), + propertySegments ?? throw new JsonException("LRO status metadata must have property segments.")); + } + + public override void Write(Utf8JsonWriter writer, InputLroStatusMetadata value, JsonSerializerOptions options) + => throw new NotSupportedException("Writing not supported"); + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputOperationLinkConverter.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputOperationLinkConverter.cs new file mode 100644 index 00000000000..fb2aa3e820b --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputOperationLinkConverter.cs @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Microsoft.TypeSpec.Generator.Input +{ + internal sealed class InputOperationLinkConverter : JsonConverter + { + public override InputOperationLink Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.StartObject) + { + reader.Read(); + } + + string? location = null; + InputModelProperty? property = null; + while (reader.TokenType != JsonTokenType.EndObject) + { + if (!reader.TryReadString("location", ref location) + && !reader.TryReadComplexType("property", options, ref property)) + { + reader.SkipProperty(); + } + } + + if (location is null) + { + throw new JsonException("Operation link must have a location."); + } + var parsedLocation = Enum.TryParse(location, ignoreCase: true, out var value) && Enum.IsDefined(value) + ? value + : InputOperationLinkLocation.Unknown; + + return new InputOperationLink(parsedLocation, + property ?? throw new JsonException("Operation link must have a property.")); + } + + public override void Write(Utf8JsonWriter writer, InputOperationLink value, JsonSerializerOptions options) + => throw new NotSupportedException("Writing not supported"); + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputPathParameterConverter.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputPathParameterConverter.cs index ab6e4c0956b..3f01656a510 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputPathParameterConverter.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputPathParameterConverter.cs @@ -64,6 +64,7 @@ internal static InputPathParameter ReadInputPathParameter(ref Utf8JsonReader rea bool isReadOnly = false; bool isOptional = false; string? access = null; + string? style = null; string? serverUrlTemplate = null; bool allowReserved = false; IReadOnlyList? decorators = null; @@ -88,6 +89,7 @@ internal static InputPathParameter ReadInputPathParameter(ref Utf8JsonReader rea || reader.TryReadComplexType("defaultValue", options, ref defaultValue) || reader.TryReadString("scope", ref scope) || reader.TryReadBoolean("explode", ref explode) + || reader.TryReadString("style", ref style) || reader.TryReadBoolean("skipUrlEncoding", ref skipUrlEncoding) || reader.TryReadComplexType("decorators", options, ref decorators) || reader.TryReadComplexType("methodParameterSegments", options, ref methodParameterSegments) @@ -115,6 +117,9 @@ internal static InputPathParameter ReadInputPathParameter(ref Utf8JsonReader rea parameter.DefaultValue = defaultValue; parameter.Scope = InputParameter.ParseScope(type, name, scope); parameter.Explode = explode; + parameter.Style = Enum.TryParse(style, ignoreCase: true, out var parsedStyle) && Enum.IsDefined(parsedStyle) + ? parsedStyle + : InputPathParameterStyle.Unknown; parameter.SkipUrlEncoding = skipUrlEncoding; parameter.MethodParameterSegments = methodParameterSegments; parameter.IsExactName = isExactName; diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/TypeSpecSerialization.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/TypeSpecSerialization.cs index 37ff8f1cf97..101f74f7772 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/TypeSpecSerialization.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/TypeSpecSerialization.cs @@ -63,6 +63,9 @@ public static class TypeSpecSerialization new InputPagingServiceMetadataConverter(), new InputLongRunningServiceMethodConverter(referenceHandler), new InputLongRunningServiceMetadataConverter(), + new InputLroStatusMetadataConverter(), + new InputLroOperationIdMetadataConverter(), + new InputOperationLinkConverter(), new InputLongRunningPagingServiceMethodConverter(referenceHandler), new InputServiceMethodResponseConverter(), new InputPropertyConverter(referenceHandler), diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/InputServiceMethodTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/InputServiceMethodTests.cs index d994b3342ba..455f4f8ef98 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/InputServiceMethodTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/InputServiceMethodTests.cs @@ -1,6 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.IO; +using System.Linq; +using System.Text.Json; using Microsoft.TypeSpec.Generator.Tests.Common; using NUnit.Framework; @@ -8,6 +11,401 @@ namespace Microsoft.TypeSpec.Generator.Input.Tests { public class InputServiceMethodTests { + [Test] + public void LoadsEmittedLroMetadata() + { + var directory = Helpers.GetAssetFileOrDirectoryPath(false); + var input = TypeSpecSerialization.Deserialize(File.ReadAllText(Path.Combine(directory, "tspCodeModel.json")))!; + var methods = input.Clients.Single().Methods; + var polling = methods.Single(m => m.Name == "getStatus").Operation; + var metadata = ((InputLongRunningServiceMethod)methods.Single(m => m.Name == "begin")) + .LongRunningServiceMetadata; + var monitor = input.Models.Single(m => m.Name == "Monitor"); + var baseMonitor = input.Models.Single(m => m.Name == "BaseMonitor"); + var statistics = input.Models.Single(m => m.Name == "Statistics"); + + Assert.AreSame(monitor, metadata.PollingResponseModel); + Assert.AreSame(baseMonitor, monitor.BaseModel); + CollectionAssert.AreEqual(new[] { "progress", "statistics", "updatedAt" }, + metadata.StatusMetadata.Select(selection => selection.Property.Name)); + CollectionAssert.AreEqual(new[] { "percent_complete", "statistics", "last_updated" }, + metadata.StatusMetadata.Select(selection => selection.PropertySegments.Single().SerializedName)); + Assert.AreSame(monitor.Properties.Single(p => p.Name == "progress"), metadata.StatusMetadata[0].Property); + Assert.AreEqual("internal", metadata.StatusMetadata[0].Property.Access); + Assert.AreSame(monitor.Properties.Single(p => p.Name == "statistics"), metadata.StatusMetadata[1].Property); + Assert.AreSame(statistics, metadata.StatusMetadata[1].Property.Type); + Assert.AreSame(baseMonitor.Properties.Single(p => p.Name == "updatedAt"), metadata.StatusMetadata[2].Property); + foreach (var selection in metadata.StatusMetadata) + { + Assert.AreSame(selection.Property, selection.PropertySegments.Single()); + } + + Assert.AreEqual(2, metadata.OperationId.Count); + var path = (InputLroOperationIdPathMetadata)metadata.OperationId[0]; + var property = (InputLroOperationIdPropertyMetadata)metadata.OperationId[1]; + Assert.AreSame(polling.Parameters.Single(p => p.Name == "id"), path.PathParameter); + Assert.AreEqual("jobId", path.PathParameter.SerializedName); + Assert.AreEqual("/jobs/{jobId}/status", path.PollingPath); + Assert.AreEqual(InputOperationLinkLocation.ResponseHeader, path.Source.Location); + Assert.AreEqual("Operation-Location", path.Source.Property.SerializedName); + Assert.AreSame(monitor.Properties.Single(p => p.Name == "jobId"), property.Property); + Assert.AreEqual("job_id", property.Property.SerializedName); + Assert.AreSame(property.Property, property.PropertySegments.Single()); + } + + [TestCase("simple", InputPathParameterStyle.Simple)] + [TestCase("label", InputPathParameterStyle.Label)] + [TestCase("matrix", InputPathParameterStyle.Matrix)] + [TestCase("path", InputPathParameterStyle.Path)] + [TestCase("fragment", InputPathParameterStyle.Fragment)] + [TestCase("future-style", InputPathParameterStyle.Unknown)] + [TestCase(null, InputPathParameterStyle.Unknown)] + public void LoadsPollingIdPathStyle(string? style, InputPathParameterStyle expected) + { + var directory = Helpers.GetAssetFileOrDirectoryPath(false, method: nameof(LoadsEmittedLroMetadata)); + var json = File.ReadAllText(Path.Combine(directory, "tspCodeModel.json")); + json = json.Replace("\"style\": \"simple\",", style == null ? "" : $"\"style\": \"{style}\","); + var input = TypeSpecSerialization.Deserialize(json)!; + var methods = input.Clients.Single().Methods; + var polling = methods.Single(m => m.Name == "getStatus").Operation; + var metadata = ((InputLongRunningServiceMethod)methods.Single(m => m.Name == "begin")) + .LongRunningServiceMetadata; + var selection = (InputLroOperationIdPathMetadata)metadata.OperationId[0]; + Assert.AreSame(polling.Parameters.Single(p => p.Name == "id"), selection.PathParameter); + Assert.AreEqual(expected, selection.PathParameter.Style); + } + + [TestCase("lro")] + [TestCase("lropaging")] + public void LoadsLongRunningMetadataWithTypedPathsAndOrderedIdSources(string methodKind) + { + var json = """ + { + "finalStateVia": 1, + "statusMetadata": [{ + "property": { + "$id": "progress", "name": "progress", "serializedName": "percent_complete", "access": "internal", + "optional": true, "type": { "kind": "int32" } + }, + "propertySegments": [{ "$ref": "progress" }] + }, { + "property": { + "$id": "updated", "name": "updatedAt", "serializedName": "last_updated", + "optional": true, "type": { "kind": "string" } + }, + "propertySegments": [{ "$ref": "progress" }, { "$ref": "updated" }] + }], + "operationId": [{ + "kind": "path", + "source": { + "location": "ResponseHeader", + "property": { + "$id": "location", "name": "location", "serializedName": "Operation-Location", + "type": { "kind": "string" } + } + }, + "pathParameter": { + "$id": "parameter", "kind": "path", "name": "id", "serializedName": "jobId", + "type": { "kind": "string" }, "scope": "method" + }, + "pollingPath": "/jobs/{jobId}/status" + }, { + "kind": "property", + "property": { + "$id": "id", "name": "jobId", "serializedName": "job_id", + "type": { "kind": "string" } + }, + "propertySegments": [{ "$ref": "id" }] + }] + } + """; + var referenceHandler = new TypeSpecReferenceHandler(); + var options = new JsonSerializerOptions + { + Converters = + { + new InputLongRunningServiceMetadataConverter(), + new InputLroStatusMetadataConverter(), + new InputLroOperationIdMetadataConverter(), + new InputOperationLinkConverter(), + new InputModelPropertyConverter(referenceHandler), + new InputTypeConverter(referenceHandler), + new InputPrimitiveTypeConverter(referenceHandler), + new InputPathParameterConverter(referenceHandler), + new System.Text.Json.Serialization.JsonStringEnumConverter(JsonNamingPolicy.CamelCase), + } + }; + var metadata = JsonSerializer.Deserialize(json, options)!; + + Assert.AreEqual(2, metadata.StatusMetadata.Count); + Assert.AreEqual("progress", metadata.StatusMetadata[0].Property.Name); + Assert.AreEqual("internal", metadata.StatusMetadata[0].Property.Access); + Assert.IsFalse(metadata.StatusMetadata[0].Property.IsRequired); + Assert.AreEqual("percent_complete", metadata.StatusMetadata[0].Property.SerializedName); + Assert.AreSame(metadata.StatusMetadata[0].Property, metadata.StatusMetadata[0].PropertySegments[0]); + Assert.AreSame(metadata.StatusMetadata[0].Property, metadata.StatusMetadata[1].PropertySegments[0]); + Assert.AreSame(metadata.StatusMetadata[1].Property, metadata.StatusMetadata[1].PropertySegments[1]); + Assert.AreEqual(2, metadata.OperationId.Count); + var path = (InputLroOperationIdPathMetadata)metadata.OperationId[0]; + Assert.AreEqual(InputOperationLinkLocation.ResponseHeader, path.Source.Location); + Assert.AreEqual("Operation-Location", path.Source.Property.SerializedName); + Assert.AreEqual("jobId", path.PathParameter.SerializedName); + Assert.AreEqual("/jobs/{jobId}/status", path.PollingPath); + var property = (InputLroOperationIdPropertyMetadata)metadata.OperationId[1]; + Assert.AreEqual("jobId", property.Property.Name); + Assert.AreEqual("job_id", property.Property.SerializedName); + Assert.AreSame(property.Property, property.PropertySegments[0]); + + var input = TypeSpecSerialization.Deserialize($$""" + { + "name": "Test", + "models": [{ + "$id": "monitor", "name": "Monitor", + "properties": [{ "$ref": "progress" }, { "$ref": "updated" }, { "$ref": "id" }] + }], + "clients": [{ + "$id": "client", "name": "TestClient", + "methods": [{ + "$id": "begin", "kind": "{{methodKind}}", "name": "begin", + "crossLanguageDefinitionId": "Test.begin", + "operation": { + "$id": "operation", "name": "begin", "httpMethod": "POST", + "uri": "https://example.com", "path": "/jobs", + "crossLanguageDefinitionId": "Test.begin" + }, + "response": {}, + "pagingMetadata": { "itemPropertySegments": ["items"] }, + "lroMetadata": {{json}} + }] + }] + } + """)!; + var method = input.Clients.Single().Methods.Single(); + var registeredMetadata = method is InputLongRunningServiceMethod lro + ? lro.LongRunningServiceMetadata + : ((InputLongRunningPagingServiceMethod)method).LongRunningServiceMetadata; + Assert.AreSame(input.Models[0].Properties[0], registeredMetadata.StatusMetadata[0].Property); + Assert.AreSame(input.Models[0].Properties[1], registeredMetadata.StatusMetadata[1].Property); + Assert.AreSame(input.Models[0].Properties[2], + ((InputLroOperationIdPropertyMetadata)registeredMetadata.OperationId[1]).Property); + } + + [TestCase(true, true)] + [TestCase(false, true)] + [TestCase(true, false)] + [TestCase(false, false)] + public void ResolvesInheritedStatusAndPollingOperationReferences(bool pollingFirst, bool modelsFirst) + { + const string parameter = """ + { + "$id": "jobId", "kind": "path", "name": "id", "serializedName": "jobId", + "type": { "kind": "string" }, "scope": "method" + } + """; + const string reference = """{ "$ref": "jobId" }"""; + var pollingMethod = $$""" + { + "$id": "poll", "kind": "basic", "name": "getStatus", + "crossLanguageDefinitionId": "Test.getStatus", + "operation": { + "$id": "polling", "name": "getStatus", "httpMethod": "GET", + "uri": "https://example.com", "path": "/jobs/{jobId}/status", + "crossLanguageDefinitionId": "Test.getStatus", + "parameters": [{{(pollingFirst ? parameter : reference)}}] + }, + "response": {} + } + """; + var beginMethod = $$""" + { + "$id": "begin", "kind": "lro", "name": "begin", + "crossLanguageDefinitionId": "Test.begin", + "operation": { + "$id": "start", "name": "begin", "httpMethod": "POST", + "uri": "https://example.com", "path": "/jobs", + "crossLanguageDefinitionId": "Test.begin" + }, + "response": {}, + "lroMetadata": { + "finalStateVia": 1, + "pollingResponseModel": { "$ref": "monitor" }, + "statusMetadata": [{ + "property": { "$ref": "inherited" }, + "propertySegments": [{ "$ref": "inherited" }] + }], + "operationId": [{ + "kind": "path", + "source": { "location": "ResponseBody", "property": { "$ref": "link" } }, + "pathParameter": {{(pollingFirst ? reference : parameter)}}, + "pollingPath": "/jobs/{jobId}/status" + }] + } + } + """; + var methodsJson = pollingFirst + ? $"{pollingMethod}, {beginMethod}" + : $"{beginMethod}, {pollingMethod}"; + const string modelsJson = """ + "models": [{ + "$id": "base", "name": "BaseMonitor", + "properties": [{ + "$id": "inherited", "name": "updated", "serializedName": "updated_at", + "type": { "kind": "string" } + }] + }, { + "$id": "monitor", "name": "Monitor", "baseModel": { "$ref": "base" }, + "properties": [] + }, { + "$id": "startResponse", "name": "StartResponse", + "properties": [{ + "$id": "link", "name": "location", "serializedName": "location", + "type": { "kind": "string" } + }] + }] + """; + var clientsJson = $$""" + "clients": [{ + "$id": "client", "name": "TestClient", + "methods": [{{methodsJson}}] + }] + """; + var input = TypeSpecSerialization.Deserialize($$""" + { + "name": "Test", + {{(modelsFirst ? $"{modelsJson}, {clientsJson}" : $"{clientsJson}, {modelsJson}")}} + } + """)!; + + var methods = input.Clients.Single().Methods; + var pollingOperation = methods.Single(m => m.Name == "getStatus").Operation; + var metadata = ((InputLongRunningServiceMethod)methods.Single(m => m.Name == "begin")) + .LongRunningServiceMetadata; + var path = (InputLroOperationIdPathMetadata)metadata.OperationId.Single(); + var baseModel = input.Models.Single(m => m.Name == "BaseMonitor"); + var monitor = input.Models.Single(m => m.Name == "Monitor"); + var startResponse = input.Models.Single(m => m.Name == "StartResponse"); + + Assert.AreSame(baseModel, monitor.BaseModel); + Assert.AreSame(monitor, metadata.PollingResponseModel); + Assert.AreSame(baseModel.Properties[0], metadata.StatusMetadata[0].Property); + Assert.AreSame(baseModel.Properties[0], metadata.StatusMetadata[0].PropertySegments[0]); + Assert.AreSame(startResponse.Properties[0], path.Source.Property); + Assert.AreSame(pollingOperation.Parameters.Single(), path.PathParameter); + } + + [TestCase("ResponseHeader", InputOperationLinkLocation.ResponseHeader)] + [TestCase("ResponseBody", InputOperationLinkLocation.ResponseBody)] + [TestCase("Self", InputOperationLinkLocation.Self)] + [TestCase("FutureLocation", InputOperationLinkLocation.Unknown)] + public void LoadsOperationLinkLocations(string location, InputOperationLinkLocation expected) + { + var referenceHandler = new TypeSpecReferenceHandler(); + var options = new JsonSerializerOptions + { + Converters = + { + new InputOperationLinkConverter(), + new InputModelPropertyConverter(referenceHandler), + new InputTypeConverter(referenceHandler), + new InputPrimitiveTypeConverter(referenceHandler), + } + }; + var link = JsonSerializer.Deserialize($$""" + { + "location": "{{location}}", + "property": { "$id": "link", "name": "link", "type": { "kind": "string" } } + } + """, options)!; + + Assert.AreEqual(expected, link.Location); + } + + [TestCase("""{}""")] + [TestCase("""{ "location": null }""")] + public void RejectsOperationLinkWithoutLocation(string json) + { + var options = new JsonSerializerOptions + { + Converters = { new InputOperationLinkConverter() } + }; + Assert.Throws(() => JsonSerializer.Deserialize(json, options)); + } + + [TestCase("""{"kind":"future"}""")] + [TestCase("""{"kind":"path"}""")] + [TestCase("""{"kind":"property","propertySegments":[]}""")] + public void RejectsInvalidOperationIdMetadata(string json) + { + var options = new JsonSerializerOptions + { + Converters = { new InputLroOperationIdMetadataConverter() } + }; + Assert.Throws(() => JsonSerializer.Deserialize(json, options)); + } + + [Test] + public void LoadsLegacyLongRunningMetadataWithoutSelections() + { + var options = new JsonSerializerOptions + { + Converters = { new InputLongRunningServiceMetadataConverter() } + }; + var metadata = JsonSerializer.Deserialize( + """{ "futureMetadata": { "values": [1, 2] }, "finalStateVia": 2, "resultPath": "result" }""", options)!; + + Assert.AreEqual(2, metadata.FinalStateVia); + Assert.AreEqual("result", metadata.ResultPath); + Assert.IsEmpty(metadata.StatusMetadata); + Assert.IsEmpty(metadata.OperationId); + Assert.IsNull(metadata.PollingResponseModel); + Assert.IsEmpty(new InputLongRunningServiceMetadata().StatusMetadata); + } + + [Test] + public void LoadsSelectedModelPropertyWithoutPollingResponseModel() + { + var input = TypeSpecSerialization.Deserialize(""" + { + "name": "Test", + "models": [{ + "$id": "detailsModel", "name": "Details", + "properties": [{ "$id": "value", "name": "value", "type": { "kind": "string" } }] + }], + "clients": [{ + "$id": "client", "name": "TestClient", + "methods": [{ + "$id": "begin", "kind": "lro", "name": "begin", + "crossLanguageDefinitionId": "Test.begin", + "operation": { + "$id": "operation", "name": "begin", "httpMethod": "POST", + "uri": "https://example.com", "path": "/jobs", + "crossLanguageDefinitionId": "Test.begin" + }, + "response": {}, + "lroMetadata": { + "finalStateVia": 1, + "statusMetadata": [{ + "property": { + "$id": "details", "name": "details", "serializedName": "details", + "optional": true, "type": { "$ref": "detailsModel" } + }, + "propertySegments": [{ "$ref": "details" }] + }] + } + }] + }] + } + """)!; + var method = (InputLongRunningServiceMethod)input.Clients.Single().Methods.Single(); + var metadata = method.LongRunningServiceMetadata; + var selection = metadata.StatusMetadata.Single(); + + Assert.IsNull(metadata.PollingResponseModel); + Assert.IsNull(selection.Property.EnclosingType); + Assert.AreSame(selection.Property, selection.PropertySegments.Single()); + Assert.AreSame(input.Models.Single(), selection.Property.Type); + } + [Test] public void CanUpdateServiceMethodName() { diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/tspCodeModel.json b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/tspCodeModel.json new file mode 100644 index 00000000000..40c85c1c721 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/tspCodeModel.json @@ -0,0 +1,714 @@ +{ + "name": "LroFixture", + "apiVersions": [], + "enums": [ + { + "$id": "1", + "kind": "enum", + "name": "MonitorStatus", + "apiVersions": [], + "crossLanguageDefinitionId": "LroFixture.Monitor.status.anonymous", + "valueType": { + "$id": "2", + "kind": "string", + "name": "string", + "crossLanguageDefinitionId": "TypeSpec.string", + "decorators": [] + }, + "values": [ + { + "$id": "3", + "kind": "enumvalue", + "name": "Running", + "value": "Running", + "valueType": { + "$ref": "2" + }, + "enumType": { + "$ref": "1" + }, + "decorators": [], + "isExactName": false + }, + { + "$id": "4", + "kind": "enumvalue", + "name": "Succeeded", + "value": "Succeeded", + "valueType": { + "$ref": "2" + }, + "enumType": { + "$ref": "1" + }, + "decorators": [], + "isExactName": false + }, + { + "$id": "5", + "kind": "enumvalue", + "name": "Failed", + "value": "Failed", + "valueType": { + "$ref": "2" + }, + "enumType": { + "$ref": "1" + }, + "decorators": [], + "isExactName": false + }, + { + "$id": "6", + "kind": "enumvalue", + "name": "Canceled", + "value": "Canceled", + "valueType": { + "$ref": "2" + }, + "enumType": { + "$ref": "1" + }, + "decorators": [], + "isExactName": false + } + ], + "namespace": "LroFixture", + "isFixed": true, + "isFlags": false, + "usage": "Output,Json,LroPolling,LroFinalEnvelope", + "decorators": [], + "isExactName": false + } + ], + "constants": [ + { + "$id": "7", + "kind": "constant", + "name": "GetStatusAccept", + "namespace": "", + "usage": "None", + "valueType": { + "$id": "8", + "kind": "string", + "name": "string", + "crossLanguageDefinitionId": "TypeSpec.string", + "decorators": [] + }, + "value": "application/json", + "decorators": [], + "isExactName": false + } + ], + "models": [ + { + "$id": "9", + "kind": "model", + "name": "Monitor", + "apiVersions": [], + "namespace": "LroFixture", + "crossLanguageDefinitionId": "LroFixture.Monitor", + "usage": "Output,Json,LroPolling,LroFinalEnvelope", + "decorators": [], + "serializationOptions": { + "json": { + "name": "Monitor" + } + }, + "isExactName": false, + "baseModel": { + "$id": "10", + "kind": "model", + "name": "BaseMonitor", + "apiVersions": [], + "namespace": "LroFixture", + "crossLanguageDefinitionId": "LroFixture.BaseMonitor", + "usage": "Output,Json,LroPolling,LroFinalEnvelope", + "decorators": [], + "serializationOptions": { + "json": { + "name": "BaseMonitor" + } + }, + "isExactName": false, + "properties": [ + { + "$id": "11", + "kind": "property", + "name": "updatedAt", + "originalName": "updated", + "apiVersions": [], + "serializedName": "last_updated", + "type": { + "$id": "12", + "kind": "utcDateTime", + "name": "utcDateTime", + "encode": "rfc3339", + "wireType": { + "$id": "13", + "kind": "string", + "name": "string", + "crossLanguageDefinitionId": "TypeSpec.string", + "decorators": [] + }, + "crossLanguageDefinitionId": "TypeSpec.utcDateTime", + "decorators": [] + }, + "optional": true, + "readOnly": false, + "discriminator": false, + "flatten": false, + "decorators": [], + "crossLanguageDefinitionId": "LroFixture.BaseMonitor.updated", + "serializationOptions": { + "json": { + "name": "last_updated" + } + }, + "isHttpMetadata": false, + "isExactName": false + } + ] + }, + "properties": [ + { + "$id": "14", + "kind": "property", + "name": "status", + "apiVersions": [], + "serializedName": "status", + "type": { + "$ref": "1" + }, + "optional": false, + "readOnly": false, + "discriminator": false, + "flatten": false, + "decorators": [], + "crossLanguageDefinitionId": "LroFixture.Monitor.status", + "serializationOptions": { + "json": { + "name": "status" + } + }, + "isHttpMetadata": false, + "isExactName": false + }, + { + "$id": "15", + "kind": "property", + "name": "result", + "apiVersions": [], + "serializedName": "result", + "type": { + "$id": "16", + "kind": "string", + "name": "string", + "crossLanguageDefinitionId": "TypeSpec.string", + "decorators": [] + }, + "optional": true, + "readOnly": false, + "discriminator": false, + "flatten": false, + "decorators": [], + "crossLanguageDefinitionId": "LroFixture.Monitor.result", + "serializationOptions": { + "json": { + "name": "result" + } + }, + "isHttpMetadata": false, + "isExactName": false + }, + { + "$id": "17", + "kind": "property", + "name": "jobId", + "originalName": "id", + "apiVersions": [], + "serializedName": "job_id", + "type": { + "$id": "18", + "kind": "string", + "name": "string", + "crossLanguageDefinitionId": "TypeSpec.string", + "decorators": [] + }, + "optional": false, + "readOnly": false, + "discriminator": false, + "flatten": false, + "decorators": [], + "crossLanguageDefinitionId": "LroFixture.Monitor.id", + "serializationOptions": { + "json": { + "name": "job_id" + } + }, + "isHttpMetadata": false, + "isExactName": false + }, + { + "$id": "19", + "kind": "property", + "name": "progress", + "originalName": "percent", + "apiVersions": [], + "serializedName": "percent_complete", + "type": { + "$id": "20", + "kind": "int32", + "name": "int32", + "crossLanguageDefinitionId": "TypeSpec.int32", + "decorators": [] + }, + "optional": true, + "readOnly": false, + "access": "internal", + "discriminator": false, + "flatten": false, + "decorators": [], + "crossLanguageDefinitionId": "LroFixture.Monitor.percent", + "serializationOptions": { + "json": { + "name": "percent_complete" + } + }, + "isHttpMetadata": false, + "isExactName": false + }, + { + "$id": "21", + "kind": "property", + "name": "statistics", + "apiVersions": [], + "serializedName": "statistics", + "type": { + "$id": "22", + "kind": "model", + "name": "Statistics", + "apiVersions": [], + "namespace": "LroFixture", + "crossLanguageDefinitionId": "LroFixture.Statistics", + "usage": "Output,Json,LroPolling,LroFinalEnvelope", + "decorators": [], + "serializationOptions": { + "json": { + "name": "Statistics" + } + }, + "isExactName": false, + "properties": [ + { + "$id": "23", + "kind": "property", + "name": "completed", + "apiVersions": [], + "serializedName": "completed", + "type": { + "$id": "24", + "kind": "int32", + "name": "int32", + "crossLanguageDefinitionId": "TypeSpec.int32", + "decorators": [] + }, + "optional": false, + "readOnly": false, + "discriminator": false, + "flatten": false, + "decorators": [], + "crossLanguageDefinitionId": "LroFixture.Statistics.completed", + "serializationOptions": { + "json": { + "name": "completed" + } + }, + "isHttpMetadata": false, + "isExactName": false + } + ] + }, + "optional": true, + "readOnly": false, + "discriminator": false, + "flatten": false, + "decorators": [], + "crossLanguageDefinitionId": "LroFixture.Monitor.statistics", + "serializationOptions": { + "json": { + "name": "statistics" + } + }, + "isHttpMetadata": false, + "isExactName": false + } + ] + }, + { + "$ref": "22" + }, + { + "$ref": "10" + } + ], + "clients": [ + { + "$id": "25", + "kind": "client", + "name": "LroFixtureClient", + "isExactName": false, + "namespace": "LroFixture", + "methods": [ + { + "$id": "26", + "kind": "basic", + "name": "getStatus", + "isExactName": false, + "accessibility": "public", + "apiVersions": [], + "operation": { + "$id": "27", + "name": "getStatus", + "isExactName": false, + "accessibility": "public", + "parameters": [ + { + "$id": "28", + "kind": "path", + "name": "id", + "serializedName": "jobId", + "type": { + "$id": "29", + "kind": "string", + "name": "string", + "crossLanguageDefinitionId": "TypeSpec.string", + "decorators": [] + }, + "isApiVersion": false, + "explode": false, + "style": "simple", + "allowReserved": false, + "skipUrlEncoding": false, + "optional": false, + "scope": "Method", + "decorators": [], + "readOnly": false, + "crossLanguageDefinitionId": "LroFixture.getStatus.id", + "methodParameterSegments": [ + { + "$id": "30", + "kind": "method", + "name": "id", + "serializedName": "jobId", + "type": { + "$id": "31", + "kind": "string", + "name": "string", + "crossLanguageDefinitionId": "TypeSpec.string", + "decorators": [] + }, + "location": "Path", + "isApiVersion": false, + "optional": false, + "scope": "Method", + "crossLanguageDefinitionId": "LroFixture.getStatus.id", + "readOnly": false, + "access": "public", + "decorators": [], + "isExactName": false + } + ], + "isExactName": false + }, + { + "$id": "32", + "kind": "header", + "name": "accept", + "serializedName": "Accept", + "type": { + "$ref": "7" + }, + "isApiVersion": false, + "optional": false, + "isContentType": false, + "scope": "Constant", + "readOnly": false, + "decorators": [], + "crossLanguageDefinitionId": "LroFixture.getStatus.accept", + "methodParameterSegments": [ + { + "$id": "33", + "kind": "method", + "name": "accept", + "serializedName": "Accept", + "type": { + "$ref": "7" + }, + "location": "Header", + "isApiVersion": false, + "optional": false, + "scope": "Constant", + "crossLanguageDefinitionId": "LroFixture.getStatus.accept", + "readOnly": false, + "access": "public", + "decorators": [], + "isExactName": false + } + ], + "isExactName": false + } + ], + "responses": [ + { + "statusCodes": [ + 200 + ], + "bodyType": { + "$ref": "9" + }, + "headers": [], + "isErrorResponse": false, + "contentTypes": [ + "application/json" + ], + "serializationOptions": { + "json": { + "name": "" + } + } + } + ], + "httpMethod": "GET", + "uri": "{endpoint}", + "path": "/jobs/{jobId}/status", + "bufferResponse": true, + "generateProtocolMethod": true, + "generateConvenienceMethod": true, + "crossLanguageDefinitionId": "LroFixture.getStatus", + "decorators": [], + "namespace": "LroFixture" + }, + "parameters": [ + { + "$ref": "30" + }, + { + "$ref": "33" + } + ], + "response": { + "type": { + "$ref": "9" + } + }, + "isOverride": false, + "generateConvenient": true, + "generateProtocol": true, + "crossLanguageDefinitionId": "LroFixture.getStatus" + }, + { + "$id": "34", + "kind": "lro", + "name": "begin", + "isExactName": false, + "accessibility": "internal", + "apiVersions": [], + "operation": { + "$id": "35", + "name": "begin", + "isExactName": false, + "accessibility": "internal", + "parameters": [], + "responses": [ + { + "statusCodes": [ + 202 + ], + "headers": [ + { + "name": "location", + "nameInResponse": "Operation-Location", + "type": { + "$id": "36", + "kind": "url", + "name": "ResourceLocation", + "crossLanguageDefinitionId": "TypeSpec.Rest.ResourceLocation", + "baseType": { + "$id": "37", + "kind": "url", + "name": "url", + "crossLanguageDefinitionId": "TypeSpec.url", + "decorators": [] + }, + "decorators": [] + } + } + ], + "isErrorResponse": false, + "serializationOptions": {} + } + ], + "httpMethod": "POST", + "uri": "{endpoint}", + "path": "/jobs", + "bufferResponse": true, + "generateProtocolMethod": true, + "generateConvenienceMethod": true, + "crossLanguageDefinitionId": "LroFixture.begin", + "decorators": [], + "namespace": "LroFixture" + }, + "parameters": [], + "response": { + "type": { + "$id": "38", + "kind": "string", + "name": "string", + "crossLanguageDefinitionId": "TypeSpec.string", + "decorators": [] + }, + "resultSegments": [ + "result" + ] + }, + "isOverride": false, + "generateConvenient": true, + "generateProtocol": true, + "crossLanguageDefinitionId": "LroFixture.begin", + "lroMetadata": { + "finalStateVia": 3, + "finalResponse": { + "statusCodes": [ + 200 + ], + "bodyType": { + "$ref": "9" + } + }, + "resultPath": "result", + "pollingResponseModel": { + "$ref": "9" + }, + "statusMetadata": [ + { + "property": { + "$ref": "19" + }, + "propertySegments": [ + { + "$ref": "19" + } + ] + }, + { + "property": { + "$ref": "21" + }, + "propertySegments": [ + { + "$ref": "21" + } + ] + }, + { + "property": { + "$ref": "11" + }, + "propertySegments": [ + { + "$ref": "11" + } + ] + } + ], + "operationId": [ + { + "$id": "39", + "kind": "path", + "source": { + "location": "ResponseHeader", + "property": { + "$id": "40", + "kind": "property", + "name": "location", + "apiVersions": [], + "serializedName": "Operation-Location", + "type": { + "$id": "41", + "kind": "url", + "name": "ResourceLocation", + "crossLanguageDefinitionId": "TypeSpec.Rest.ResourceLocation", + "baseType": { + "$id": "42", + "kind": "url", + "name": "url", + "crossLanguageDefinitionId": "TypeSpec.url", + "decorators": [] + }, + "decorators": [] + }, + "optional": false, + "readOnly": false, + "discriminator": false, + "flatten": false, + "decorators": [], + "crossLanguageDefinitionId": "LroFixture..anonymous.location", + "serializationOptions": {}, + "isHttpMetadata": true, + "isExactName": false + } + }, + "pathParameter": { + "$ref": "28" + }, + "pollingPath": "/jobs/{jobId}/status" + }, + { + "$id": "43", + "kind": "property", + "property": { + "$ref": "17" + }, + "propertySegments": [ + { + "$ref": "17" + } + ] + } + ] + } + } + ], + "parameters": [ + { + "$id": "44", + "kind": "endpoint", + "name": "endpoint", + "serializedName": "endpoint", + "doc": "Service host", + "type": { + "$id": "45", + "kind": "url", + "name": "endpoint", + "crossLanguageDefinitionId": "TypeSpec.url" + }, + "isApiVersion": false, + "optional": false, + "scope": "Client", + "isEndpoint": true, + "serverUrlTemplate": "{endpoint}", + "skipUrlEncoding": false, + "readOnly": false, + "crossLanguageDefinitionId": "LroFixture.endpoint", + "isExactName": false + } + ], + "initializedBy": 1, + "decorators": [], + "crossLanguageDefinitionId": "LroFixture", + "apiVersions": [], + "isMultiServiceClient": false + } + ] +}