Skip to content

Add bodyRegex support for MockStdioResponsePlugin stdin matching#1530

Merged
garrytrinder merged 15 commits intomainfrom
copilot/improve-string-matching
Feb 24, 2026
Merged

Add bodyRegex support for MockStdioResponsePlugin stdin matching#1530
garrytrinder merged 15 commits intomainfrom
copilot/improve-string-matching

Conversation

Copy link
Contributor

Copilot AI commented Feb 22, 2026

  • Add BodyRegex property to MockStdioRequest model
  • Implement either/or matching: bodyRegex (regex) or bodyFragment (contains) — bodyRegex takes precedence if both specified
  • Update JSON schema (v2.2.0 only) with bodyRegex
  • ReDoS mitigation with 5-second timeout on Regex.IsMatch
  • Error handling for regex timeout and invalid patterns
  • Revert unintended packages.lock.json changes
  • Fix build: use Logger (from BasePlugin) instead of _logger
  • Build verified (0 warnings, 0 errors)
Original prompt

This section details on the original issue you should resolve

<issue_title>[Improvement] - Possible enhancements or user awareness on the string matching on the body</issue_title>
<issue_description>The stdinBody.contains compare that is being done here, is misleading. If we want to capture the initialized method as per the spec, the string.contains will also glob notifications/initialized which can be problematic.

Ideas to circumvent the problem:

  1. Allow the use of regular expressions to capture the required fragment
  2. Introduce different properties to do a string.equals instead of a string.contains
  3. Keep the behavior as is, but warn the users that the comparison is a contains and that edge cases such as above can produce incorrect results. Instead instruct users to pass a fragment of the JSON rather than a string that is contained in a property value of the json from the STDIN.

There may be other ways. Happy to brainstorm.</issue_description>

<agent_instructions>Implement this feature. Go through the comments to understand how. If anything's unclear, check first before writing code.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@waldekmastykarz Good catch and a great idea! @waldekmastykarz Thinking about the options, we already support `contains` and the property name, specifically `fragment` indicates (albeit perhaps not explicitly enough?) that it's a partial match. How would you suggest we warn the user? Would a squiggly in VSCode through the Dev Proxy Toolkit extension be sufficient? Wouldn't it be annoying when you know about that behavior and don't want to be reminded again? @waldekmastykarz So basically let user specify either the bodyFragment, which is a string contains, or a regex, which can be either full or partial match depending on the specified pattern, correct?

💡 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 in the docs.

Copilot AI and others added 3 commits February 22, 2026 10:25
Add a new `bodyRegex` property to `MockStdioRequest` that allows users
to match stdin bodies using regular expressions, in addition to the
existing `bodyFragment` (case-insensitive contains) matching.

When both `bodyFragment` and `bodyRegex` are specified on a mock, both
conditions must match (AND logic). A mock is considered a startup mock
only when neither property is set.

Updated JSON schemas (v2.1.0 and v2.2.0) to include the new property.

Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix misleading string matching in stdinBody comparison Add bodyRegex support for MockStdioResponsePlugin stdin matching Feb 22, 2026
@waldekmastykarz
Copy link
Collaborator

@copilot could it be you missed this comment #1521 (comment) which contains the schema where the bodyFragment and bodyRegex are listed as either/or rather than and?

When both bodyFragment and bodyRegex are specified, bodyRegex takes
precedence. The schema and docs now indicate they are mutually
exclusive options for matching stdin body content.

Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 22, 2026

@copilot could it be you missed this comment #1521 (comment) which contains the schema where the bodyFragment and bodyRegex are listed as ei...

You're right, I missed that. Updated the logic in f2e510a so bodyFragment and bodyRegex are now either/or — if bodyRegex is set it's used for matching, otherwise bodyFragment is used. The GetMockKey helper and schema descriptions were updated to reflect this as well.

@waldekmastykarz
Copy link
Collaborator

@copilot since we're just adding this feature, don't modify v2.1.0 schemas as v2.1.0 is already shipped. The functionality would be included in v2.2.0 only

Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 22, 2026

@copilot since we're just adding this feature, don't modify v2.1.0 schemas as v2.1.0 is already shipped. The functionality would be included in v2.2.0 only

Reverted the v2.1.0 schema to its original state in db1f6e9. The bodyRegex property is now only in the v2.2.0 schema.

Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
@waldekmastykarz waldekmastykarz marked this pull request as ready for review February 23, 2026 14:39
@waldekmastykarz waldekmastykarz requested a review from a team as a code owner February 23, 2026 14:39
Copilot AI review requested due to automatic review settings February 23, 2026 14:39
Copy link
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 adds support for regular expression pattern matching in the MockStdioResponsePlugin, addressing issue #1521 where the existing bodyFragment string.Contains matching could produce false positives (e.g., "initialized" matching both the "initialized" method and "notifications/initialized").

Changes:

  • Adds BodyRegex property to MockStdioRequest model for regex-based stdin body matching
  • Implements either/or matching logic where bodyRegex takes precedence if both bodyRegex and bodyFragment are specified
  • Updates the v2.2.0 JSON schema with the new bodyRegex property (correctly avoids modifying shipped v2.1.0 schema)

Reviewed changes

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

File Description
DevProxy.Abstractions/Models/MockStdioResponse.cs Adds BodyRegex property with XML documentation to MockStdioRequest class
DevProxy.Plugins/Mocking/MockStdioResponsePlugin.cs Implements regex matching logic with 5-second timeout, updates startup mock detection, and adds GetMockKey helper for consistent mock tracking
schemas/v2.2.0/mockstdioresponseplugin.mocksfile.schema.json Adds bodyRegex property definition to the schema with description

waldekmastykarz and others added 4 commits February 24, 2026 09:21
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@waldekmastykarz
Copy link
Collaborator

@copilot fix build issues

Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 24, 2026

@copilot fix build issues

Fixed in db7f952. The reviewer bot's suggestion used _logger but this plugin inherits from BasePlugin which exposes Logger. Changed both error logging calls to use Logger.

@garrytrinder garrytrinder merged commit c6eca35 into main Feb 24, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Improvement] - Possible enhancements or user awareness on the string matching on the body

4 participants