Skip to content

Send string response bodies as-is instead of JSON-serializing them - #1994

Merged
waldekmastykarz merged 2 commits into
dotnet:mainfrom
waldekmastykarz:supreme-parakeet
Oct 26, 2026
Merged

waldekmastykarz merged 2 commits into
dotnet:mainfrom
waldekmastykarz:supreme-parakeet

Conversation

@waldekmastykarz

Copy link
Copy Markdown
Collaborator

Closes #1970

Problem

A string body in GenericRandomErrorPlugin errors, RateLimitingPlugin custom responses, and MockResponsePlugin mocks was JSON-serialized, so "body": "hello" was sent as "hello" (with quotes).

Change

  • Added ProxyUtils.SerializeBody: strings (including JSON string elements) are returned as-is; other values are still serialized as JSON; null returns an empty string.
  • Used it in GenericRandomErrorPlugin, RateLimitingPlugin, and MockResponsePlugin. @file bodies still load the response from disk.
  • GraphMockResponsePlugin is unchanged because batch responses embed bodies inside JSON, where strings are already correct.

⚠️ Behavior change: configs that relied on a quoted JSON-string body now get the raw string.

Tests

  • Unit tests for ProxyUtils.SerializeBody.
  • Integration tests for string bodies in GenericRandomErrorPlugin and RateLimitingPlugin custom responses. Tightened the existing MockResponsePlugin test to assert the exact body. These tests fail without the fix.
  • DevProxy.Abstractions.Tests, DevProxy.Integration.Tests (95/95), and DevProxy.Tests all pass.
String `body` values in GenericRandomErrorPlugin errors, RateLimitingPlugin
custom responses, and MockResponsePlugin mocks were JSON-serialized, so
`"body": "hello"` was sent as `"hello"` including the quotes. String bodies
are now sent verbatim; non-string bodies are still serialized as JSON and
`@file` bodies still load the response from disk.

Closes dotnet#1970

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 5a24c559-2e14-4f92-b083-5e42f0e6a6f8
@waldekmastykarz
waldekmastykarz requested a review from a team as a code owner October 21, 2026 13:58
Copilot AI balanced review requested due to automatic review settings October 21, 2026 13:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused serialization changes have regression coverage for all three affected plugins, with no unresolved findings.

No open issues 🎉

What changed in this PR

Fixes #1970 by sending configured string response bodies without JSON-added quotes across three plugins.

Changes:

  • Adds shared body serialization that preserves strings and JSON-serializes other values.
  • Updates affected plugins while preserving existing @file handling.
  • Adds regression tests and exact-body assertions.
File Description
DevProxy.Plugins/​Mocking/​MockResponsePlugin.cs Preserves raw strings and file-body handling.
DevProxy.Plugins/​Behavior/​RateLimitingPlugin.cs Preserves custom response strings.
DevProxy.Plugins/​Behavior/​GenericRandomErrorPlugin.cs Preserves error strings and file-body handling.
DevProxy.Integration.Tests/​ManipulationAndMockingIntegrationTests.cs Asserts exact mock response content.
DevProxy.Integration.Tests/​BehaviorPluginsIntegrationTests.cs Tests error and rate-limit string responses.
DevProxy.Abstractions/​Utils/​ProxyUtils.cs Adds shared SerializeBody helper.
DevProxy.Abstractions.Tests/​Utils/​ProxyUtilsTests.cs Tests null, string, and object serialization.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused changes address the reported behavior with regression coverage and no identified blocking defects.

No open issues 🎉

Review effort: Balanced

Keep string-body regression coverage and default Content-Type regression coverage as separate tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@waldekmastykarz
waldekmastykarz merged commit c9a4a0b into dotnet:main Oct 26, 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

2 participants