Skip to content

[repo-assist] refactor: replace Regex.Replace with String.Replace for path parameter substitution + test coverage (+7 tests)#431

Open
github-actions[bot] wants to merge 3 commits into
masterfrom
repo-assist/improve-operation-compiler-path-param-20260511-3823be065e9059d0
Open

[repo-assist] refactor: replace Regex.Replace with String.Replace for path parameter substitution + test coverage (+7 tests)#431
github-actions[bot] wants to merge 3 commits into
masterfrom
repo-assist/improve-operation-compiler-path-param-20260511-3823be065e9059d0

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

Two related improvements to OperationCompiler.fs and its test suite.

1. Remove Regex.Replace for path parameter substitution

Path parameter substitution in generated client code used Regex.Replace with a literal pattern like {petId}. This required:

  • An open System.Text.RegularExpressions import
  • Escaping $ characters in the replacement value (to avoid regex back-reference interpretation of $0, $&, etc.)

Before:

let pattern = $"{{{name}}}"
// Escape $ in the replacement to avoid regex back-reference interpretation ($0, $& etc.)
let escaped = <@ (%value).Replace("$", "$$") @>
let path' = <@ Regex.Replace(%path, pattern, %escaped) @>

After:

let pattern = $"{{{name}}}"
let path' = <@ (%path).Replace(pattern, %value) @>

String.Replace is simpler, faster, and does not interpret any characters in the replacement string specially. The behaviour is identical since the pattern is always a literal string like {petId} — no regex features are used.

Net change: −4 lines; removes open System.Text.RegularExpressions, the escaped intermediate expression, and the $ → $$ escaping step.

2. New OperationCompiler tests (+7 tests, 389 → 396)

Fills three gaps in the unit test suite:

  • Multiple path parameters (/users/{userId}/posts/{postId}): verifies both params appear in the method signature, are required (not optional), and CancellationToken comes last.
  • PATCH operation: verifies a PATCH endpoint with path param + JSON body generates the correct method signature.
  • Auto-generated operation name (no operationId): verifies the compiler handles a missing operationId gracefully and generates a method with the correct parameters.

Test Status

✅ All 396 unit tests pass after the changes.

SwaggerProvider.Tests  Total: 396, Errors: 0, Failed: 0, Skipped: 1, Time: 1.119s

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@c7d030cd6d4607b90d9ac3ffc8b24aff4f251632

github-actions Bot and others added 2 commits May 11, 2026 20:16
…bstitution

Path parameter substitution used Regex.Replace with a literal pattern like
{petId}. This required escaping '$' in the replacement value to prevent
regex back-reference interpretation ($0, $& etc.).

String.Replace is simpler, faster, and does not interpret any characters
in the replacement string specially. The behaviour is identical: both
replace every occurrence of the literal token in the path template.

Removes the System.Text.RegularExpressions open and the intermediate
escaped expression, reducing generated code complexity and eliminating
a (small) regex engine overhead per path parameter per API call.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…d auto-generated names (+7 tests)

Fills three gaps in the OperationCompiler unit test suite:

1. Multiple path parameters (/users/{userId}/posts/{postId}): verify
   both params appear in the signature, are required, and CancellationToken
   comes last.

2. PATCH operation: verify a PATCH endpoint with path param + JSON body
   generates the expected method with correct parameter order.

3. Auto-generated operation name (no operationId): verify compilation
   succeeds and the generated method has the expected parameter signature
   (categoryId + CancellationToken) when no operationId is specified.

Total: 389 → 396 tests (7 new).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@sergey-tihon sergey-tihon marked this pull request as ready for review May 11, 2026 20:29
Copilot AI review requested due to automatic review settings May 11, 2026 20:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the design-time operation code generation to use String.Replace instead of Regex.Replace when substituting {path} parameters in generated client methods, and adds new unit tests covering additional operation-compilation scenarios.

Changes:

  • Replaced regex-based path parameter substitution with String.Replace, removing the need for regex imports and $-escaping in replacements.
  • Added new operation-compilation tests covering multiple path parameters, PATCH operations, and missing operationId handling.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/SwaggerProvider.DesignTime/OperationCompiler.fs Removes System.Text.RegularExpressions usage and switches path param substitution to String.Replace.
tests/SwaggerProvider.Tests/Schema.OperationCompilationTests.fs Adds 7 tests for multiple path params, PATCH signature generation, and missing operationId cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +796 to +797
let allMethods = types |> List.collect(fun t -> t.GetMethods() |> Array.toList)
allMethods |> List.isEmpty |> shouldEqual false
let lastParam = parameters |> Array.last

lastParam.ParameterType
|> shouldEqual typeof<Threading.CancellationToken>
Comment on lines +759 to +760
lastParam.ParameterType
|> shouldEqual typeof<Threading.CancellationToken>
Comment on lines +811 to +812
&& ps[0].Name = "categoryId"
&& ps[1].ParameterType = typeof<Threading.CancellationToken>)
@sergey-tihon
Copy link
Copy Markdown
Member

@copilot fix inline comments, format code and ensure that it build and tests work

Copy link
Copy Markdown
Contributor

Copilot AI commented May 11, 2026

@copilot fix inline comments, format code and ensure that it build and tests work

Done in 59fde7b. I fixed the inline review items in Schema.OperationCompilationTests.fs (consistent typeof<CancellationToken> usage and a concrete assertion for the auto-generated method name GetCategoryItems), ran Fantomas on the file, and verified build/tests:

  • dotnet fsi build.fsx -t Build
  • dotnet build tests/SwaggerProvider.Tests/SwaggerProvider.Tests.fsproj -c Release
  • dotnet tests/SwaggerProvider.Tests/bin/Release/net10.0/SwaggerProvider.Tests.dll ✅ (396 passed, 1 skipped)

Copilot AI requested a review from sergey-tihon May 11, 2026 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants