From 2d8dfb1ddcff40cfb5da4386d1ed11aa8fd91db0 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Fri, 17 May 2024 14:48:49 -0600 Subject: [PATCH] Fix System.Text.Json formatter's support for named arguments object Fixes #1028 --- src/StreamJsonRpc/SystemTextJsonFormatter.cs | 3 +-- test/StreamJsonRpc.Tests/JsonRpcTests.cs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/StreamJsonRpc/SystemTextJsonFormatter.cs b/src/StreamJsonRpc/SystemTextJsonFormatter.cs index 5e41a5fc..54893f80 100644 --- a/src/StreamJsonRpc/SystemTextJsonFormatter.cs +++ b/src/StreamJsonRpc/SystemTextJsonFormatter.cs @@ -487,9 +487,8 @@ public class SystemTextJsonFormatter : FormatterBase, IJsonRpcMessageFormatter, using (this.formatter.TrackDeserialization(this, parameters)) { // Support for opt-in to deserializing all named arguments into a single parameter. - if (parameters.Length == 1 && this.formatter.ApplicableMethodAttributeOnDeserializingMethod?.UseSingleObjectParameterDeserialization is true) + if (parameters.Length == 1 && this.formatter.ApplicableMethodAttributeOnDeserializingMethod?.UseSingleObjectParameterDeserialization is true && this.JsonArguments is not null) { - Assumes.NotNull(this.JsonArguments); typedArguments[0] = this.JsonArguments.Value.Deserialize(parameters[0].ParameterType, this.formatter.massagedUserDataSerializerOptions); return ArgumentMatchResult.Success; } diff --git a/test/StreamJsonRpc.Tests/JsonRpcTests.cs b/test/StreamJsonRpc.Tests/JsonRpcTests.cs index 4a795c5c..799598b0 100644 --- a/test/StreamJsonRpc.Tests/JsonRpcTests.cs +++ b/test/StreamJsonRpc.Tests/JsonRpcTests.cs @@ -1453,6 +1453,13 @@ public abstract class JsonRpcTests : TestBase Assert.Equal(7, sum); } + [Fact] + public async Task InvokeWithSingleObjectParameter_SupplyNoArgument() + { + int sum = await this.clientRpc.InvokeWithParameterObjectAsync(nameof(Server.MethodWithSingleObjectParameterWithDefaultValue), cancellationToken: this.TimeoutToken); + Assert.Equal(-1, sum); + } + [Fact] public async Task InvokeWithSingleObjectParameter_SendingExpectedObjectAndCancellationToken_InterfaceMethodAttributed() { @@ -3337,6 +3344,12 @@ public abstract class JsonRpcTests : TestBase return fields.x + fields.y; } + [JsonRpcMethod(UseSingleObjectParameterDeserialization = true)] + public static int MethodWithSingleObjectParameterWithDefaultValue(XAndYProperties? arg = null) + { + return arg is not null ? arg.x + arg.y : -1; + } + [JsonRpcMethod("test/MethodWithSingleObjectParameterWithProgress", UseSingleObjectParameterDeserialization = true)] public static int MethodWithSingleObjectParameterWithProgress(XAndYPropertiesWithProgress fields) {