[PR #1301] [MERGED] Fix errors when converting response bodies with top-level JSON arrays to C# code #1326

Closed
opened 2025-12-29 09:19:19 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/wiremock/WireMock.Net/pull/1301
Author: @asaf92
Created: 5/21/2025
Status: Merged
Merged: 5/22/2025
Merged by: @StefH

Base: masterHead: fix/json-array-parsing


📝 Commits (1)

  • ae6a76f fix errors when converting json arrays to C# code

📊 Changes

1 file changed (+2 additions, -2 deletions)

View changed files

📝 src/WireMock.Net/Util/CSharpFormatter.cs (+2 -2)

📄 Description

When trying to convert static mapping to C# code, I ran into an exception when the body is a top-level array rather than a pure string or a (non-array) JSON object.

The problem is that this code doesn't distinguish between arrays and objects, and assumes that every non-null that isn't a string is an object:

case BodyType.Json:
    if (bodyData.BodyAsJson is string bodyStringValue)
    {
        sb.AppendLine($"        .WithBody({ToCSharpStringLiteral(bodyStringValue)})");
    }
    else if (bodyData.BodyAsJson is { } jsonBody)
    {
        var anonymousObjectDefinition = ConvertToAnonymousObjectDefinition(jsonBody);
        sb.AppendLine($"        .WithBodyAsJson({anonymousObjectDefinition})");
    }
                                                                                      
    break;

My solution is to load a JToken instead of a JObject in ConvertToAnonymousObjectDefinition, which seems like the original intention given that ConvertJsonToAnonymousObjectDefinition (which is defined in an internal class) expects a JToken and isn't called anywhere else in the code.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/wiremock/WireMock.Net/pull/1301 **Author:** [@asaf92](https://github.com/asaf92) **Created:** 5/21/2025 **Status:** ✅ Merged **Merged:** 5/22/2025 **Merged by:** [@StefH](https://github.com/StefH) **Base:** `master` ← **Head:** `fix/json-array-parsing` --- ### 📝 Commits (1) - [`ae6a76f`](https://github.com/wiremock/WireMock.Net/commit/ae6a76f4c2570fcc289599a7a3362e60eb9910cf) fix errors when converting json arrays to C# code ### 📊 Changes **1 file changed** (+2 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `src/WireMock.Net/Util/CSharpFormatter.cs` (+2 -2) </details> ### 📄 Description When trying to convert static mapping to C# code, I ran into an exception when the body is a top-level array rather than a pure string or a (non-array) JSON object. The problem is that this code doesn't distinguish between arrays and objects, and assumes that every non-null that isn't a string is an object: ```cs case BodyType.Json: if (bodyData.BodyAsJson is string bodyStringValue) { sb.AppendLine($" .WithBody({ToCSharpStringLiteral(bodyStringValue)})"); } else if (bodyData.BodyAsJson is { } jsonBody) { var anonymousObjectDefinition = ConvertToAnonymousObjectDefinition(jsonBody); sb.AppendLine($" .WithBodyAsJson({anonymousObjectDefinition})"); } break; ``` My solution is to load a JToken instead of a JObject in ConvertToAnonymousObjectDefinition, which seems like the original intention given that ConvertJsonToAnonymousObjectDefinition (which is defined in an internal class) expects a JToken and isn't called anywhere else in the code. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-29 09:19:19 +01:00
adam closed this issue 2025-12-29 09:19:19 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#1326