Skip to content

Fix C# back-compat service method overloads for reserved param names; suppress AZC0002 when previous method ends in CancellationToken#10642

Merged
jorgerangel-msft merged 9 commits into
mainfrom
copilot/fix-reserved-param-names
May 13, 2026
Merged

Fix C# back-compat service method overloads for reserved param names; suppress AZC0002 when previous method ends in CancellationToken#10642
jorgerangel-msft merged 9 commits into
mainfrom
copilot/fix-reserved-param-names

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 11, 2026

Back-compat overloads generated for service methods (added in 72a95c2) produced uncompilable C# when previous parameters had raw input names that are not valid C# identifiers (e.g. OData query parameters such as $select, $top, $skip, $count). They also could not be emitted correctly when the previous method ended in a CancellationToken parameter — making CT optional introduces a CS0121 ambiguity at basic call sites with the new method, and making CT required violates the SDK guideline that CancellationToken parameters must be optional.

Changes

  • ClientProvider.BuildBackCompatOverloadForNewOptionalParameters — emit named-argument labels using the C# variable name (select:, top:) rather than the raw input name ($select:, $top:). Match previous→current parameters by ToVariableName() so renames that differ only by reserved characters still align.
  • ClientProvider.ProcessBackCompatForNewOptionalParameters — when the previous method's trailing parameter is a CancellationToken, the back-compat overload is still emitted (with the trailing CT required, to avoid a CS0121 ambiguity with the new method) but is wrapped in #pragma warning disable AZC0002 / #pragma warning restore AZC0002 so the SDK analyzer rule that requires CancellationToken parameters to be optional does not fire on the hidden compatibility overload. The trailing-parameter type comparison uses CSharpType.CSharpTypeIgnoreNullableComparer so nullability is ignored.
  • Tests — the four existing back-compat baselines now include the back-compat overloads with the AZC0002 pragma wrappers around them. Added a new BackCompatibility_NewOptionalParameterWithReservedName test (with testdata files) that exercises the reserved-name fix via a previous-contract protocol method (ending in RequestOptions, so no AZC0002 suppression is required) plus a new $select query parameter, verifying the generated back-compat overload uses @select: default rather than the buggy $select: default.

Example (reserved-name fix)

Before:

[EditorBrowsable(EditorBrowsableState.Never)]
public virtual AsyncPageable<SearchIndexResponse> GetIndexesWithSelectedPropertiesAsync(
    IEnumerable<string> @select, CancellationToken cancellationToken)
{
    return this.GetIndexesWithSelectedPropertiesAsync(
        $select: default, $top: default, $skip: default, $count: default,
        cancellationToken: cancellationToken);
}

After:

#pragma warning disable AZC0002 // Back-compat overload preserves the previous method signature where CancellationToken was the trailing parameter. Making it optional would introduce an ambiguous call with the new method.
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual AsyncPageable<SearchIndexResponse> GetIndexesWithSelectedPropertiesAsync(
    IEnumerable<string> @select, CancellationToken cancellationToken)
{
    return this.GetIndexesWithSelectedPropertiesAsync(
        select: default, top: default, skip: default, count: default,
        cancellationToken: cancellationToken);
}
#pragma warning restore AZC0002 // ...

… preserve optional CancellationToken

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/09ec7e5f-2d2f-4a58-8662-7d148879bb09

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label May 11, 2026
… on non-trailing params

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/09ec7e5f-2d2f-4a58-8662-7d148879bb09

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix generated back compat service methods for reserved param names Fix C# back-compat service method overloads for reserved param names and CancellationToken May 11, 2026
Copilot AI requested a review from jorgerangel-msft May 11, 2026 19:51
Comment thread packages/http-client-csharp/generator/docs/backward-compatibility.md Outdated
…; use testdata for new test

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/3f9baf01-88a8-4b30-a7ca-d0a5b5897fe4

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Copilot AI changed the title Fix C# back-compat service method overloads for reserved param names and CancellationToken Fix C# back-compat service method overloads for reserved param names May 11, 2026
Copilot AI requested a review from jorgerangel-msft May 11, 2026 20:13
Copilot AI changed the title Fix C# back-compat service method overloads for reserved param names Fix C# back-compat service method overloads for reserved param names; skip overload when previous method ends in CancellationToken May 11, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 11, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@10642

commit: e89375b

@JonathanCrd JonathanCrd marked this pull request as ready for review May 11, 2026 23:50
Copy link
Copy Markdown
Contributor

@jorgerangel-msft jorgerangel-msft left a comment

Choose a reason for hiding this comment

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

Can we also please get a regen preview? It might also be worth running the debug profile script against Azure/azure-sdk-for-net#59156 to verify the changes

…reserved-name test

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/439261fa-3acd-4492-94bc-8c1fbecb77f7

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
…ing when previous method ends with CancellationToken

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/cfe7f3df-2dc7-4049-b5a3-48583727e070

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Copilot AI changed the title Fix C# back-compat service method overloads for reserved param names; skip overload when previous method ends in CancellationToken Fix C# back-compat service method overloads for reserved param names; suppress AZC0002 when previous method ends in CancellationToken May 12, 2026
Copilot AI requested a review from jorgerangel-msft May 12, 2026 23:57
@jorgerangel-msft jorgerangel-msft added this pull request to the merge queue May 13, 2026
Merged via the queue into main with commit afc3dbb May 13, 2026
29 checks passed
@jorgerangel-msft jorgerangel-msft deleted the copilot/fix-reserved-param-names branch May 13, 2026 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated Back Compat Service Methods Do Not Compile If Params Have Reserved Names

4 participants