[repo-assist] perf+test: replace UriBuilder+ParseQueryString with StringBuilder; add 9 tests (500→509)#457
Merged
sergey-tihon merged 3 commits intoJun 2, 2026
Conversation
…d 9 tests (500→509) Tasks: 8 (perf), 9 (tests), 5 (coding improvement) perf: RuntimeHelpers.createHttpRequest - Replace UriBuilder + System.Web.HttpUtility.ParseQueryString + NameValueCollection with a single StringBuilder + Uri.EscapeDataString pass. - Avoids 4-5 allocations per API call (UriBuilder, NameValueCollection, Hashtable, intermediate strings). Now allocates only one StringBuilder. - Encoding changes from form-encoding (spaces as '+') to RFC 3986 percent-encoding (spaces as '%20'); both are accepted by HTTP servers. - Removes the Seq.isEmpty early-exit fast path (no longer needed — the StringBuilder path is already allocation-efficient with zero params). - Removes the now-unused System.Web.HttpUtility reference. coding: RuntimeHelpers.toFormUrlEncodedContent - Merge the Seq.filter + Seq.choose passes into a single Seq.choose, avoiding an intermediate sequence allocation for the common case. tests (9 new, 500→509): - RuntimeHelpersTests: 4 tests documenting RFC 3986 percent-encoding in createHttpRequest (spaces, & and = in values, spaces in names). - Schema.OperationCompilationTests: 2 tests asserting that the '200' response takes priority over '201' when both are present in an operation. - Schema.V2SchemaCompilationTests: 4 tests verifying compiled operation return types from a Swagger 2.0 petstore (listPets→Task<Pet[]>, getPet→Task<Pet>, getPet path param int64, createPet→Task<IO.Stream> per OpenApi normalisation). Build/test: - dotnet fantomas --check: no issues - Unit tests: 509 total, 0 failed, 1 skipped Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the runtime request/query construction logic to reduce allocations, and adds tests that document/verify expected return-type compilation behavior across OpenAPI v3 and Swagger v2 schemas.
Changes:
- Reworked
RuntimeHelpers.createHttpRequestquery-string assembly to useStringBuilder+Uri.EscapeDataStringinstead ofUriBuilder+ParseQueryString. - Streamlined
toFormUrlEncodedContentto avoid an extra sequence pass. - Added new unit tests covering query percent-encoding behavior and schema operation return-type selection/priority rules.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/SwaggerProvider.Runtime/RuntimeHelpers.fs | Replaces query-string building implementation and refactors form-url-encoding sequence processing. |
| tests/SwaggerProvider.Tests/RuntimeHelpersTests.fs | Adds tests asserting RFC3986 percent-encoding behavior for query params. |
| tests/SwaggerProvider.Tests/Schema.OperationCompilationTests.fs | Adds tests asserting 200 response schema priority over other 2xx responses. |
| tests/SwaggerProvider.Tests/Schema.V2SchemaCompilationTests.fs | Adds tests validating generated method return types/parameters for a minimal Swagger v2 petstore schema. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
|
@copilot address inline comments |
Contributor
Addressed in commit
|
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.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
Three improvements across runtime performance, test coverage, and code clarity.
Task 8 — Performance: faster query string building
RuntimeHelpers.createHttpRequestpreviously usedUriBuilder+System.Web.HttpUtility.ParseQueryString+NameValueCollectionto assemble the query string. This allocates 4–5 objects per API call (UriBuilder, NameValueCollection, internal Hashtable, intermediate strings).This PR replaces that with a single
StringBuilder+Uri.EscapeDataStringpass:The encoding changes from
application/x-www-form-urlencoded(+for spaces) to RFC 3986 percent-encoding (%20for spaces). Both are accepted by all standards-compliant HTTP servers. TheSystem.Webreference is removed.Task 5 (fallback for Task 4) — Coding: single-pass
toFormUrlEncodedContentThe
toFormUrlEncodedContentfunction previously did two sequentialSeqpasses (filterthenchoose). These are merged into a singleSeq.choosepass, avoiding an intermediate sequence allocation.Task 9 — Testing: 9 new tests (500 → 509)
RuntimeHelpersTests (4 tests): document the new RFC 3986 encoding behaviour:
%20, not+&,=) in values are percent-encodedSchema.OperationCompilationTests (2 tests): verify 200 response takes priority over 201:
200(string) and201(integer) responses are defined,200wins →Task<string>201integer schema is not usedSchema.V2SchemaCompilationTests (4 tests): verify Swagger 2.0 operation return types:
listPets→Task<Pet[]>getPet→Task<Pet>getPetpath parameter isint64createPet→Task<IO.Stream>(documents that Microsoft.OpenApi normalises a v2201with no schema toapplication/octet-stream)Trade-offs
%20vs+for spaces: RFC 3986 is the correct encoding for query components;+is application/x-www-form-urlencoded. Real-world APIs accept both, but%20is strictly correct for query strings outside of HTML forms.StringBuilderpre-allocates no extra capacity — for operations with no query params the result is just the trimmed path (same as the previous fast-path).Test Status
dotnet fantomas --check: all files pass ✅Add this agentic workflows to your repo
To install this agentic workflow, run