Commit ba787fc
fix: required client init parameters should not trigger separate ClientOptions type (#10375)
`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
```tsp
// 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
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: JonathanCrd <17486462+JonathanCrd@users.noreply.github.com>
Co-authored-by: jolov <jolov@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 3bad3dc commit ba787fc
3 files changed
Lines changed: 58 additions & 4 deletions
File tree
- packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel
- src/Providers
- test/Providers
- ClientProviders
Lines changed: 17 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
89 | 92 | | |
90 | 93 | | |
91 | | - | |
| 94 | + | |
92 | 95 | | |
93 | 96 | | |
94 | 97 | | |
| |||
98 | 101 | | |
99 | 102 | | |
100 | 103 | | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
101 | 110 | | |
102 | 111 | | |
103 | 112 | | |
| |||
111 | 120 | | |
112 | 121 | | |
113 | 122 | | |
114 | | - | |
| 123 | + | |
115 | 124 | | |
116 | | - | |
| 125 | + | |
| 126 | + | |
117 | 127 | | |
118 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
119 | 132 | | |
120 | 133 | | |
121 | 134 | | |
| |||
Lines changed: 36 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
466 | 466 | | |
467 | 467 | | |
468 | 468 | | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
469 | 505 | | |
470 | 506 | | |
471 | 507 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
72 | 77 | | |
73 | 78 | | |
74 | 79 | | |
| |||
0 commit comments