Skip to content

Allow passing JsonSerializerOptions to a request mock#82

Merged
dennisdoomen merged 2 commits intomainfrom
copilot/add-jsonserializeroptions-to-request-mock
Mar 10, 2026
Merged

Allow passing JsonSerializerOptions to a request mock#82
dennisdoomen merged 2 commits intomainfrom
copilot/add-jsonserializeroptions-to-request-mock

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 10, 2026

Adds a Using(JsonSerializerOptions) method to RequestMockBuilder so custom serialization settings (e.g. naming policies, converters) propagate consistently to all JSON operations on that mock — body matching and response serialization alike.

Changes

  • RequestMockBuilder: Added jsonSerializerOptions field and Using(JsonSerializerOptions) fluent method (with null guard). Updated all JsonSerializer.Serialize() call sites — WithBody(object), RespondsWithJsonContent, and both JSON-serializing RespondsWithODataResult overloads — to forward the configured options. Options are captured into a local before lambdas to avoid closing over a mutable field.
  • API verification files: Updated net8.0.verified.txt and net472.verified.txt to reflect the new public method.

Example

var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };

mock.ForGet()
    .WithPath("/api/users")
    .Using(options)
    .RespondsWithJsonContent(new { UserId = 42, UserName = "Alice" });
// Response body: {"userId":42,"userName":"Alice"}

mock.ForPost()
    .WithPath("/api/users")
    .Using(options)
    .WithBody(new { UserId = 42, UserName = "Alice" })  // matches camelCase JSON
    .RespondsWithStatus(HttpStatusCode.Created);
Original prompt

This section details on the original issue you should resolve

<issue_title>Allow passing JsonSerializerOptions to a request mock</issue_title>
<issue_description>Add a Using method on RequestMockBuilder that takes a JsonSerializerOptions object that will be used everywhere in that mock where the JsonSerializer is used.</issue_description>

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


💡 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.

Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>
Copilot AI changed the title [WIP] Add method to pass JsonSerializerOptions to request mock Allow passing JsonSerializerOptions to a request mock Mar 10, 2026
@dennisdoomen dennisdoomen marked this pull request as ready for review March 10, 2026 18:51
@dennisdoomen dennisdoomen enabled auto-merge (squash) March 10, 2026 18:53
/// </summary>
public RequestMockBuilder Using(JsonSerializerOptions options)
{
if (options is null)

Check notice

Code scanning / InspectCode

Join null check with assignment Note

Join null check with assignment
@dennisdoomen dennisdoomen added the enhancement New feature or request label Mar 10, 2026
@github-actions
Copy link
Copy Markdown

Test Results

  3 files  ±0    3 suites  ±0   4s ⏱️ -2s
131 tests +2  131 ✅ +2  0 💤 ±0  0 ❌ ±0 
256 runs  +4  256 ✅ +4  0 💤 ±0  0 ❌ ±0 

Results for commit 8989ad2. ± Comparison against base commit 45b198f.

@coveralls
Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 22918576764

Details

  • 12 of 14 (85.71%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.06%) to 83.315%

Changes Missing Coverage Covered Lines Changed/Added Lines %
Mockly/RequestMockBuilder.cs 12 14 85.71%
Totals Coverage Status
Change from base Build 22918158276: -0.06%
Covered Lines: 1206
Relevant Lines: 1381

💛 - Coveralls

@dennisdoomen dennisdoomen merged commit 695c214 into main Mar 10, 2026
10 checks passed
@dennisdoomen dennisdoomen deleted the copilot/add-jsonserializeroptions-to-request-mock branch March 10, 2026 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow passing JsonSerializerOptions to a request mock

4 participants