Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exceptions being thrown when ETW attempts to log messages with System.Text.Json #1039

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions src/StreamJsonRpc/SystemTextJsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ public override bool TryGetArgumentByNameOrIndex(string? name, int position, Typ
valueElement = propertyValue;
}

break;
case JsonValueKind.Object when name is null && position is 0:
valueElement = this.JsonArguments.Value;
break;
case JsonValueKind.Array:
int elementIndex = 0;
Expand Down
27 changes: 27 additions & 0 deletions test/StreamJsonRpc.Tests/SystemTextJsonFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Buffers;
using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -68,6 +69,32 @@ public void STJAttributesWinOverDataMemberWithoutDataContract()
Assert.Equal(1, doc.RootElement.GetProperty("params")[0].GetProperty("B").GetInt32());
}

[Fact]
public void STJPositionalArgsInJsonRpcEventSource()
{
// Create a mock request.
string jsonRequest = """
{
"jsonrpc":"2.0",
"id":3,
"method":"someMethod",
"params":
{
"someValue": 5
}
}
""";
ReadOnlySequence<byte> jsonSequence = new ReadOnlySequence<byte>(this.Formatter.Encoding.GetBytes(jsonRequest));
var jsonMessage = (JsonRpcRequest)this.Formatter.Deserialize(jsonSequence);

// JsonRpcEventSource calls TryGetArgumentByNameOrIndex to get a string representation of the message.
// It is possible to get a positional parameters message (name is null) with a single arguments represented as a JsonValueKind.Object.
Assert.True(jsonMessage.TryGetArgumentByNameOrIndex(null, 0, null, out object? value));
Assert.IsType<JsonElement>(value);

Assert.Equal(5, ((JsonElement)value).GetProperty("someValue").GetInt32());
}

protected override SystemTextJsonFormatter CreateFormatter() => new();

[DataContract]
Expand Down