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 System.Text.Json formatter's support for named arguments object #1045

Merged
merged 1 commit into from
May 17, 2024
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
3 changes: 1 addition & 2 deletions src/StreamJsonRpc/SystemTextJsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,8 @@ public override ArgumentMatchResult TryGetTypedArguments(ReadOnlySpan<ParameterI
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;
}
Expand Down
13 changes: 13 additions & 0 deletions test/StreamJsonRpc.Tests/JsonRpcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,13 @@ public async Task InvokeWithSingleObjectParameter_SendingExpectedObjectAndCancel
Assert.Equal(7, sum);
}

[Fact]
public async Task InvokeWithSingleObjectParameter_SupplyNoArgument()
{
int sum = await this.clientRpc.InvokeWithParameterObjectAsync<int>(nameof(Server.MethodWithSingleObjectParameterWithDefaultValue), cancellationToken: this.TimeoutToken);
Assert.Equal(-1, sum);
}

[Fact]
public async Task InvokeWithSingleObjectParameter_SendingExpectedObjectAndCancellationToken_InterfaceMethodAttributed()
{
Expand Down Expand Up @@ -3337,6 +3344,12 @@ public static int MethodWithSingleObjectParameterAndCancellationToken(XAndYPrope
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)
{
Expand Down