Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ private static void RegisterDefaultSerializers(Dictionary<Type, JsonSerializer>
return
[
(JsonRpcStrings.JsonRpc, "2.0"),
(JsonRpcStrings.Code, error.ErrorCode),
(JsonRpcStrings.Id, error.Id),
(JsonRpcStrings.Error, errorMsg)
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ private static void RegisterRpcMessageSerializers()
Dictionary<string, object?> values = new()
{
[JsonRpcStrings.JsonRpc] = "2.0",
[JsonRpcStrings.Code] = error.ErrorCode,
[JsonRpcStrings.Id] = error.Id,
[JsonRpcStrings.Error] = new Dictionary<string, object?>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ private static void AssertSerialize(Type type, string instanceSerialized)

if (type == typeof(ErrorMessage))
{
Assert.AreEqual("""{"jsonrpc":"2.0","code":2,"id":1,"error":{"code":2,"data":{},"message":"This is error"}}""".Replace(" ", string.Empty), instanceSerialized, because);
// Per JSON-RPC 2.0 §5.1, "code" must only appear inside the nested "error" object,
// never at the top level of the response envelope.
Assert.AreEqual("""{"jsonrpc":"2.0","id":1,"error":{"code":2,"data":{},"message":"This is error"}}""".Replace(" ", string.Empty), instanceSerialized, because);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using Microsoft.Testing.Platform.ServerMode;
using Microsoft.Testing.Platform.ServerMode.Json;

using JsonDocument = System.Text.Json.JsonDocument;
using JsonElement = System.Text.Json.JsonElement;
Comment thread
Evangelink marked this conversation as resolved.

using TestNode = Microsoft.Testing.Platform.Extensions.Messages.TestNode;

namespace Microsoft.Testing.Platform.UnitTests;
Expand Down Expand Up @@ -116,6 +119,56 @@ public async Task Serialize_ArrayOfObjects()
Assert.AreEqual("""[{"name":"Thomas","children":[{"name":"Ruth","children":null}]},[2],3]""", actual);
}

[TestMethod]
public async Task Serialize_ErrorMessage_EmitsJsonRpc20CompliantShape()
{
// Arrange
ErrorMessage errorMessage = new(
Id: 42,
ErrorCode: -32601,
Message: "Method not found",
Data: null);

// Act
string actual = await _json.SerializeAsync(errorMessage);

// Assert
// Per JSON-RPC 2.0 §5.1, "code" MUST live inside the "error" object and MUST NOT
// appear at the top level of the response envelope.
using var document = JsonDocument.Parse(actual);
JsonElement root = document.RootElement;

Assert.AreEqual("2.0", root.GetProperty("jsonrpc").GetString());
Assert.AreEqual(42, root.GetProperty("id").GetInt32());
Assert.IsFalse(
root.TryGetProperty("code", out _),
"Top-level 'code' must not be present; it belongs inside the 'error' object per JSON-RPC 2.0 §5.1.");

JsonElement error = root.GetProperty("error");
Assert.AreEqual(-32601, error.GetProperty("code").GetInt32());
Assert.AreEqual("Method not found", error.GetProperty("message").GetString());
}

[TestMethod]
public async Task Serialize_ErrorMessage_RoundTripsViaDeserializer()
{
// Arrange
ErrorMessage original = new(
Id: 7,
ErrorCode: -32000,
Message: "Server error",
Data: null);

// Act
string serialized = await _json.SerializeAsync(original);
ErrorMessage roundTripped = _json.Deserialize<ErrorMessage>(serialized.AsMemory());

// Assert
Assert.AreEqual(original.Id, roundTripped.Id);
Assert.AreEqual(original.ErrorCode, roundTripped.ErrorCode);
Assert.AreEqual(original.Message, roundTripped.Message);
}

[TestMethod]
public void DeserializePerson()
{
Expand Down