Commit b2b6044
Enable UserAgentPolicy in generated client libraries (#8983)
## Enable UserAgentPolicy in Generated Libraries
### Completed Work
- [x] Research UserAgentPolicy API and usage patterns
- [x] Modify `ClientProvider` to create UserAgentPolicy through
abstraction layer
- [x] Verify all tests pass successfully
- [x] Address feedback: Make UserAgentPolicy a virtual method with
default null value
- [x] Address feedback: Use nameof(System.Type.Assembly) for property
reference
- [x] Address feedback: Remove optional null parameter from
UserAgentPolicy constructor
- [x] Address feedback: Refactor variable names for clarity
### Implementation Details
The UserAgentPolicy is now automatically added to the pipeline for all
generated clients:
- Added `UserAgentPolicy` as a **virtual method** in `ClientPipelineApi`
with default return value of `null`
- Implemented in `ClientPipelineProvider` to return `new
UserAgentPolicy(typeof(ClientType).Assembly)`
- `ClientProvider` builds a list of per-retry policies, conditionally
adding UserAgentPolicy first if not null, then auth policies
- Uses `nameof(System.Type.Assembly)` for compile-time safety
- Omits optional applicationId parameter (cleaner code)
- Policy is added as the first item in the `perRetryPolicies` array,
before authentication policies
- Works for clients with no auth, API key auth, and OAuth2 auth
- No configuration option needed - always enabled by default
- **Optional via abstractions** - implementations can override the
virtual method to return null and disable UserAgentPolicy
### Code Structure
The refactored code uses a `perRetryPoliciesList` that is built up
conditionally:
1. UserAgentPolicy is added first (if not null)
2. Authentication policies are added based on the auth type
3. The list is converted to an array for the pipeline
This makes it clear that all policies in the list are per-retry
policies, not just auth-specific policies.
### Generated Code Example
```csharp
Pipeline = ClientPipeline.Create(
options,
Array.Empty<PipelinePolicy>(),
new PipelinePolicy[] {
new UserAgentPolicy(typeof(SampleClient).Assembly),
ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy(_keyCredential, AuthorizationHeader)
},
Array.Empty<PipelinePolicy>());
```
- Fixes #7863
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Enable UserAgentPolicy once SCM ships</issue_title>
> <issue_description>Once
Azure/azure-sdk-for-net#51113 ships, we should
integrate the UserAgentPolicy into generated libraries. We should add an
option so that users can opt out of it, but this can be on by default so
users don't have to know to turn it on. We can document this behavior
clearly.</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> <comment_new><author>@JoshLove-msft</author><body>
> For Azure, we would just turn it off as the Azure.Core pipeline has
its own UserAgentPolicy.</body></comment_new>
> <comment_new><author>@JoshLove-msft</author><body>
> > For Azure, we would just turn it off as the Azure.Core pipeline has
its own UserAgentPolicy.
>
> Actually, we don't need to add config for this until we hear feedback
that users don't want it on. For Azure, it will not use the same
pipeline so there is no need to turn it off.</body></comment_new>
> </comments>
>
</details>
- Fixes #7863
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more [Copilot coding agent
tips](https://gh.io/copilot-coding-agent-tips) in the docs.
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: JoshLove-msft <[email protected]>1 parent 2f78f49 commit b2b6044
File tree
16 files changed
+31
-17
lines changed- packages/http-client-csharp/generator
- Microsoft.TypeSpec.Generator.ClientModel
- src/Providers
- Abstractions
- test/Providers/ClientProviders/TestData/ClientProviderTests
- TestProjects/Local/Sample-TypeSpec/src/Generated
16 files changed
+31
-17
lines changedLines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
61 | 64 | | |
62 | 65 | | |
63 | 66 | | |
| |||
Lines changed: 13 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
570 | 570 | | |
571 | 571 | | |
572 | 572 | | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
573 | 581 | | |
574 | 582 | | |
575 | 583 | | |
576 | 584 | | |
577 | 585 | | |
578 | | - | |
| 586 | + | |
| 587 | + | |
579 | 588 | | |
580 | 589 | | |
581 | | - | |
| 590 | + | |
| 591 | + | |
582 | 592 | | |
583 | 593 | | |
584 | | - | |
| 594 | + | |
585 | 595 | | |
586 | 596 | | |
587 | 597 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
0 commit comments