Skip to content

Fix partial method customization parameter renaming#11058

Merged
JoshLove-msft merged 4 commits into
microsoft:mainfrom
m-nash:fix/partial-method-param-rename
Jun 23, 2026
Merged

Fix partial method customization parameter renaming#11058
JoshLove-msft merged 4 commits into
microsoft:mainfrom
m-nash:fix/partial-method-param-rename

Conversation

@m-nash

@m-nash m-nash commented Jun 23, 2026

Copy link
Copy Markdown
Member

Problem

When a hand-written partial method declaration customizes a generated method (the documented "Customize a generated client method's signature" feature), the generated partial implementation comes out with parameter names that don't compile.

TypeProvider.CreatePartialMethodFromCustomSignature passed customSignature.Parameters as both arguments to RenameAndCloneParameters. The intended contract is (generatorParameters, customParameters): the first carries the generator metadata and the parameter declarations referenced by the method body and XML docs; the second only supplies the names.

Because the custom parameters were used for the signature, the signature ended up referencing different ParameterProvider/CodeWriterDeclaration instances than the body and XML docs. The writer's name de-duplication then appended a numeric suffix, e.g.:

private static partial void MyMethod(string input)
{
    input0.ToString(); // signature says `input`, body says `input0`
}

Fix

Pass generatedMethod.Signature.Parameters as the generator parameters so the signature, body, and XML docs all share the same parameter declarations. This matches the contract documented on RenameAndCloneParameters and the existing correct usage in ScmMethodProviderCollection.BuildConvenienceMethod.

Tests

  • New regression test CustomPartialMethodImplementationKeepsParameterNames (fails before the fix showing input0, passes after).
  • Full suites green: 1543 Microsoft.TypeSpec.Generator tests + 1436 Microsoft.TypeSpec.Generator.ClientModel tests.

Docs

Added a note to customization.md: types generated into the same assembly must be fully qualified with global:: in a partial method declaration, since they are unresolved symbols when the declaration is read.

CreatePartialMethodFromCustomSignature passed the custom signature's
parameters as the generator parameters, so the generated partial
implementation's signature referenced different parameter declarations
than its body and XML docs. The writer's name de-duplication then
appended a numeric suffix (e.g. input0) to the signature parameters,
producing code that does not compile.

Pass the generated method's parameters as the generator parameters so
the signature, body, and XML docs share the same parameter declarations.

Also document that types generated into the same assembly must be fully
qualified with global:: in a partial method declaration.

Co-authored-by: Copilot <223556219+Copilot@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 Jun 23, 2026
@m-nash m-nash requested a review from Copilot June 23, 2026 17:46

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

Fixes a C# generator bug where customizing a generated method via a hand-written partial declaration could produce mismatched parameter declarations between the signature and body (leading to de-duplication suffixes like input0 and resulting compilation failures). This aligns TypeProvider.CreatePartialMethodFromCustomSignature with the intended RenameAndCloneParameters(generatorParameters, customParameters) contract and adds a regression test + documentation guidance for partial signatures.

Changes:

  • Fix partial-method parameter cloning to use the generated method’s parameter providers as the metadata/declaration source.
  • Add a regression test + custom-code fixture ensuring generated partial implementations keep body/signature parameter names aligned.
  • Document a global:: qualification requirement when partial declarations reference types generated into the same assembly.

Reviewed changes

Copilot reviewed 5 out of 5 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/Providers/TypeProviderTests.cs Adds a regression test validating partial-method customization does not introduce parameter suffix renames.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/TestData/TypeProviderTests/CustomPartialMethodImplementationKeepsParameterNames/Custom.cs Adds custom partial declaration fixture used by the new regression test.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/TypeProvider.cs Fixes parameter source passed into RenameAndCloneParameters to keep signature/body/XML doc declarations consistent.
packages/http-client-csharp/.tspd/docs/customization.md Documents the need to fully qualify same-assembly generated types with global:: in partial declarations.
.chronus/changes/fix-partial-method-parameter-rename-2026-6-23-9-50-0.md Adds a Chronus changelog entry for the change.

Comment thread .chronus/changes/fix-partial-method-parameter-rename-2026-6-23-9-50-0.md Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pkg-pr-new

pkg-pr-new Bot commented Jun 23, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: b6f0f16

@azure-sdk-automation

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@github-actions

Copy link
Copy Markdown
Contributor

No changes needing a change description found.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When a partial method declaration's return type references a type generated into
the same assembly, the type is unresolved when the declaration is read (Roslyn
error type with no namespace), producing malformed 'global::.TypeName' output.

BuildPartialSignature now accepts the generator's resolved return type, and
CreatePartialMethodFromCustomSignature passes it. C# requires a partial method's
declaration and implementation to share the same return type, so the generator's
resolved type is necessarily correct. Removes the docs note that asked users to
qualify return types with global::.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@JoshLove-msft JoshLove-msft added this pull request to the merge queue Jun 23, 2026
Merged via the queue into microsoft:main with commit 1546f61 Jun 23, 2026
29 checks passed
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.

4 participants