Skip to content

[http-client-csharp] Fix partial method customization rebinding#11246

Open
ArcturusZhang wants to merge 3 commits into
microsoft:mainfrom
ArcturusZhang:arcturus/fix-partial-method-customization
Open

[http-client-csharp] Fix partial method customization rebinding#11246
ArcturusZhang wants to merge 3 commits into
microsoft:mainfrom
ArcturusZhang:arcturus/fix-partial-method-customization

Conversation

@ArcturusZhang

Copy link
Copy Markdown
Member

Fixes #11245

This fixes partial method signature customization in the C# generator:

  • rebinds generated body references when late customization renames parameters;
  • emits method modifiers in valid C# order for virtual partial methods;
  • preserves generated async on partial implementations;
  • keeps implementation parameters required while leaving optional metadata on the custom partial declaration.

Validation:

  • dotnet test packages\http-client-csharp\generator\Microsoft.TypeSpec.Generator\test\Microsoft.TypeSpec.Generator.Tests.csproj --filter "FullyQualifiedName~OutputLibraryVisitorTests.PartialMethodCustomizationRenamesGeneratedBodyReferences|FullyQualifiedName~CodeWriterTests.CodeWriter_WriteMethodDeclaration_WithVirtualPartialModifier" --no-restore
  • dotnet test packages\http-client-csharp\generator\Microsoft.TypeSpec.Generator.ClientModel\test\Microsoft.TypeSpec.Generator.ClientModel.Tests.csproj --filter "FullyQualifiedName~ClientProviderCustomizationTests" --no-restore

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: a8ef33da-e40d-4f79-86d7-7d947641e6b4
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: a8ef33da-e40d-4f79-86d7-7d947641e6b4
@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 Jul 14, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: a8ef33da-e40d-4f79-86d7-7d947641e6b4
@pkg-pr-new

pkg-pr-new Bot commented Jul 14, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: e4b1002

@github-actions

Copy link
Copy Markdown
Contributor

No changes needing a change description found.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes C# generator partial method signature customization so that customized parameter names correctly rebind into already-generated method bodies, while also addressing C# modifier ordering and async preservation for partial implementations (per #11245).

Changes:

  • Rebinds generated method-body parameter references when late customization renames parameters (by renaming ParameterProviders in-place rather than cloning).
  • Fixes method modifier emission order so virtual partial is emitted in a valid order, with a dedicated unit test.
  • Preserves generated async on partial implementations while keeping implementation parameters required (no defaults), even when custom partial declarations include optional metadata.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Writers/CodeWriterTests.cs Adds a regression test asserting public virtual partial ... modifier ordering.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/TestData/OutputLibraryVisitorTests/PartialMethodCustomizationRenamesGeneratedBodyReferences/TestName.cs Adds custom partial declaration test input that renames a parameter and includes virtual partial.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/OutputLibraryVisitorTests.cs Adds regression test ensuring body references use customized parameter names and defaults are removed from implementations.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Writers/CodeWriter.cs Adjusts modifier emission to place partial after method-specific modifiers like virtual.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/TypeProvider.cs Switches to in-place parameter renaming for partial method customization and preserves generated async.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/PartialMethodCustomization.cs Introduces RenameParametersInPlace helper to avoid breaking cached body references.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs Threads generated return type/modifiers into partial signature building for protocol/convenience SCM methods.

Comment on lines +171 to +187
public static IReadOnlyList<ParameterProvider> RenameParametersInPlace(
IReadOnlyList<ParameterProvider> generatorParameters,
IReadOnlyList<ParameterProvider> customParameters,
bool removeDefaults)
{
for (int i = 0; i < generatorParameters.Count && i < customParameters.Count; i++)
{
var generatedParameter = generatorParameters[i];
generatedParameter.Update(name: customParameters[i].Name);
if (removeDefaults)
{
generatedParameter.DefaultValue = null;
}
}

return generatorParameters;
}
@jorgerangel-msft

Copy link
Copy Markdown
Contributor

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.

[http-client-csharp] Fix partial method customization parameter rebinding

3 participants