[http-client-csharp] Fix trailing path separator for optional path parameter#11096
Open
JoshLove-msft wants to merge 2 commits into
Open
[http-client-csharp] Fix trailing path separator for optional path parameter#11096JoshLove-msft wants to merge 2 commits into
JoshLove-msft wants to merge 2 commits into
Conversation
When a route ends with an optional path parameter (e.g.
/items/{name}/{version} with optional version), the separator '/'
preceding the optional segment was emitted unconditionally, producing
/items/{name}/ when the value was null instead of /items/{name}.
Defer the trailing separator into the parameter's null check so it is
only written when the optional value is present.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
commit: |
Contributor
|
No changes needing a change description found. |
|
You can try these changes here
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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}(withcertificate-versionoptional) generated:So when
certificateVersionis 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
shouldPrependWithPathSeparatorlogic 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:Required path parameters are unaffected.
Tests
TestBuildCreateRequestMethodWithOptionalPathParameter(+ golden file) covering the trailing optional-segment case.ServerTemplate*tests that relied on the test factory's defaultisRequired: falsefor what were intended to be normal required path parameters.Microsoft.TypeSpec.Generator.ClientModelsuite passes (1440 tests).Reported in Azure/azure-sdk-for-net#60162 (Bug A).
--generated by Copilot