Skip to content

fix: required client init parameters should not trigger separate ClientOptions type#10375

Merged
JoshLove-msft merged 4 commits intomainfrom
copilot/fix-client-options-bug
Apr 17, 2026
Merged

fix: required client init parameters should not trigger separate ClientOptions type#10375
JoshLove-msft merged 4 commits intomainfrom
copilot/fix-client-options-bug

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

UseSingletonInstance in ClientOptionsProvider treated all non-standard client parameters as requiring a separate client-specific ClientOptions class. But required parameters (no DefaultValue) never become properties on the options class—they're inlined as constructor parameters on the client. This caused unnecessary per-client options types when using @@clientInitialization with required parameters in multi-client scenarios.

Changes

  • ClientOptionsProvider.cs: Added parameter.DefaultValue != null check in UseSingletonInstance so only optional parameters (which actually become options properties) trigger a separate client-specific options type. This aligns the singleton decision logic with BuildProperties, which already filters on DefaultValue != null.
  • ClientOptionsProviderTests.cs: Added MultipleClientsWithRequiredCustomParametersShareSingletonOptions test covering the multi-client scenario where a required custom parameter should still share the singleton options.

Before/After

// Multi-client scenario with required init parameter
@@clientInitialization(KnowledgeBaseRetrievalClient, {
  parameters: KnowledgeBaseRetrievalClientOptions, // knowledgeBaseName: string (required)
});

Before: Generates KnowledgeBaseRetrievalClientOptions (separate, unnecessary options type)
After: Shares singleton <ServiceName>ClientOptions with other clients in the library

…arate ClientOptions type

In UseSingletonInstance, skip required parameters (those without DefaultValue)
when determining if a client needs a separate client-specific options type.
Required parameters are inlined as constructor parameters on the client and
never become properties on the ClientOptions class, so they should not trigger
the creation of a separate client-specific options type.

Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/6c0324cb-28eb-47ca-b77e-05f5375bcea7

Co-authored-by: JonathanCrd <17486462+JonathanCrd@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 Apr 14, 2026
Copilot AI changed the title [WIP] Fix bug with unintended ClientOptions generation in C# fix: required client init parameters should not trigger separate ClientOptions type Apr 14, 2026
Copilot AI requested a review from JonathanCrd April 14, 2026 22:52
…ameters

The DefaultValue check was too broad — it also skipped InputPathParameters
like subscriptionId (required, no default), incorrectly allowing multi-service
clients to share the singleton options type. Narrow the check to only apply
to InputMethodParameter (from @@clientInitialization), preserving the original
behavior for other parameter types.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 16, 2026

Open in StackBlitz

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

commit: 367bc44

@github-actions
Copy link
Copy Markdown
Contributor

No changes needing a change description found.

Multi-service clients need client-specific options for service-specific
API version properties, so UseSingletonInstance now returns false for
them explicitly. Also adds singleton reset to ClientProviderTests.SetUp
to prevent state leaking between tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@JoshLove-msft JoshLove-msft enabled auto-merge April 16, 2026 23:15
@JoshLove-msft JoshLove-msft added this pull request to the merge queue Apr 16, 2026
Merged via the queue into main with commit ba787fc Apr 17, 2026
29 checks passed
@JoshLove-msft JoshLove-msft deleted the copilot/fix-client-options-bug branch April 17, 2026 00:35
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.

[Bug]: Required client initialization parameters generate unintended ClientOptions in C#

4 participants