Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .chronus/changes/dotnet-shared-lro-metadata-2026-11-12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@typespec/http-client-csharp"
---

Preserve TCGC `@lroStatusMetadata` selections, typed `propertySegments`, and ordered `@lroOperationId` sources in the shared .NET input model for Azure generator consumption. Existing generated clients remain unchanged when this metadata is absent.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -54,6 +55,7 @@ import type {
InputLongRunningPagingServiceMethod,
InputLongRunningServiceMetadata,
InputLongRunningServiceMethod,
InputLroOperationIdMetadata,
InputNextLink,
InputPagingServiceMetadata,
InputPagingServiceMethod,
Expand All @@ -79,7 +81,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(
Expand Down Expand Up @@ -726,6 +728,58 @@ function loadLongRunningMetadata(
): [InputLongRunningServiceMetadata, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();

const metadata = method.lroMetadata;
const statusMetadata = metadata.statusMetadata?.length
? metadata.statusMetadata.map((entry) => ({
property: diagnostics.pipe(fromSdkModelProperty(sdkContext, entry.property)),
propertySegments: entry.propertySegments.map((property) =>
diagnostics.pipe(fromSdkModelProperty(sdkContext, property)),
),
}))
: undefined;
const operationId = metadata.operationId?.length
? metadata.operationId.map((entry): InputLroOperationIdMetadata => {
if (entry.kind === "property") {
return {
kind: "property",
property: diagnostics.pipe(fromSdkModelProperty(sdkContext, entry.property)),
propertySegments: entry.propertySegments.map((property) =>
diagnostics.pipe(fromSdkModelProperty(sdkContext, property)),
),
};
}
const step = metadata.statusMonitorStep;
const operation =
step?.kind === "nextOperationReference" ? step.target.operation : step?.operation;
compilerAssert(operation, "An LRO operation ID path requires a linked polling operation.");
const parameter = diagnostics.pipe(
fromParameter(sdkContext, entry.pathParameter, method.apiVersions),
);
compilerAssert(
parameter?.kind === "path",
"An LRO operation ID requires a path parameter.",
);
const sourceProperty = diagnostics.pipe(
fromSdkModelProperty(sdkContext, entry.source.property),
);
if (entry.source.location === "ResponseHeader" && entry.source.property.__raw) {
sourceProperty.serializedName = getHeaderFieldName(
sdkContext.program,
entry.source.property.__raw,
);
}
return {
kind: "path",
source: {
location: entry.source.location,
property: sourceProperty,
},
pathParameter: parameter,
path: operation.path,
};
})
: undefined;

return diagnostics.wrap({
finalStateVia: convertLroFinalStateVia(method.lroMetadata.finalStateVia),
finalResponse: {
Expand All @@ -740,6 +794,8 @@ function loadLongRunningMetadata(
: undefined,
} as OperationResponse,
resultPath: method.lroMetadata.finalResultPath,
statusMetadata,
operationId,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,12 @@ function fromSdkModelType(
return diagnostics.wrap(inputModelType);
}

function fromSdkModelProperty(
export function fromSdkModelProperty(
sdkContext: CSharpEmitterContext,
sdkProperty: SdkModelPropertyType,
sdkModel: SdkModelType,
): [InputModelProperty | undefined, readonly Diagnostic[]] {
sdkModel?: SdkModelType,
): [InputModelProperty, readonly Diagnostic[]] {
const diagnostics = createDiagnosticCollector();
// TODO -- this returns undefined because some properties we do not support yet.
let property = sdkContext.__typeCache.properties.get(sdkProperty) as
InputModelProperty | undefined;
if (property) {
Expand All @@ -316,7 +315,7 @@ function fromSdkModelProperty(
? getDeprecated(sdkContext.program, sdkProperty.__raw)
: undefined,
type: diagnostics.pipe(
fromSdkType(sdkContext, sdkProperty.type, sdkProperty, sdkModel.namespace),
fromSdkType(sdkContext, sdkProperty.type, sdkProperty, sdkModel?.namespace),
),
optional: sdkProperty.optional,
readOnly: isReadOnly(sdkProperty),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -75,8 +81,31 @@ export interface InputLongRunningServiceMetadata {
finalStateVia: OperationFinalStateVia;
finalResponse: OperationResponse;
resultPath?: string;
statusMetadata?: InputLroStatusMetadata[];
operationId?: InputLroOperationIdMetadata[];
}

export interface InputLroStatusMetadata {
property: InputModelProperty;
propertySegments: InputModelProperty[];
}

export type InputLroOperationIdMetadata =
| {
kind: "path";
source: {
location: "ResponseHeader" | "ResponseBody" | "Self";
property: InputModelProperty;
};
pathParameter: InputPathParameter;
path: string;
}
| {
kind: "property";
property: InputModelProperty;
propertySegments: InputModelProperty[];
};

export interface InputLongRunningPagingServiceMethod extends InputServiceMethodBase {
kind: "lropaging";
lroMetadata: InputLongRunningServiceMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,91 @@ describe("Operation Converter", () => {
runner = await createEmitterTestHost();
});

it.each([true, false])("preserves selected LRO metadata: %s", async (selected) => {
const program = await typeSpecCompile(
`
model Result { value: string; }
model Usage { tokens: int32; }
enum OperationState { Running, Succeeded, Failed, Canceled }
model MonitorBase {
${selected ? '@lroStatusMetadata @clientName("usageDetails")' : ""}
@encodedName("application/json", "usage_details")
usage?: Usage;
}
model Monitor extends MonitorBase {
${selected ? "@lroOperationId" : ""}
@encodedName("application/json", "operation_id")
id: string;
@lroStatus status: OperationState;
@lroResult result?: Result;
}
@get @route("/jobs/{jobId}")
op getStatus(${selected ? "@lroOperationId" : ""} @path jobId: string): Monitor;
@pollingOperation(getStatus)
@post @route("/jobs")
op begin(): {
@statusCode code: 202;
@pollingLocation @header("Operation-Location") location: url;
};
`,
runner,
{ IsAzureCoreNeeded: true, IsTCGCNeeded: true },
);
strictEqual(program.diagnostics.filter((d) => d.severity === "error").length, 0);
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;
const serialized = JSON.parse(serializeCodeModel(sdkContext, root));
const serializedMetadata = serialized.clients[0].methods.find(
(m: { name: string }) => m.name === "begin",
).lroMetadata;
if (!selected) {
strictEqual(serializedMetadata.statusMetadata, undefined);
strictEqual(serializedMetadata.operationId, undefined);
return;
}

const base = root.models.find((m) => m.name === "MonitorBase")!;
const monitor = root.models.find((m) => m.name === "Monitor")!;
const usage = base.properties.find((p) => p.name === "usageDetails")!;
strictEqual(metadata.statusMetadata?.[0].property, usage);
deepStrictEqual(metadata.statusMetadata[0].propertySegments, [usage]);
strictEqual(usage.serializedName, "usage_details");
strictEqual(usage.optional, true);
deepStrictEqual(
metadata.operationId?.map((id) => id.kind),
["path", "property"],
);
const path = metadata.operationId[0];
ok(path.kind === "path");
strictEqual(path.path, "/jobs/{jobId}");
strictEqual(path.source.location, "ResponseHeader");
strictEqual(path.source.property.serializedName, "Operation-Location");
deepStrictEqual(
path.pathParameter,
root.clients[0].methods
.find((m) => m.name === "getStatus")!
.operation.parameters.find((p) => p.name === "jobId"),
);
const body = metadata.operationId[1];
ok(body.kind === "property");
strictEqual(
body.property,
monitor.properties.find((p) => p.name === "id"),
);
deepStrictEqual(body.propertySegments, [body.property]);
strictEqual(body.property.serializedName, "operation_id");
ok(serializedMetadata.statusMetadata[0].property.$ref);
deepStrictEqual(serializedMetadata.statusMetadata[0].propertySegments, [
serializedMetadata.statusMetadata[0].property,
]);
ok(serializedMetadata.operationId[0].pathParameter.$id);
ok(serializedMetadata.operationId[1].property.$ref);
});

it.each([undefined, "", 'Use "read" instead.\nSee C:\\sdk.'])(
"preserves operation deprecation metadata: %s",
async (message) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Linq;

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<InputLroStatusMetadata> statusMetadata,
IReadOnlyList<InputLroOperationIdMetadata> operationId)
{
FinalStateVia = finalStateVia;
FinalResponse = finalResponse;
ResultPath = resultPath;
StatusMetadata = statusMetadata;
OperationId = operationId;
}

public InputLongRunningServiceMetadata() : this(1, new InputOperationResponse(), null) { }

public int FinalStateVia { get; }
public InputOperationResponse FinalResponse { get; }
public string? ResultPath { get; }
public IReadOnlyList<InputLroStatusMetadata> StatusMetadata { get; }
public IReadOnlyList<InputLroOperationIdMetadata> OperationId { get; }

/// <summary>
/// Meaningful return type of the long running operation.
Expand Down
Original file line number Diff line number Diff line change
@@ -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() { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 path)
{
Source = source;
PathParameter = pathParameter;
Path = path;
}

public InputOperationLink Source { get; }
public InputPathParameter PathParameter { get; }
/// <summary>The linked polling operation's route template, not the polling URL received at runtime.</summary>
public string Path { get; }
}
}
Original file line number Diff line number Diff line change
@@ -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<InputModelProperty> propertySegments)
{
Property = property;
PropertySegments = propertySegments;
}

public InputModelProperty Property { get; }
public IReadOnlyList<InputModelProperty> PropertySegments { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;

namespace Microsoft.TypeSpec.Generator.Input
{
/// <summary>
/// Auxiliary values selected from the latest received status-monitor response, including failed or canceled responses.
/// Values are not persisted in continuation tokens and may be absent until a response is received.
/// </summary>
public sealed class InputLroStatusMetadata
{
public InputLroStatusMetadata(InputModelProperty property, IReadOnlyList<InputModelProperty> propertySegments)
{
Property = property;
PropertySegments = propertySegments;
}

public InputModelProperty Property { get; }
/// <summary>Typed property path rooted at the status-monitor response model.</summary>
public IReadOnlyList<InputModelProperty> PropertySegments { get; }
}
}
Loading
Loading