Skip to content

Generate ClientSettings and auth constructors for individually-initialized sub-clients#10042

Merged
JoshLove-msft merged 3 commits intomicrosoft:mainfrom
JoshLove-msft:jolov/clientsettings-subclient-support
Mar 16, 2026
Merged

Generate ClientSettings and auth constructors for individually-initialized sub-clients#10042
JoshLove-msft merged 3 commits intomicrosoft:mainfrom
JoshLove-msft:jolov/clientsettings-subclient-support

Conversation

@JoshLove-msft
Copy link
Contributor

@JoshLove-msft JoshLove-msft commented Mar 16, 2026

Summary

Extends the C# generator to produce ClientSettings classes and authentication constructors for sub-clients that have InitializedBy.Individually (i.e., can be constructed directly by the user, not only via parent client accessors).

Changes

ClientProvider.cs

  • Auth fields for individually-initialized sub-clients: Sub-clients with InitializedBy.Individually now get _apiKeyAuthFields / _oauth2Fields (previously only root clients had these).
  • Auth constructors for sub-clients: AppendSubClientPublicConstructors now generates credential-based constructors (ApiKeyCredential, AuthenticationTokenProvider) that chain to the internal AuthenticationPolicy constructor via this(...).
  • Internal constructor always uses auth policy: Changed BuildPrimaryConstructorBody condition from authPolicyParam != null && authFields != null to authPolicyParam != null.
  • EffectiveClientOptions property: Sub-clients resolve the root client's ClientOptions type via GetRootClient()?.ClientOptions.
  • Removed auth params from parent-init constructor: GetSubClientInternalConstructorParameters no longer includes credential params.

ClientSettingsProvider.cs

  • Uses _clientProvider.EffectiveClientOptions instead of duplicating parent-walking logic.

ScmOutputLibrary.cs

  • ClientSettings emission decoupled from ClientOptions != null.

Test Updates

  • Updated 8 expected test data files for the pipeline body change.
  • Updated sub-client constructor test assertions for the new auth constructor pattern.
  • 6 new sub-client settings tests in ClientSettingsProviderTests.cs.

Generated Output

  • Metrics.cs now has auth constructors (ApiKeyCredential, AuthenticationTokenProvider) and auth pipeline.
  • MetricsSettings.cs — new generated settings class for the Metrics sub-client.

Testing

  • All 1220 unit tests pass
  • Generate.ps1 succeeds with no diff beyond expected changes

- Sub-clients with InitializedBy.Individually now get ClientSettings class
- Sub-clients with InitializedBy.Individually now get auth constructors (ApiKeyCredential, AuthenticationTokenProvider)
- Internal AuthenticationPolicy constructor always adds auth policy to pipeline
- EffectiveClientOptions property on ClientProvider resolves root client's options for sub-clients
- Updated test expected files for unsupported-auth pipeline body change

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 Mar 16, 2026
@JoshLove-msft JoshLove-msft changed the title Generate ClientSettings for individually-initialized sub-clients and fix missing auth for subclient ctors Generate ClientSettings and auth constructors for individually-initialized sub-clients Mar 16, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 16, 2026

Open in StackBlitz

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

commit: fb2f91f

@github-actions
Copy link
Contributor

No changes needing a change description found.

@JoshLove-msft JoshLove-msft force-pushed the jolov/clientsettings-subclient-support branch from c755a3c to 4bfb878 Compare March 16, 2026 18:02
@JoshLove-msft JoshLove-msft enabled auto-merge March 16, 2026 18:13
…izedBy

- Remove canBeIndividuallyInitialized variable and all ClientOptions null-checks
  for auth fields and ClientSettings; use only the InitializedBy flag
- Add braces to if blocks in constructor builder loops
- Update InputFactory.Client default to Individually (matches production)
- Update test sub-clients to explicitly set initializedBy: Parent

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@JoshLove-msft JoshLove-msft force-pushed the jolov/clientsettings-subclient-support branch from 4bfb878 to dcdc8b3 Compare March 16, 2026 18:34
When authenticationPolicy is null (no-auth clients), the pipeline was
including null in the per-retry policies array causing NullReferenceException.
Now generates an if/else to only include authenticationPolicy when non-null.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@JoshLove-msft JoshLove-msft added this pull request to the merge queue Mar 16, 2026
Merged via the queue into microsoft:main with commit ed27486 Mar 16, 2026
25 checks passed
@JoshLove-msft JoshLove-msft deleted the jolov/clientsettings-subclient-support branch March 16, 2026 22:26
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.

2 participants