Commit 1e977cc
[http-client-csharp] Fix trailing path separator for optional path parameter (#11096)
## Problem
When a route ends with an **optional** path parameter, the `/` separator
preceding that segment was emitted unconditionally, leaving a dangling
trailing slash when the value is null.
For example, `/certificates/{certificate-name}/{certificate-version}`
(with `certificate-version` optional) generated:
```csharp
uri.AppendPath(certificateName, true);
uri.AppendPath("/", false); // emitted unconditionally
if ((certificateVersion != null))
{
uri.AppendPath(certificateVersion, true);
}
```
So when `certificateVersion` is null the wire URL becomes
`/certificates/{name}/` instead of `/certificates/{name}`. Services
(e.g. Key Vault) and recorded test cassettes expect no trailing slash.
The existing `shouldPrependWithPathSeparator` logic only handled the
case where the preceding literal does *not* end in `/`; it missed the
case where the separator had already been written unconditionally.
## Fix
In `RestClientProvider.AddUriSegments`, when the upcoming path parameter
is optional and the preceding literal ends with `/`, defer that trailing
`/` so it is written together with the parameter value inside the null
check:
```csharp
uri.AppendPath(certificateName, true);
if ((certificateVersion != null))
{
uri.AppendPath("/", false);
uri.AppendPath(certificateVersion, true);
}
```
Required path parameters are unaffected.
## Tests
- Added `TestBuildCreateRequestMethodWithOptionalPathParameter` (+
golden file) covering the trailing optional-segment case.
- Updated two `ServerTemplate*` tests that relied on the test factory's
default `isRequired: false` for what were intended to be normal required
path parameters.
- Full `Microsoft.TypeSpec.Generator.ClientModel` suite passes (1440
tests).
Reported in Azure/azure-sdk-for-net#60162 (Bug A).
--generated by Copilot
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 924435b commit 1e977cc
4 files changed
Lines changed: 78 additions & 5 deletions
File tree
- packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel
- src/Providers
- test/Providers
- ClientProviders
- RestClientProviders
- TestData/RestClientProviderTests
Lines changed: 19 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
717 | 717 | | |
718 | 718 | | |
719 | 719 | | |
720 | | - | |
721 | 720 | | |
722 | 721 | | |
723 | 722 | | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
724 | 741 | | |
725 | 742 | | |
726 | 743 | | |
| |||
766 | 783 | | |
767 | 784 | | |
768 | 785 | | |
769 | | - | |
| 786 | + | |
770 | 787 | | |
771 | 788 | | |
772 | 789 | | |
| |||
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3589 | 3589 | | |
3590 | 3590 | | |
3591 | 3591 | | |
3592 | | - | |
3593 | | - | |
| 3592 | + | |
| 3593 | + | |
3594 | 3594 | | |
3595 | 3595 | | |
3596 | 3596 | | |
| |||
3633 | 3633 | | |
3634 | 3634 | | |
3635 | 3635 | | |
3636 | | - | |
| 3636 | + | |
3637 | 3637 | | |
3638 | 3638 | | |
3639 | 3639 | | |
| |||
Lines changed: 28 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
819 | 819 | | |
820 | 820 | | |
821 | 821 | | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
822 | 850 | | |
823 | 851 | | |
824 | 852 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
0 commit comments