From c531ef53afb47684ff8c7bccefb1eb5c8c9fbe7c Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Thu, 12 Nov 2026 12:33:33 -0500 Subject: [PATCH 1/7] feat(http-client-csharp): preserve LRO status and identity metadata Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- packages/http-client-csharp/docs/plugins.md | 10 ++ .../emitter/src/lib/operation-converter.ts | 54 ++++++- .../emitter/src/lib/type-converter.ts | 2 +- .../emitter/src/type/input-service-method.ts | 34 +++- .../test/Unit/operation-converter.test.ts | 121 ++++++++++++++ .../InputLongRunningServiceMetadata.cs | 18 +++ .../InputTypes/InputLroOperationIdMetadata.cs | 10 ++ .../InputLroOperationIdPathMetadata.cs | 22 +++ .../InputLroOperationIdPropertyMetadata.cs | 19 +++ .../src/InputTypes/InputLroStatusMetadata.cs | 22 +++ .../src/InputTypes/InputOperationLink.cs | 17 ++ ...nputLongRunningServiceMetadataConverter.cs | 11 +- .../InputLroOperationIdMetadataConverter.cs | 55 +++++++ .../InputLroStatusMetadataConverter.cs | 39 +++++ .../InputOperationLinkConverter.cs | 42 +++++ .../Serialization/TypeSpecSerialization.cs | 3 + .../test/InputServiceMethodTests.cs | 147 +++++++++++++++++- 17 files changed, 619 insertions(+), 7 deletions(-) create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdMetadata.cs create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdPathMetadata.cs create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroOperationIdPropertyMetadata.cs create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLroStatusMetadata.cs create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputOperationLink.cs create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLroOperationIdMetadataConverter.cs create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLroStatusMetadataConverter.cs create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputOperationLinkConverter.cs diff --git a/packages/http-client-csharp/docs/plugins.md b/packages/http-client-csharp/docs/plugins.md index 66834b76ef9..a967374f99c 100644 --- a/packages/http-client-csharp/docs/plugins.md +++ b/packages/http-client-csharp/docs/plugins.md @@ -99,3 +99,13 @@ Notes: - Plugins loaded via the `plugins` option are applied in addition to any plugins discovered through `node_modules`. After the assemblies are loaded, every discovered `GeneratorPlugin` has its `Apply` method invoked on the selected generator before generation runs, so all of your registered visitors, rewriters, and references take effect. + +## Long-running operation metadata + +Custom generators can read TCGC `0.74.0` selections through `InputLongRunningServiceMetadata.StatusMetadata` and `OperationId`. The shared emitter transports these selections; it does not generate poller accessors or change polling behavior. + +Each `InputLroStatusMetadata` contains the selected `InputModelProperty` in `Property` and an ordered, typed `PropertySegments` path relative to `pollingInfo.responseModel`, not the final result. Segments preserve client names, wire serialization names, types, and optionality. Status selections retain TCGC's direct-then-inherited order. Consumers must read values from the latest monitor response, including initial monitor, failed, and canceled responses, and clear absent values rather than retain stale ones. Values are not continuation-token state; the next poll repopulates them after rehydration. + +`OperationId` preserves TCGC's availability order: `InputLroOperationIdPathMetadata` first, then `InputLroOperationIdPropertyMetadata` when both are selected. The path entry carries `Source` (the polling link's `Location` and `Property`), `PathParameter`, and the modeled monitor route in `PollingPath`. Use the parameter's wire name and route to locate the ID; it need not be the last URI segment. The property entry carries `Property` and typed `PropertySegments`, rooted at the same monitor model as status selections. Keep the URI-derived ID available as a fallback and after rehydration, but continue polling with the complete polling URL, never the extracted ID. + +Both collections are empty when selections are absent. Existing LRO constructors and final-response metadata remain unchanged. A custom generator using these fields must consume matching builds of the shared npm emitter and `Microsoft.TypeSpec.Generator.Input` assembly. 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..7f085f00015 100644 --- a/packages/http-client-csharp/emitter/src/lib/operation-converter.ts +++ b/packages/http-client-csharp/emitter/src/lib/operation-converter.ts @@ -41,6 +41,7 @@ import { } from "@typespec/compiler"; import { unsafe_getEventDefinitions } from "@typespec/events/experimental"; import type { HttpStatusCodeRange } from "@typespec/http"; +import { getHeaderFieldName } from "@typespec/http"; import { isTerminalEvent } from "@typespec/sse"; import type { CSharpEmitterContext } from "../sdk-context.js"; import { collectionFormatToDelimMap } from "../type/collection-format.js"; @@ -79,7 +80,7 @@ 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 } from "./type-converter.js"; import { getClientNamespaceString, isReadOnly } from "./utils.js"; export function fromSdkServiceMethod( @@ -726,6 +727,15 @@ function loadLongRunningMetadata( ): [InputLongRunningServiceMetadata, readonly Diagnostic[]] { const diagnostics = createDiagnosticCollector(); + const metadata = method.lroMetadata; + const convertProperty = (property: SdkModelPropertyType) => { + const converted = diagnostics.pipe( + fromSdkModelProperty(sdkContext, property, metadata.pollingInfo.responseModel), + ); + compilerAssert(converted, "LRO metadata must reference a supported model property."); + return converted; + }; + return diagnostics.wrap({ finalStateVia: convertLroFinalStateVia(method.lroMetadata.finalStateVia), finalResponse: { @@ -740,6 +750,48 @@ function loadLongRunningMetadata( : undefined, } as OperationResponse, resultPath: method.lroMetadata.finalResultPath, + statusMetadata: metadata.statusMetadata?.map((entry) => ({ + property: convertProperty(entry.property), + propertySegments: entry.propertySegments.map(convertProperty), + })), + operationId: metadata.operationId?.map((entry) => { + if (entry.kind === "property") { + return { + kind: "property", + property: convertProperty(entry.property), + propertySegments: entry.propertySegments.map(convertProperty), + }; + } + + const step = metadata.statusMonitorStep; + const pollingOperation = + step?.kind === "nextOperationLink" ? step.operation : step?.target.operation; + compilerAssert(pollingOperation, "A path operation ID requires a linked polling operation."); + const pathParameter = diagnostics.pipe( + fromParameter(sdkContext, entry.pathParameter, method.apiVersions), + ); + compilerAssert(pathParameter?.kind === "path", "An operation ID requires a path parameter."); + const sourceProperty = convertProperty(entry.source.property); + if (entry.source.location === "ResponseHeader") { + compilerAssert( + entry.source.property.__raw, + "A polling header must have a source property.", + ); + sourceProperty.serializedName = getHeaderFieldName( + sdkContext.program, + entry.source.property.__raw, + ); + } + return { + kind: "path", + source: { + location: entry.source.location, + property: sourceProperty, + }, + pathParameter, + pollingPath: pollingOperation.path, + }; + }), }); } 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 22fdaac29fd..e9f3fcee2d2 100644 --- a/packages/http-client-csharp/emitter/src/lib/type-converter.ts +++ b/packages/http-client-csharp/emitter/src/lib/type-converter.ts @@ -286,7 +286,7 @@ function fromSdkModelType( return diagnostics.wrap(inputModelType); } -function fromSdkModelProperty( +export function fromSdkModelProperty( sdkContext: CSharpEmitterContext, sdkProperty: SdkModelPropertyType, sdkModel: SdkModelType, 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..8cc53a8bdb9 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,13 @@ // 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, + 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 +81,34 @@ export interface InputLongRunningServiceMetadata { finalStateVia: OperationFinalStateVia; finalResponse: OperationResponse; resultPath?: string; + statusMetadata?: InputLroStatusMetadata[]; + operationId?: InputLroOperationIdMetadata[]; } +export interface InputLroStatusMetadata { + property: InputModelProperty; + /** Typed path relative to the polling response model, not the final result. */ + propertySegments: InputModelProperty[]; +} + +export interface InputOperationLink { + location: "ResponseHeader" | "ResponseBody" | "Self"; + property: InputModelProperty; +} + +export type InputLroOperationIdMetadata = + | { + kind: "path"; + source: InputOperationLink; + pathParameter: InputPathParameter; + pollingPath: string; + } + | { + kind: "property"; + property: InputModelProperty; + propertySegments: InputModelProperty[]; + }; + 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..cd78a9e03fb 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 @@ -19,6 +19,127 @@ describe("Operation Converter", () => { runner = await createEmitterTestHost(); }); + it.each(["both", "body", "path", "none"])( + "preserves selected LRO metadata and ordered operation ID sources: %s", + async (selection) => { + const selected = selection !== "none"; + 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; + } + @get @route("/jobs/{jobId}/status") + op getStatus( + ${["both", "path"].includes(selection) ? "@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 [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); + const json = serializeCodeModel(sdkContext, root); + strictEqual(json.includes('"statusMetadata"'), false); + strictEqual(json.includes('"operationId"'), false); + return; + } + 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 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"); + deepStrictEqual( + 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"); + } 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; + 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), + ); + }, + ); + 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.Input/src/InputTypes/InputLongRunningServiceMetadata.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputLongRunningServiceMetadata.cs index 143bac7cb69..25edc4ddccd 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,18 @@ 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) { FinalStateVia = finalStateVia; FinalResponse = finalResponse; ResultPath = resultPath; + StatusMetadata = statusMetadata; + OperationId = operationId; } public InputLongRunningServiceMetadata() : this(1, new InputOperationResponse(), null) { } @@ -19,6 +28,15 @@ public InputLongRunningServiceMetadata(int finalStateVia, InputOperationResponse public int FinalStateVia { get; } public InputOperationResponse FinalResponse { get; } public string? ResultPath { 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..180a5a02b83 --- /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(string location, InputModelProperty property) + { + Location = location; + Property = property; + } + + public string Location { get; } + public InputModelProperty Property { get; } + } +} 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..cc0ae622905 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,23 @@ private InputLongRunningServiceMetadata CreateOperationLongRunning(ref Utf8JsonR int finalStateVia = default; InputOperationResponse? finalResponse = null; string? resultPath = 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("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 ?? []); 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..1703bba9320 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputOperationLinkConverter.cs @@ -0,0 +1,42 @@ +// 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 not ("ResponseHeader" or "ResponseBody" or "Self")) + { + throw new JsonException($"Unknown operation link location: '{location}'."); + } + + return new InputOperationLink(location, + 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/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..73b31afb190 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,8 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Linq; +using System.Text.Json; using Microsoft.TypeSpec.Generator.Tests.Common; using NUnit.Framework; @@ -8,6 +10,149 @@ namespace Microsoft.TypeSpec.Generator.Input.Tests { public class InputServiceMethodTests { + [TestCase("lro")] + [TestCase("lropaging")] + public void LoadsLongRunningMetadataWithTypedPathsAndOrderedIdSources(string methodKind) + { + var json = """ + { + "finalStateVia": 1, + "statusMetadata": [{ + "property": { + "$id": "progress", "name": "progress", "serializedName": "percent_complete", + "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.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("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("""{"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.IsEmpty(new InputLongRunningServiceMetadata().StatusMetadata); + } + [Test] public void CanUpdateServiceMethodName() { From 8475016b972cabcdb81ede36d24df1441088ce5d Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 18 Nov 2026 12:05:52 -0500 Subject: [PATCH 2/7] fix(http-client-csharp): preserve LRO metadata references Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- packages/http-client-csharp/docs/plugins.md | 10 - .../emitter/src/lib/operation-converter.ts | 216 ++++++++--- .../emitter/src/type/input-service-method.ts | 36 +- .../test/Unit/operation-converter.test.ts | 353 +++++++++++++++++- .../InputLongRunningServiceMetadata.cs | 12 + .../src/InputTypes/InputOperationLink.cs | 4 +- .../InputTypes/InputOperationLinkLocation.cs | 19 + ...nputLongRunningServiceMetadataConverter.cs | 4 +- .../InputOperationLinkConverter.cs | 12 +- .../test/InputServiceMethodTests.cs | 146 +++++++- 10 files changed, 715 insertions(+), 97 deletions(-) create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputOperationLinkLocation.cs diff --git a/packages/http-client-csharp/docs/plugins.md b/packages/http-client-csharp/docs/plugins.md index a967374f99c..66834b76ef9 100644 --- a/packages/http-client-csharp/docs/plugins.md +++ b/packages/http-client-csharp/docs/plugins.md @@ -99,13 +99,3 @@ Notes: - Plugins loaded via the `plugins` option are applied in addition to any plugins discovered through `node_modules`. After the assemblies are loaded, every discovered `GeneratorPlugin` has its `Apply` method invoked on the selected generator before generation runs, so all of your registered visitors, rewriters, and references take effect. - -## Long-running operation metadata - -Custom generators can read TCGC `0.74.0` selections through `InputLongRunningServiceMetadata.StatusMetadata` and `OperationId`. The shared emitter transports these selections; it does not generate poller accessors or change polling behavior. - -Each `InputLroStatusMetadata` contains the selected `InputModelProperty` in `Property` and an ordered, typed `PropertySegments` path relative to `pollingInfo.responseModel`, not the final result. Segments preserve client names, wire serialization names, types, and optionality. Status selections retain TCGC's direct-then-inherited order. Consumers must read values from the latest monitor response, including initial monitor, failed, and canceled responses, and clear absent values rather than retain stale ones. Values are not continuation-token state; the next poll repopulates them after rehydration. - -`OperationId` preserves TCGC's availability order: `InputLroOperationIdPathMetadata` first, then `InputLroOperationIdPropertyMetadata` when both are selected. The path entry carries `Source` (the polling link's `Location` and `Property`), `PathParameter`, and the modeled monitor route in `PollingPath`. Use the parameter's wire name and route to locate the ID; it need not be the last URI segment. The property entry carries `Property` and typed `PropertySegments`, rooted at the same monitor model as status selections. Keep the URI-derived ID available as a fallback and after rehydration, but continue polling with the complete polling URL, never the extracted ID. - -Both collections are empty when selections are absent. Existing LRO constructors and final-response metadata remain unchanged. A custom generator using these fields must consume matching builds of the shared npm emitter and `Microsoft.TypeSpec.Generator.Input` assembly. 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 7f085f00015..1e26246dbe7 100644 --- a/packages/http-client-csharp/emitter/src/lib/operation-converter.ts +++ b/packages/http-client-csharp/emitter/src/lib/operation-converter.ts @@ -27,6 +27,7 @@ import { getHttpOperationParameter, getParamAlias, getResponseAsBool, + getSdkModelWithDiagnostics, isHttpMetadata, shouldGenerateConvenient, shouldGenerateProtocol, @@ -55,17 +56,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, @@ -136,7 +141,7 @@ export function fromSdkServiceMethod( ), ); lroServiceMethod.lroMetadata = diagnostics.pipe( - loadLongRunningMetadata(sdkContext, sdkMethod), + loadLongRunningMetadata(sdkContext, sdkMethod, rootApiVersions), ); method = lroServiceMethod; break; @@ -151,7 +156,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), @@ -724,18 +729,165 @@ export function fromMethodParameter( function loadLongRunningMetadata( sdkContext: CSharpEmitterContext, method: SdkLroServiceMethod | SdkLroPagingServiceMethod, + rootApiVersions: string[], ): [InputLongRunningServiceMetadata, readonly Diagnostic[]] { const diagnostics = createDiagnosticCollector(); - const metadata = method.lroMetadata; - const convertProperty = (property: SdkModelPropertyType) => { - const converted = diagnostics.pipe( - fromSdkModelProperty(sdkContext, property, metadata.pollingInfo.responseModel), + const reportInvalidSelection = (message: string) => { + diagnostics.add( + createDiagnostic({ + code: "general-error", + format: { message: `Invalid LRO metadata for '${method.name}': ${message}` }, + target: method.__raw ?? NoTarget, + }), + ); + }; + + 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 = diagnostics.pipe( + fromSdkModelProperty(sdkContext, entry.property, metadata.pollingInfo.responseModel), ); - compilerAssert(converted, "LRO metadata must reference a supported model property."); - return converted; + if (!property) { + reportInvalidSelection("a selected property could not be converted."); + return undefined; + } + + const propertySegments: InputModelProperty[] = []; + for (const segment of entry.propertySegments) { + const converted = diagnostics.pipe( + fromSdkModelProperty(sdkContext, segment, metadata.pollingInfo.responseModel), + ); + 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; + } + + const step = metadata.statusMonitorStep; + const pollingOperation = + step?.kind === "nextOperationLink" ? step.operation : step?.target.operation; + if (!pollingOperation?.path || !entry.pathParameter || entry.pathParameter.kind !== "path") { + reportInvalidSelection( + "the operation ID requires a linked polling operation and path parameter.", + ); + continue; + } + + const pollingClient = sdkContext.getClientForOperation(pollingOperation.__raw.operation); + const clients = [...sdkContext.sdkPackage.clients]; + let pollingMethod: SdkServiceMethod | undefined; + 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 ?? [])); + } + + // 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; + } + + let location: InputOperationLinkLocation; + switch (entry.source.location) { + case "ResponseHeader": + location = InputOperationLinkLocation.ResponseHeader; + break; + case "ResponseBody": + location = InputOperationLinkLocation.ResponseBody; + break; + case "Self": + location = InputOperationLinkLocation.Self; + break; + default: + reportInvalidSelection("the operation ID has an unsupported polling link location."); + 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; + } + 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), finalResponse: { @@ -750,48 +902,12 @@ function loadLongRunningMetadata( : undefined, } as OperationResponse, resultPath: method.lroMetadata.finalResultPath, - statusMetadata: metadata.statusMetadata?.map((entry) => ({ - property: convertProperty(entry.property), - propertySegments: entry.propertySegments.map(convertProperty), - })), - operationId: metadata.operationId?.map((entry) => { - if (entry.kind === "property") { - return { - kind: "property", - property: convertProperty(entry.property), - propertySegments: entry.propertySegments.map(convertProperty), - }; - } - - const step = metadata.statusMonitorStep; - const pollingOperation = - step?.kind === "nextOperationLink" ? step.operation : step?.target.operation; - compilerAssert(pollingOperation, "A path operation ID requires a linked polling operation."); - const pathParameter = diagnostics.pipe( - fromParameter(sdkContext, entry.pathParameter, method.apiVersions), - ); - compilerAssert(pathParameter?.kind === "path", "An operation ID requires a path parameter."); - const sourceProperty = convertProperty(entry.source.property); - if (entry.source.location === "ResponseHeader") { - compilerAssert( - entry.source.property.__raw, - "A polling header must have a source property.", - ); - sourceProperty.serializedName = getHeaderFieldName( - sdkContext.program, - entry.source.property.__raw, - ); - } - return { - kind: "path", - source: { - location: entry.source.location, - property: sourceProperty, - }, - pathParameter, - pollingPath: pollingOperation.path, - }; - }), + pollingResponseModel: + statusMetadata.length > 0 || operationId.length > 0 + ? diagnostics.pipe(fromSdkType(sdkContext, metadata.pollingInfo.responseModel)) + : undefined, + statusMetadata: metadata.statusMetadata ? statusMetadata : undefined, + operationId: metadata.operationId ? operationId : undefined, }); } 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 8cc53a8bdb9..8de1aa83d3e 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 @@ -5,6 +5,7 @@ import type { InputOperation } from "./input-operation.js"; import type { InputMethodParameter, InputModelProperty, + InputModelType, InputParameter, InputPathParameter, InputType, @@ -81,6 +82,7 @@ export interface InputLongRunningServiceMetadata { finalStateVia: OperationFinalStateVia; finalResponse: OperationResponse; resultPath?: string; + pollingResponseModel?: InputModelType; statusMetadata?: InputLroStatusMetadata[]; operationId?: InputLroOperationIdMetadata[]; } @@ -91,23 +93,33 @@ export interface InputLroStatusMetadata { propertySegments: InputModelProperty[]; } +export enum InputOperationLinkLocation { + Unknown = "Unknown", + ResponseHeader = "ResponseHeader", + ResponseBody = "ResponseBody", + Self = "Self", +} + export interface InputOperationLink { - location: "ResponseHeader" | "ResponseBody" | "Self"; + 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 = - | { - kind: "path"; - source: InputOperationLink; - pathParameter: InputPathParameter; - pollingPath: string; - } - | { - kind: "property"; - property: InputModelProperty; - propertySegments: InputModelProperty[]; - }; + InputLroOperationIdPathMetadata | InputLroOperationIdPropertyMetadata; export interface InputLongRunningPagingServiceMethod extends InputServiceMethodBase { kind: "lropaging"; 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 cd78a9e03fb..861c9ba161f 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,5 @@ import type { TestHost } from "@typespec/compiler/testing"; -import { deepStrictEqual, ok, strictEqual } from "assert"; +import { deepStrictEqual, notStrictEqual, ok, strictEqual } from "assert"; import { beforeEach, describe, it } from "vitest"; import { serializeCodeModel } from "../../src/code-model-writer.js"; import { createModel } from "../../src/lib/client-model-builder.js"; @@ -19,10 +19,29 @@ describe("Operation Converter", () => { runner = await createEmitterTestHost(); }); - it.each(["both", "body", "path", "none"])( - "preserves selected LRO metadata and ordered operation ID sources: %s", - async (selection) => { + 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; } @@ -46,17 +65,7 @@ describe("Operation Converter", () => { ${selected ? "@lroStatusMetadata" : ""} statistics?: Statistics; } - @get @route("/jobs/{jobId}/status") - op getStatus( - ${["both", "path"].includes(selection) ? "@lroOperationId" : ""} - @path("jobId") id: string - ): Monitor; - @post @route("/jobs") - @pollingOperation(getStatus) - op begin(): { - @statusCode code: 202; - @pollingLocation @header("Operation-Location") location: ResourceLocation; - }; + ${pollingFirst ? `${pollingOperation}\n${startOperation}` : `${startOperation}\n${pollingOperation}`} `, runner, { IsAzureCoreNeeded: true, IsTCGCNeeded: true }, @@ -70,11 +79,17 @@ describe("Operation Converter", () => { 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, @@ -90,6 +105,12 @@ describe("Operation Converter", () => { 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, @@ -105,7 +126,7 @@ describe("Operation Converter", () => { if (entry.kind === "path") { strictEqual(entry.pollingPath, "/jobs/{jobId}/status"); strictEqual(entry.pathParameter.serializedName, "jobId"); - deepStrictEqual( + strictEqual( entry.pathParameter, root.clients[0].methods .find((m) => m.name === "getStatus")! @@ -113,6 +134,23 @@ describe("Operation Converter", () => { ); 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); @@ -123,6 +161,12 @@ describe("Operation Converter", () => { 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], @@ -137,6 +181,283 @@ describe("Operation Converter", () => { 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"], + ); + ok( + diagnostics.some( + (d) => + d.severity === "error" && + d.code === "@typespec/http-client-csharp/general-error" && + d.message.includes("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"], + ); + ok( + diagnostics.some( + (d) => + d.severity === "error" && + d.code === "@typespec/http-client-csharp/general-error" && + d.message.includes("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([ + { + decorators: '@pollingLocation @header("Operation-Location")', + expectedLocation: "ResponseHeader", + expectedName: "Operation-Location", + }, + { + decorators: "@pollingLocation", + expectedLocation: "ResponseBody", + expectedName: "status_url", + }, + { + decorators: "@pollingLocation @body", + 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"); + if (expectedLocation === "Self") { + // The linked-operation fixture is classified as ResponseBody by the current TCGC. + sdkPath.source.location = "Self"; + } + + 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); + } }, ); 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 25edc4ddccd..54919eef11a 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 @@ -15,12 +15,20 @@ public InputLongRunningServiceMetadata(int finalStateVia, InputOperationResponse 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) { } @@ -29,6 +37,10 @@ public InputLongRunningServiceMetadata(int finalStateVia, InputOperationResponse public InputOperationResponse FinalResponse { get; } public string? ResultPath { get; } /// + /// The polling response model owning the selected status metadata and operation ID paths, when present. + /// + public InputModelType? PollingResponseModel { get; } + /// /// Auxiliary values from the latest polling response, in selection order. /// public IReadOnlyList StatusMetadata { 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 index 180a5a02b83..5999d68e595 100644 --- 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 @@ -5,13 +5,13 @@ namespace Microsoft.TypeSpec.Generator.Input { public sealed class InputOperationLink { - public InputOperationLink(string location, InputModelProperty property) + public InputOperationLink(InputOperationLinkLocation location, InputModelProperty property) { Location = location; Property = property; } - public string Location { get; } + 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/Serialization/InputLongRunningServiceMetadataConverter.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputLongRunningServiceMetadataConverter.cs index cc0ae622905..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 @@ -30,6 +30,7 @@ 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) @@ -37,6 +38,7 @@ private InputLongRunningServiceMetadata CreateOperationLongRunning(ref Utf8JsonR var isKnownProperty = reader.TryReadInt32("finalStateVia", ref finalStateVia) || reader.TryReadComplexType("finalResponse", options, ref finalResponse) || reader.TryReadString("resultPath", ref resultPath) + || reader.TryReadComplexType("pollingResponseModel", options, ref pollingResponseModel) || reader.TryReadComplexType("statusMetadata", options, ref statusMetadata) || reader.TryReadComplexType("operationId", options, ref operationId); @@ -46,7 +48,7 @@ private InputLongRunningServiceMetadata CreateOperationLongRunning(ref Utf8JsonR } } - var result = new InputLongRunningServiceMetadata(finalStateVia, finalResponse ?? new InputOperationResponse(), resultPath, statusMetadata ?? [], operationId ?? []); + 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/InputOperationLinkConverter.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputOperationLinkConverter.cs index 1703bba9320..f31f64c2e0c 100644 --- 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 @@ -27,12 +27,16 @@ public override InputOperationLink Read(ref Utf8JsonReader reader, Type typeToCo } } - if (location is not ("ResponseHeader" or "ResponseBody" or "Self")) + var parsedLocation = location switch { - throw new JsonException($"Unknown operation link location: '{location}'."); - } + nameof(InputOperationLinkLocation.ResponseHeader) => InputOperationLinkLocation.ResponseHeader, + nameof(InputOperationLinkLocation.ResponseBody) => InputOperationLinkLocation.ResponseBody, + nameof(InputOperationLinkLocation.Self) => InputOperationLinkLocation.Self, + null => throw new JsonException("Operation link must have a location."), + _ => InputOperationLinkLocation.Unknown + }; - return new InputOperationLink(location, + return new InputOperationLink(parsedLocation, property ?? throw new JsonException("Operation link must have a property.")); } 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 73b31afb190..1cfe86d1b0a 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,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using System.Linq; @@ -81,7 +81,7 @@ public void LoadsLongRunningMetadataWithTypedPathsAndOrderedIdSources(string met 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("ResponseHeader", path.Source.Location); + 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); @@ -124,6 +124,147 @@ public void LoadsLongRunningMetadataWithTypedPathsAndOrderedIdSources(string met ((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":[]}""")] @@ -150,6 +291,7 @@ public void LoadsLegacyLongRunningMetadataWithoutSelections() Assert.AreEqual("result", metadata.ResultPath); Assert.IsEmpty(metadata.StatusMetadata); Assert.IsEmpty(metadata.OperationId); + Assert.IsNull(metadata.PollingResponseModel); Assert.IsEmpty(new InputLongRunningServiceMetadata().StatusMetadata); } From 7dac1f911d23879408c39a2c6ae6ca7e87dde1be Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 18 Nov 2026 13:36:12 -0500 Subject: [PATCH 3/7] fix(http-client-csharp): preserve LRO metadata for hidden monitors Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../emitter/src/lib/operation-converter.ts | 58 +- .../emitter/src/type/input-service-method.ts | 1 + .../test/Unit/operation-converter.test.ts | 183 ++++- .../InputLongRunningServiceMetadata.cs | 2 +- .../test/InputServiceMethodTests.cs | 87 +++ .../LoadsEmittedLroMetadata/main.tsp | 56 ++ .../LoadsEmittedLroMetadata/tspCodeModel.json | 713 ++++++++++++++++++ .../src/Providers/PropertyProvider.cs | 17 +- .../test/Providers/PropertyProviderTests.cs | 53 ++ 9 files changed, 1118 insertions(+), 52 deletions(-) create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/main.tsp create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/tspCodeModel.json 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 1e26246dbe7..f7a69da5ba2 100644 --- a/packages/http-client-csharp/emitter/src/lib/operation-converter.ts +++ b/packages/http-client-csharp/emitter/src/lib/operation-converter.ts @@ -42,7 +42,7 @@ import { } from "@typespec/compiler"; import { unsafe_getEventDefinitions } from "@typespec/events/experimental"; import type { HttpStatusCodeRange } from "@typespec/http"; -import { getHeaderFieldName } 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"; @@ -733,6 +733,24 @@ function loadLongRunningMetadata( ): [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({ @@ -797,9 +815,6 @@ function loadLongRunningMetadata( continue; } - const step = metadata.statusMonitorStep; - const pollingOperation = - step?.kind === "nextOperationLink" ? step.operation : step?.target.operation; if (!pollingOperation?.path || !entry.pathParameter || entry.pathParameter.kind !== "path") { reportInvalidSelection( "the operation ID requires a linked polling operation and path parameter.", @@ -807,20 +822,6 @@ function loadLongRunningMetadata( continue; } - const pollingClient = sdkContext.getClientForOperation(pollingOperation.__raw.operation); - const clients = [...sdkContext.sdkPackage.clients]; - let pollingMethod: SdkServiceMethod | undefined; - 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 ?? [])); - } - // 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; @@ -848,13 +849,23 @@ function loadLongRunningMetadata( 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 = InputOperationLinkLocation.ResponseBody; + location = + isBody(sdkContext.program, rawSource) || isBodyRoot(sdkContext.program, rawSource) + ? InputOperationLinkLocation.Self + : InputOperationLinkLocation.ResponseBody; break; case "Self": location = InputOperationLinkLocation.Self; @@ -864,12 +875,6 @@ function loadLongRunningMetadata( 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; - } const sourceModel = diagnostics.pipe(getSdkModelWithDiagnostics(sdkContext, rawSource.model)); const convertedSource = diagnostics.pipe(fromSdkModelProperty(sdkContext, source, sourceModel)); if (!convertedSource) { @@ -903,7 +908,8 @@ function loadLongRunningMetadata( } as OperationResponse, resultPath: method.lroMetadata.finalResultPath, pollingResponseModel: - statusMetadata.length > 0 || operationId.length > 0 + (statusMetadata.length > 0 || operationId.length > 0) && + (pollingMethod || sdkContext.__typeCache.types.has(metadata.pollingInfo.responseModel)) ? diagnostics.pipe(fromSdkType(sdkContext, metadata.pollingInfo.responseModel)) : undefined, statusMetadata: metadata.statusMetadata ? statusMetadata : undefined, 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 8de1aa83d3e..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 @@ -82,6 +82,7 @@ export interface InputLongRunningServiceMetadata { finalStateVia: OperationFinalStateVia; finalResponse: OperationResponse; resultPath?: string; + /** Present when the polling response model is otherwise emitted. */ pollingResponseModel?: InputModelType; statusMetadata?: InputLroStatusMetadata[]; operationId?: InputLroOperationIdMetadata[]; 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 861c9ba161f..810fdeaac97 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,6 @@ -import type { TestHost } from "@typespec/compiler/testing"; +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"; @@ -19,6 +20,21 @@ 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", readFileSync(new URL("main.tsp", fixture), "utf8")); + 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 })), @@ -254,14 +270,11 @@ describe("Operation Converter", () => { method.lroMetadata.operationId?.map((entry) => entry.kind), ["property"], ); - ok( - diagnostics.some( - (d) => - d.severity === "error" && - d.code === "@typespec/http-client-csharp/general-error" && - d.message.includes("operation ID"), - ), - ); + 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 () => { @@ -298,14 +311,12 @@ describe("Operation Converter", () => { method.lroMetadata.operationId?.map((entry) => entry.kind), ["path", "property"], ); - ok( - diagnostics.some( - (d) => - d.severity === "error" && - d.code === "@typespec/http-client-csharp/general-error" && - d.message.includes("nonempty property path"), - ), - ); + 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 () => { @@ -382,6 +393,135 @@ describe("Operation Converter", () => { strictEqual(path.pollingPath, "/jobs/{jobId}/status"); }); + 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([ { decorators: '@pollingLocation @header("Operation-Location")', @@ -398,6 +538,11 @@ describe("Operation Converter", () => { 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 }) => { @@ -432,10 +577,6 @@ describe("Operation Converter", () => { ok(sdkMethod?.kind === "lro"); const sdkPath = sdkMethod.lroMetadata.operationId?.find((entry) => entry.kind === "path"); ok(sdkPath?.kind === "path"); - if (expectedLocation === "Self") { - // The linked-operation fixture is classified as ResponseBody by the current TCGC. - sdkPath.source.location = "Self"; - } const [root, diagnostics] = createModel(sdkContext); deepStrictEqual(diagnostics, []); 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 54919eef11a..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 @@ -37,7 +37,7 @@ public InputLongRunningServiceMetadata(int finalStateVia, InputOperationResponse public InputOperationResponse FinalResponse { get; } public string? ResultPath { get; } /// - /// The polling response model owning the selected status metadata and operation ID paths, when present. + /// The polling response model when it is otherwise emitted. Selections may be present even when this is null. /// public InputModelType? PollingResponseModel { get; } /// 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 1cfe86d1b0a..8d11d001c8a 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,7 @@ // 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; @@ -10,6 +11,47 @@ 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.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("lro")] [TestCase("lropaging")] public void LoadsLongRunningMetadataWithTypedPathsAndOrderedIdSources(string methodKind) @@ -295,6 +337,51 @@ public void LoadsLegacyLongRunningMetadataWithoutSelections() 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/main.tsp b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/main.tsp new file mode 100644 index 00000000000..87b51871e96 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/main.tsp @@ -0,0 +1,56 @@ +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 + @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") +@pollingOperation(getStatus) +op begin(): { + @statusCode code: 202; + @pollingLocation @header("Operation-Location") location: ResourceLocation; +}; 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..cb76d0c66cd --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/tspCodeModel.json @@ -0,0 +1,713 @@ +{ + "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, + "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": "public", + "apiVersions": [], + "operation": { + "$id": "35", + "name": "begin", + "isExactName": false, + "accessibility": "public", + "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 + } + ] +} diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs index 4d623af6ef6..b547ace0a53 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs @@ -81,14 +81,23 @@ internal static bool TryCreate(InputProperty inputProperty, TypeProvider enclosi } public PropertyProvider(InputProperty inputProperty, TypeProvider enclosingType) + : this(inputProperty, enclosingType, false) + { + } + + /// + /// Creates a property, optionally treating the input as output-only even when it has no owning input model. + /// + public PropertyProvider(InputProperty inputProperty, TypeProvider enclosingType, bool isOutputOnly) : this( inputProperty, CodeModelGenerator.Instance.TypeFactory.CreateCSharpType(inputProperty.Type) ?? throw new InvalidOperationException($"Could not create CSharpType for property {inputProperty.Name}"), - enclosingType) + enclosingType, + isOutputOnly) { } - private PropertyProvider(InputProperty inputProperty, CSharpType propertyType, TypeProvider enclosingType) + private PropertyProvider(InputProperty inputProperty, CSharpType propertyType, TypeProvider enclosingType, bool isOutputOnly = false) { InputProperty = inputProperty; if (!inputProperty.IsRequired && !propertyType.IsCollection) @@ -99,7 +108,7 @@ private PropertyProvider(InputProperty inputProperty, CSharpType propertyType, T EnclosingType = enclosingType; _serializationFormat = CodeModelGenerator.Instance.TypeFactory.GetSerializationFormat(inputProperty); _isRequiredNonNullableConstant = inputProperty.IsRequired && propertyType is { IsLiteral: true, IsNullable: false }; - var propHasSetter = PropertyHasSetter(propertyType, inputProperty); + var propHasSetter = !isOutputOnly && PropertyHasSetter(propertyType, inputProperty); MethodSignatureModifiers setterModifier = !propHasSetter ? MethodSignatureModifiers.None : inputProperty.IsReadOnly && !IsDiscriminatorProperty(inputProperty) ? MethodSignatureModifiers.Private | MethodSignatureModifiers.Protected @@ -107,7 +116,7 @@ private PropertyProvider(InputProperty inputProperty, CSharpType propertyType, T Type = inputProperty.IsReadOnly ? propertyType.OutputType : propertyType; IsDiscriminator = IsDiscriminatorProperty(inputProperty); - var hasOutputUsage = inputProperty.EnclosingType?.Usage.HasFlag(InputModelTypeUsage.Output) ?? false; + var hasOutputUsage = isOutputOnly || (inputProperty.EnclosingType?.Usage.HasFlag(InputModelTypeUsage.Output) ?? false); Modifiers = IsDiscriminator || (!hasOutputUsage && _isRequiredNonNullableConstant) ? MethodSignatureModifiers.Internal : MethodSignatureModifiers.Public; var identifierName = inputProperty.IsExactName ? inputProperty.Name : inputProperty.Name.ToIdentifierName(); if (!inputProperty.IsExactName) diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs index f7e823f5786..127c25e417a 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs @@ -76,6 +76,59 @@ public void TestSnakeCaseProperty() Assert.AreEqual("Description for snake_case", property.Description!.ToString()); } + [TestCase(false)] + [TestCase(true)] + public void OutputOnlyPropertyCanBeCreatedWithoutAnInputModel(bool isReadOnly) + { + var input = InputFactory.Property( + "StartTime", + new InputDateTimeType( + DateTimeKnownEncoding.Rfc7231, + "utcDateTime", + "TypeSpec.utcDateTime", + InputPrimitiveType.String), + isReadOnly: isReadOnly, + wireName: "started_at"); + + var property = new PropertyProvider(input, new TestTypeProvider(), isOutputOnly: true); + + Assert.IsNull(input.EnclosingType); + Assert.AreEqual("StartsOn", property.Name); + Assert.AreEqual(new CSharpType(typeof(DateTimeOffset)).WithNullable(true), property.Type); + Assert.AreEqual(SerializationFormat.DateTime_RFC7231, property.SerializationFormat); + Assert.AreEqual("started_at", property.WireInfo?.SerializedName); + Assert.IsFalse(property.Body.HasSetter); + } + + [Test] + public void OutputOnlyPropertyPreservesPropertyLevelArrayEncoding() + { + var input = InputFactory.Property( + "items", + InputFactory.Array(InputPrimitiveType.String), + encode: ArrayKnownEncoding.PipeDelimited); + + var property = new PropertyProvider(input, new TestTypeProvider(), isOutputOnly: true); + + Assert.IsNull(input.EnclosingType); + Assert.AreEqual(SerializationFormat.Array_PipeDelimited, property.SerializationFormat); + Assert.IsFalse(property.Body.HasSetter); + } + + [Test] + public void OutputOnlyPropertyCanUseAModelTypeWithoutAnInputModelOwner() + { + var details = InputFactory.Model("Details", properties: []); + var input = InputFactory.Property("details", details, isReadOnly: true); + + var property = new PropertyProvider(input, new TestTypeProvider(), isOutputOnly: true); + + Assert.IsNull(input.EnclosingType); + Assert.AreEqual("Details", property.Type.Name); + Assert.IsTrue(property.Type.IsNullable); + Assert.IsFalse(property.Body.HasSetter); + } + [Test] public void TestPropertyDocumentationPreservesCodeSpans() { From 944b69ebbb55cd62efb497d7b69263e0dd67d295 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 18 Nov 2026 15:04:25 -0500 Subject: [PATCH 4/7] fix(http-client-csharp): preserve LRO metadata paths and access Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../emitter/src/lib/client-model-builder.ts | 26 +- .../emitter/src/lib/operation-converter.ts | 88 +++- .../emitter/src/lib/type-converter.ts | 14 +- .../emitter/src/sdk-context.ts | 3 + .../test/Unit/operation-converter.test.ts | 436 ++++++++++++++++++ .../ModelReaderWriterContextDefinition.cs | 41 +- ...ModelReaderWriterContextDefinitionTests.cs | 60 +++ .../src/InputTypes/InputPathParameter.cs | 4 + .../src/InputTypes/InputPathParameterStyle.cs | 21 + .../InputPathParameterConverter.cs | 11 + .../test/InputServiceMethodTests.cs | 26 +- .../LoadsEmittedLroMetadata/main.tsp | 2 + .../LoadsEmittedLroMetadata/tspCodeModel.json | 5 +- .../src/Providers/PropertyProvider.cs | 4 +- .../test/Providers/PropertyProviderTests.cs | 33 ++ 15 files changed, 744 insertions(+), 30 deletions(-) create mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/InputPathParameterStyle.cs 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..adb64612efc 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,6 +6,7 @@ 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"; @@ -21,8 +22,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. @@ -104,7 +105,7 @@ 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, constants, sdkContext.__typeCache.lroMetadataEnums); // 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,7 +167,11 @@ 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[], + constants: InputLiteralType[], + lroMetadataEnums: Set, +): void { // First, fix names for constants and constant-derived enums in model properties for (const model of models) { for (const property of model.properties) { @@ -174,16 +179,13 @@ function fixNamingConflicts(models: InputModelType[], constants: InputLiteralTyp 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); + if (lroMetadataEnums.has(type)) { + type.usage |= UsageFlags.Output | UsageFlags.Json; + } } } } 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 f7a69da5ba2..e4e691276c9 100644 --- a/packages/http-client-csharp/emitter/src/lib/operation-converter.ts +++ b/packages/http-client-csharp/emitter/src/lib/operation-converter.ts @@ -31,6 +31,8 @@ import { isHttpMetadata, shouldGenerateConvenient, shouldGenerateProtocol, + updateUsageOrAccess, + UsageFlags, } from "@azure-tools/typespec-client-generator-core"; import type { Diagnostic, Union } from "@typespec/compiler"; import { @@ -85,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 { fromSdkModelProperty, fromSdkType } from "./type-converter.js"; +import { + fromSdkModelProperty, + fromSdkType, + normalizeLiteralTypeForProperty, +} from "./type-converter.js"; import { getClientNamespaceString, isReadOnly } from "./utils.js"; export function fromSdkServiceMethod( @@ -761,6 +767,67 @@ function loadLongRunningMetadata( ); }; + 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[]; @@ -770,9 +837,7 @@ function loadLongRunningMetadata( return undefined; } - const property = diagnostics.pipe( - fromSdkModelProperty(sdkContext, entry.property, metadata.pollingInfo.responseModel), - ); + const property = convertSelectedProperty(entry.property); if (!property) { reportInvalidSelection("a selected property could not be converted."); return undefined; @@ -780,9 +845,7 @@ function loadLongRunningMetadata( const propertySegments: InputModelProperty[] = []; for (const segment of entry.propertySegments) { - const converted = diagnostics.pipe( - fromSdkModelProperty(sdkContext, segment, metadata.pollingInfo.responseModel), - ); + const converted = convertSelectedProperty(segment); if (!converted) { reportInvalidSelection("a selected property path could not be converted."); return undefined; @@ -907,11 +970,12 @@ function loadLongRunningMetadata( : undefined, } as OperationResponse, resultPath: method.lroMetadata.finalResultPath, - pollingResponseModel: - (statusMetadata.length > 0 || operationId.length > 0) && - (pollingMethod || sdkContext.__typeCache.types.has(metadata.pollingInfo.responseModel)) - ? diagnostics.pipe(fromSdkType(sdkContext, metadata.pollingInfo.responseModel)) - : undefined, + 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 64edc9275bd..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" } @@ -322,6 +322,7 @@ export 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/test/Unit/operation-converter.test.ts b/packages/http-client-csharp/emitter/test/Unit/operation-converter.test.ts index 810fdeaac97..bcb78e27d68 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,3 +1,4 @@ +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"; @@ -393,6 +394,76 @@ describe("Operation Converter", () => { 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) => { @@ -522,6 +593,371 @@ describe("Operation Converter", () => { }, ); + 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([ + { 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")', 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..3b65b3f7aea 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,10 @@ protected override IReadOnlyList BuildAttributes() } foreach (var provider in buildableProviders) { - if (!IsPublicApi(provider) + if ((!IsPublicApi(provider) + && (!provider.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal) + || provider.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Private) + || !lroMetadataTypes.Contains(provider.Type))) || !ShouldWriteProvider(provider) || customizedBuildableTypes.Contains(GetTypeIdentity(provider.Type))) { @@ -90,6 +95,40 @@ protected override IReadOnlyList BuildAttributes() return attributes.OrderBy(a => GetSimpleTypeName(a.Key)).Select(kvp => kvp.Value).ToList(); } + 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; + } + + 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..b579c6e2998 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,66 @@ 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())); + } + + [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/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/InputPathParameterConverter.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/InputPathParameterConverter.cs index ab6e4c0956b..4fedd03c6e8 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,15 @@ internal static InputPathParameter ReadInputPathParameter(ref Utf8JsonReader rea parameter.DefaultValue = defaultValue; parameter.Scope = InputParameter.ParseScope(type, name, scope); parameter.Explode = explode; + parameter.Style = style switch + { + "simple" => InputPathParameterStyle.Simple, + "label" => InputPathParameterStyle.Label, + "matrix" => InputPathParameterStyle.Matrix, + "path" => InputPathParameterStyle.Path, + "fragment" => InputPathParameterStyle.Fragment, + _ => InputPathParameterStyle.Unknown, + }; parameter.SkipUrlEncoding = skipUrlEncoding; parameter.MethodParameterSegments = methodParameterSegments; parameter.IsExactName = isExactName; 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 8d11d001c8a..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 @@ -31,6 +31,7 @@ public void LoadsEmittedLroMetadata() 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); @@ -52,6 +53,28 @@ public void LoadsEmittedLroMetadata() 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) @@ -61,7 +84,7 @@ public void LoadsLongRunningMetadataWithTypedPathsAndOrderedIdSources(string met "finalStateVia": 1, "statusMetadata": [{ "property": { - "$id": "progress", "name": "progress", "serializedName": "percent_complete", + "$id": "progress", "name": "progress", "serializedName": "percent_complete", "access": "internal", "optional": true, "type": { "kind": "int32" } }, "propertySegments": [{ "$ref": "progress" }] @@ -116,6 +139,7 @@ public void LoadsLongRunningMetadataWithTypedPathsAndOrderedIdSources(string met 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]); diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/main.tsp b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/main.tsp index 87b51871e96..75fd6f5fe87 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/main.tsp +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/main.tsp @@ -35,6 +35,7 @@ model Monitor extends BaseMonitor { id: string; @lroStatusMetadata + @access(Access.internal, "csharp") @clientName("progress") @encodedName("application/json", "percent_complete") percent?: int32; @@ -49,6 +50,7 @@ op getStatus(@lroOperationId @path("jobId") id: string): Monitor; @post @route("/jobs") +@access(Access.internal, "csharp") @pollingOperation(getStatus) op begin(): { @statusCode code: 202; 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 index cb76d0c66cd..40c85c1c721 100644 --- 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 @@ -265,6 +265,7 @@ }, "optional": true, "readOnly": false, + "access": "internal", "discriminator": false, "flatten": false, "decorators": [], @@ -511,13 +512,13 @@ "kind": "lro", "name": "begin", "isExactName": false, - "accessibility": "public", + "accessibility": "internal", "apiVersions": [], "operation": { "$id": "35", "name": "begin", "isExactName": false, - "accessibility": "public", + "accessibility": "internal", "parameters": [], "responses": [ { diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs index b547ace0a53..58e26a1adf7 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs @@ -117,7 +117,9 @@ private PropertyProvider(InputProperty inputProperty, CSharpType propertyType, T Type = inputProperty.IsReadOnly ? propertyType.OutputType : propertyType; IsDiscriminator = IsDiscriminatorProperty(inputProperty); var hasOutputUsage = isOutputOnly || (inputProperty.EnclosingType?.Usage.HasFlag(InputModelTypeUsage.Output) ?? false); - Modifiers = IsDiscriminator || (!hasOutputUsage && _isRequiredNonNullableConstant) ? MethodSignatureModifiers.Internal : MethodSignatureModifiers.Public; + Modifiers = IsDiscriminator || (isOutputOnly && inputProperty.Access == "internal") || (!hasOutputUsage && _isRequiredNonNullableConstant) + ? MethodSignatureModifiers.Internal + : MethodSignatureModifiers.Public; var identifierName = inputProperty.IsExactName ? inputProperty.Name : inputProperty.Name.ToIdentifierName(); if (!inputProperty.IsExactName) { diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs index 127c25e417a..521d142afeb 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs @@ -129,6 +129,39 @@ public void OutputOnlyPropertyCanUseAModelTypeWithoutAnInputModelOwner() Assert.IsFalse(property.Body.HasSetter); } + [TestCase("internal", false, MethodSignatureModifiers.Internal)] + [TestCase("internal", true, MethodSignatureModifiers.Internal)] + [TestCase("public", false, MethodSignatureModifiers.Public)] + [TestCase(null, false, MethodSignatureModifiers.Public)] + public void OutputOnlyPropertyPreservesAccess(string? access, bool hasOwner, MethodSignatureModifiers expected) + { + var input = InputFactory.Property("details", InputPrimitiveType.String); + if (access != null) + { + input.Update(access: access); + } + if (hasOwner) + { + InputFactory.Model("Monitor", properties: [input]); + } + + var property = new PropertyProvider(input, new TestTypeProvider(), isOutputOnly: true); + + Assert.AreEqual(expected, property.Modifiers); + } + + [Test] + public void OrdinaryModelPropertyKeepsExistingAccessibility() + { + var input = InputFactory.Property("details", InputPrimitiveType.String); + input.Update(access: "internal"); + InputFactory.Model("Monitor", properties: [input]); + + var property = new PropertyProvider(input, new TestTypeProvider()); + + Assert.AreEqual(MethodSignatureModifiers.Public, property.Modifiers); + } + [Test] public void TestPropertyDocumentationPreservesCodeSpans() { From cf52ec169e58ae06f6f28d6db833542523a9a7f3 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 18 Nov 2026 16:22:23 -0500 Subject: [PATCH 5/7] fix(http-client-csharp): preserve LRO metadata type visibility Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../emitter/src/lib/client-model-builder.ts | 48 +++++++- .../test/Unit/operation-converter.test.ts | 109 ++++++++++++++++++ .../ModelReaderWriterContextDefinition.cs | 6 + ...ModelReaderWriterContextDefinitionTests.cs | 31 +++++ .../src/Providers/PropertyProvider.cs | 2 +- .../test/Providers/PropertyProviderTests.cs | 21 ++++ 6 files changed, 213 insertions(+), 4 deletions(-) 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 adb64612efc..d9c84eb85a2 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 @@ -15,6 +15,7 @@ import type { InputEnumType, InputLiteralType, InputModelType, + InputType, InputUnionType, } from "../type/input-type.js"; import { fromSdkClients } from "./client-converter.js"; @@ -83,7 +84,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") { @@ -95,9 +98,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); + } } } @@ -105,7 +116,13 @@ 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, sdkContext.__typeCache.lroMetadataEnums); + fixNamingConflicts( + models, + enums, + constants, + sdkContext.__typeCache.lroMetadataEnums, + typesBeforeClients, + ); // 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 @@ -169,13 +186,19 @@ function parseApiVersions( */ function fixNamingConflicts( models: InputModelType[], + enums: InputEnumType[], constants: InputLiteralType[], lroMetadataEnums: Set, + typesBeforeClients: Set, ): 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 @@ -218,6 +241,25 @@ function fixNamingConflicts( modelNameMap.set(key, 1); } } + + // Metadata enums must have distinct output file names without renaming preexisting types. + const usedTypeNames = new Set(models.map((model) => model.name.toLowerCase())); + for (const type of enums) { + if (!lroMetadataEnums.has(type) || typesBeforeClients.has(type)) { + usedTypeNames.add(type.name.toLowerCase()); + } + } + 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(type.name.toLowerCase())) { + type.name = `${baseName}${suffix++}`; + } + usedTypeNames.add(type.name.toLowerCase()); + } } /** 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 bcb78e27d68..9376faff28f 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 @@ -891,6 +891,115 @@ describe("Operation Converter", () => { }, ); + 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([ { optional: true, shareSdkType: false }, { optional: false, shareSdkType: false }, 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 3b65b3f7aea..309aca68e90 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 @@ -113,6 +113,12 @@ private static HashSet CollectLroMetadataTypes() 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)); 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 b579c6e2998..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 @@ -128,6 +128,37 @@ public void InternalLroMetadataModelHasBuildableContext(bool paging, bool collec 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() { diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs index 58e26a1adf7..f35b900a328 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs @@ -117,7 +117,7 @@ private PropertyProvider(InputProperty inputProperty, CSharpType propertyType, T Type = inputProperty.IsReadOnly ? propertyType.OutputType : propertyType; IsDiscriminator = IsDiscriminatorProperty(inputProperty); var hasOutputUsage = isOutputOnly || (inputProperty.EnclosingType?.Usage.HasFlag(InputModelTypeUsage.Output) ?? false); - Modifiers = IsDiscriminator || (isOutputOnly && inputProperty.Access == "internal") || (!hasOutputUsage && _isRequiredNonNullableConstant) + Modifiers = IsDiscriminator || (isOutputOnly && (inputProperty.Access == "internal" || !propertyType.IsPublic)) || (!hasOutputUsage && _isRequiredNonNullableConstant) ? MethodSignatureModifiers.Internal : MethodSignatureModifiers.Public; var identifierName = inputProperty.IsExactName ? inputProperty.Name : inputProperty.Name.ToIdentifierName(); diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs index 521d142afeb..4a9b83f150c 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs @@ -150,6 +150,27 @@ public void OutputOnlyPropertyPreservesAccess(string? access, bool hasOwner, Met Assert.AreEqual(expected, property.Modifiers); } + [TestCase("internal", true, false, MethodSignatureModifiers.Internal)] + [TestCase("internal", false, false, MethodSignatureModifiers.Internal)] + [TestCase("internal", false, true, MethodSignatureModifiers.Internal)] + [TestCase("public", true, false, MethodSignatureModifiers.Public)] + [TestCase("public", false, false, MethodSignatureModifiers.Public)] + public void OutputOnlyPropertyDoesNotExposeNonPublicTypes(string access, bool isEnum, bool isCollection, MethodSignatureModifiers expected) + { + InputType type = isEnum + ? InputFactory.StringEnum("MonitorMode", [("Ready", "ready")], access: access, isExtensible: true) + : InputFactory.Model("Details", access: access); + if (isCollection) + { + type = InputFactory.Array(type); + } + var input = InputFactory.Property("details", type); + + var property = new PropertyProvider(input, new TestTypeProvider(), isOutputOnly: true); + + Assert.AreEqual(expected, property.Modifiers); + } + [Test] public void OrdinaryModelPropertyKeepsExistingAccessibility() { From 2a7b05086535860af3d09e4790aa19e4fd40a39e Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 18 Nov 2026 17:10:45 -0500 Subject: [PATCH 6/7] fix(http-client-csharp): avoid metadata enum name collisions Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../emitter/src/lib/client-model-builder.ts | 35 ++- .../test/Unit/operation-converter.test.ts | 243 ++++++++++++++++++ 2 files changed, 273 insertions(+), 5 deletions(-) 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 d9c84eb85a2..2705d1d41f6 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 @@ -12,6 +12,7 @@ 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, @@ -122,6 +123,7 @@ export function createModel(sdkContext: CSharpEmitterContext): [CodeModel, reado constants, sdkContext.__typeCache.lroMetadataEnums, typesBeforeClients, + sdkContext.__typeCache.clients.values(), ); // Resolve the root namespace. When this cannot be determined the generated code model @@ -190,6 +192,7 @@ function fixNamingConflicts( 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 @@ -242,11 +245,20 @@ function fixNamingConflicts( } } - // Metadata enums must have distinct output file names without renaming preexisting types. - const usedTypeNames = new Set(models.map((model) => model.name.toLowerCase())); + // Metadata enums 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(type.name.toLowerCase()); + usedTypeNames.add(csharpTypeNameKey(type)); } } const metadataEnums = enums @@ -255,13 +267,26 @@ function fixNamingConflicts( for (const type of metadataEnums) { const baseName = type.name; let suffix = 1; - while (usedTypeNames.has(type.name.toLowerCase())) { + while (usedTypeNames.has(csharpTypeNameKey(type))) { type.name = `${baseName}${suffix++}`; } - usedTypeNames.add(type.name.toLowerCase()); + 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(); +} + /** * Dedupe key for a model or enum. Uses `crossLanguageDefinitionId` when present; * otherwise falls back to `anon:${name}` since TCGC may report inconsistent 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 9376faff28f..c1a74bfd878 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 @@ -1000,6 +1000,249 @@ describe("Operation Converter", () => { }, ); + 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 }, From ffa8e1f9b33ada0147d7ee92d1f93c07e5e79a98 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Thu, 19 Nov 2026 11:57:46 -0500 Subject: [PATCH 7/7] refactor(http-client-csharp): address LRO metadata review feedback Remove the public PropertyProvider isOutputOnly overload, use Enum.TryParse for operation link locations and path styles, separate LRO metadata enum usage from naming, clarify context registration, and inline the fixture TypeSpec source in the emitter test. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../emitter/src/lib/client-model-builder.ts | 10 +- .../test/Unit/operation-converter.test.ts | 64 ++++++++++- .../ModelReaderWriterContextDefinition.cs | 11 +- .../InputOperationLinkConverter.cs | 13 +-- .../InputPathParameterConverter.cs | 12 +- .../LoadsEmittedLroMetadata/main.tsp | 58 ---------- .../src/Providers/PropertyProvider.cs | 21 +--- .../test/Providers/PropertyProviderTests.cs | 107 ------------------ 8 files changed, 90 insertions(+), 206 deletions(-) delete mode 100644 packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/main.tsp 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 2705d1d41f6..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 @@ -125,6 +125,10 @@ export function createModel(sdkContext: CSharpEmitterContext): [CodeModel, reado 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 @@ -209,9 +213,6 @@ function fixNamingConflicts( } else if (type.kind === "enum" && type.crossLanguageDefinitionId === "") { // Fix enum names for enums created from constants normalizeLiteralTypeForProperty(type, model, property.name); - if (lroMetadataEnums.has(type)) { - type.usage |= UsageFlags.Output | UsageFlags.Json; - } } } } @@ -245,7 +246,8 @@ function fixNamingConflicts( } } - // Metadata enums must have distinct C# type and output file names without renaming existing types. + // 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); 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 c1a74bfd878..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 @@ -14,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; @@ -26,7 +88,7 @@ describe("Operation Converter", () => { "../../../generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/", import.meta.url, ); - runner.addTypeSpecFile("main.tsp", readFileSync(new URL("main.tsp", fixture), "utf8")); + runner.addTypeSpecFile("main.tsp", lroMetadataFixtureSource); await runner.compile("./", { noEmit: true }); const sdkContext = await createCSharpSdkContext(createEmitterContext(runner.program)); const [root, diagnostics] = createModel(sdkContext); 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 309aca68e90..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 @@ -71,10 +71,7 @@ protected override IReadOnlyList BuildAttributes() } foreach (var provider in buildableProviders) { - if ((!IsPublicApi(provider) - && (!provider.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal) - || provider.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Private) - || !lroMetadataTypes.Contains(provider.Type))) + if (!(IsPublicApi(provider) || IsInternalLroMetadataType(provider, lroMetadataTypes)) || !ShouldWriteProvider(provider) || customizedBuildableTypes.Contains(GetTypeIdentity(provider.Type))) { @@ -95,6 +92,12 @@ 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); 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 index f31f64c2e0c..fb2aa3e820b 100644 --- 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 @@ -27,14 +27,13 @@ public override InputOperationLink Read(ref Utf8JsonReader reader, Type typeToCo } } - var parsedLocation = location switch + if (location is null) { - nameof(InputOperationLinkLocation.ResponseHeader) => InputOperationLinkLocation.ResponseHeader, - nameof(InputOperationLinkLocation.ResponseBody) => InputOperationLinkLocation.ResponseBody, - nameof(InputOperationLinkLocation.Self) => InputOperationLinkLocation.Self, - null => throw new JsonException("Operation link must have a location."), - _ => InputOperationLinkLocation.Unknown - }; + 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.")); 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 4fedd03c6e8..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 @@ -117,15 +117,9 @@ internal static InputPathParameter ReadInputPathParameter(ref Utf8JsonReader rea parameter.DefaultValue = defaultValue; parameter.Scope = InputParameter.ParseScope(type, name, scope); parameter.Explode = explode; - parameter.Style = style switch - { - "simple" => InputPathParameterStyle.Simple, - "label" => InputPathParameterStyle.Label, - "matrix" => InputPathParameterStyle.Matrix, - "path" => InputPathParameterStyle.Path, - "fragment" => InputPathParameterStyle.Fragment, - _ => InputPathParameterStyle.Unknown, - }; + 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/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/main.tsp b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/main.tsp deleted file mode 100644 index 75fd6f5fe87..00000000000 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/test/TestData/InputServiceMethodTests/LoadsEmittedLroMetadata/main.tsp +++ /dev/null @@ -1,58 +0,0 @@ -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; -}; diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs index f35b900a328..4d623af6ef6 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PropertyProvider.cs @@ -81,23 +81,14 @@ internal static bool TryCreate(InputProperty inputProperty, TypeProvider enclosi } public PropertyProvider(InputProperty inputProperty, TypeProvider enclosingType) - : this(inputProperty, enclosingType, false) - { - } - - /// - /// Creates a property, optionally treating the input as output-only even when it has no owning input model. - /// - public PropertyProvider(InputProperty inputProperty, TypeProvider enclosingType, bool isOutputOnly) : this( inputProperty, CodeModelGenerator.Instance.TypeFactory.CreateCSharpType(inputProperty.Type) ?? throw new InvalidOperationException($"Could not create CSharpType for property {inputProperty.Name}"), - enclosingType, - isOutputOnly) + enclosingType) { } - private PropertyProvider(InputProperty inputProperty, CSharpType propertyType, TypeProvider enclosingType, bool isOutputOnly = false) + private PropertyProvider(InputProperty inputProperty, CSharpType propertyType, TypeProvider enclosingType) { InputProperty = inputProperty; if (!inputProperty.IsRequired && !propertyType.IsCollection) @@ -108,7 +99,7 @@ private PropertyProvider(InputProperty inputProperty, CSharpType propertyType, T EnclosingType = enclosingType; _serializationFormat = CodeModelGenerator.Instance.TypeFactory.GetSerializationFormat(inputProperty); _isRequiredNonNullableConstant = inputProperty.IsRequired && propertyType is { IsLiteral: true, IsNullable: false }; - var propHasSetter = !isOutputOnly && PropertyHasSetter(propertyType, inputProperty); + var propHasSetter = PropertyHasSetter(propertyType, inputProperty); MethodSignatureModifiers setterModifier = !propHasSetter ? MethodSignatureModifiers.None : inputProperty.IsReadOnly && !IsDiscriminatorProperty(inputProperty) ? MethodSignatureModifiers.Private | MethodSignatureModifiers.Protected @@ -116,10 +107,8 @@ private PropertyProvider(InputProperty inputProperty, CSharpType propertyType, T Type = inputProperty.IsReadOnly ? propertyType.OutputType : propertyType; IsDiscriminator = IsDiscriminatorProperty(inputProperty); - var hasOutputUsage = isOutputOnly || (inputProperty.EnclosingType?.Usage.HasFlag(InputModelTypeUsage.Output) ?? false); - Modifiers = IsDiscriminator || (isOutputOnly && (inputProperty.Access == "internal" || !propertyType.IsPublic)) || (!hasOutputUsage && _isRequiredNonNullableConstant) - ? MethodSignatureModifiers.Internal - : MethodSignatureModifiers.Public; + var hasOutputUsage = inputProperty.EnclosingType?.Usage.HasFlag(InputModelTypeUsage.Output) ?? false; + Modifiers = IsDiscriminator || (!hasOutputUsage && _isRequiredNonNullableConstant) ? MethodSignatureModifiers.Internal : MethodSignatureModifiers.Public; var identifierName = inputProperty.IsExactName ? inputProperty.Name : inputProperty.Name.ToIdentifierName(); if (!inputProperty.IsExactName) { diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs index 4a9b83f150c..f7e823f5786 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/PropertyProviderTests.cs @@ -76,113 +76,6 @@ public void TestSnakeCaseProperty() Assert.AreEqual("Description for snake_case", property.Description!.ToString()); } - [TestCase(false)] - [TestCase(true)] - public void OutputOnlyPropertyCanBeCreatedWithoutAnInputModel(bool isReadOnly) - { - var input = InputFactory.Property( - "StartTime", - new InputDateTimeType( - DateTimeKnownEncoding.Rfc7231, - "utcDateTime", - "TypeSpec.utcDateTime", - InputPrimitiveType.String), - isReadOnly: isReadOnly, - wireName: "started_at"); - - var property = new PropertyProvider(input, new TestTypeProvider(), isOutputOnly: true); - - Assert.IsNull(input.EnclosingType); - Assert.AreEqual("StartsOn", property.Name); - Assert.AreEqual(new CSharpType(typeof(DateTimeOffset)).WithNullable(true), property.Type); - Assert.AreEqual(SerializationFormat.DateTime_RFC7231, property.SerializationFormat); - Assert.AreEqual("started_at", property.WireInfo?.SerializedName); - Assert.IsFalse(property.Body.HasSetter); - } - - [Test] - public void OutputOnlyPropertyPreservesPropertyLevelArrayEncoding() - { - var input = InputFactory.Property( - "items", - InputFactory.Array(InputPrimitiveType.String), - encode: ArrayKnownEncoding.PipeDelimited); - - var property = new PropertyProvider(input, new TestTypeProvider(), isOutputOnly: true); - - Assert.IsNull(input.EnclosingType); - Assert.AreEqual(SerializationFormat.Array_PipeDelimited, property.SerializationFormat); - Assert.IsFalse(property.Body.HasSetter); - } - - [Test] - public void OutputOnlyPropertyCanUseAModelTypeWithoutAnInputModelOwner() - { - var details = InputFactory.Model("Details", properties: []); - var input = InputFactory.Property("details", details, isReadOnly: true); - - var property = new PropertyProvider(input, new TestTypeProvider(), isOutputOnly: true); - - Assert.IsNull(input.EnclosingType); - Assert.AreEqual("Details", property.Type.Name); - Assert.IsTrue(property.Type.IsNullable); - Assert.IsFalse(property.Body.HasSetter); - } - - [TestCase("internal", false, MethodSignatureModifiers.Internal)] - [TestCase("internal", true, MethodSignatureModifiers.Internal)] - [TestCase("public", false, MethodSignatureModifiers.Public)] - [TestCase(null, false, MethodSignatureModifiers.Public)] - public void OutputOnlyPropertyPreservesAccess(string? access, bool hasOwner, MethodSignatureModifiers expected) - { - var input = InputFactory.Property("details", InputPrimitiveType.String); - if (access != null) - { - input.Update(access: access); - } - if (hasOwner) - { - InputFactory.Model("Monitor", properties: [input]); - } - - var property = new PropertyProvider(input, new TestTypeProvider(), isOutputOnly: true); - - Assert.AreEqual(expected, property.Modifiers); - } - - [TestCase("internal", true, false, MethodSignatureModifiers.Internal)] - [TestCase("internal", false, false, MethodSignatureModifiers.Internal)] - [TestCase("internal", false, true, MethodSignatureModifiers.Internal)] - [TestCase("public", true, false, MethodSignatureModifiers.Public)] - [TestCase("public", false, false, MethodSignatureModifiers.Public)] - public void OutputOnlyPropertyDoesNotExposeNonPublicTypes(string access, bool isEnum, bool isCollection, MethodSignatureModifiers expected) - { - InputType type = isEnum - ? InputFactory.StringEnum("MonitorMode", [("Ready", "ready")], access: access, isExtensible: true) - : InputFactory.Model("Details", access: access); - if (isCollection) - { - type = InputFactory.Array(type); - } - var input = InputFactory.Property("details", type); - - var property = new PropertyProvider(input, new TestTypeProvider(), isOutputOnly: true); - - Assert.AreEqual(expected, property.Modifiers); - } - - [Test] - public void OrdinaryModelPropertyKeepsExistingAccessibility() - { - var input = InputFactory.Property("details", InputPrimitiveType.String); - input.Update(access: "internal"); - InputFactory.Model("Monitor", properties: [input]); - - var property = new PropertyProvider(input, new TestTypeProvider()); - - Assert.AreEqual(MethodSignatureModifiers.Public, property.Modifiers); - } - [Test] public void TestPropertyDocumentationPreservesCodeSpans() {