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
4 changes: 2 additions & 2 deletions src/MagicOnion.Internal/GrpcMethodHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using Grpc.Core;
using MagicOnion.Serialization;
using MessagePack;
Expand All @@ -9,7 +9,7 @@ internal static class GrpcMethodHelper
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TRaw ToRaw<T, TRaw>(T obj)
=> (obj is ValueType)
=> (typeof(T).IsValueType)
? (TRaw)(object)Box.Create(obj)
: DangerousDummyNull.GetObjectOrDummyNull(Unsafe.As<T, TRaw>(ref obj));

Expand Down
58 changes: 57 additions & 1 deletion tests/MagicOnion.Client.Tests/UnaryTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Grpc.Core;
using Grpc.Core;
using MagicOnion.Internal;
using NSubstitute;

Expand Down Expand Up @@ -465,6 +465,61 @@ public async Task ThrowsResponse()
_ = callInvokerMock.ReceivedWithAnyArgs().AsyncUnaryCall(default(Method<Box<Nil>, Box<int>>)!, default, default, default!);
}

[Fact]
public async Task Issue1021_AccessViolationException()
{
// Arrange
var callInvokerMock = Substitute.For<CallInvoker>();
callInvokerMock.AsyncUnaryCall(Arg.Any<Method<Box<int?>, Box<Nil>>>(), Arg.Any<string>(), Arg.Any<CallOptions>(), GrpcMethodHelper.ToRaw<int?, Box<int?>>(null))
.Returns(new AsyncUnaryCall<Box<Nil>>(Task.FromResult(Box.Create(Nil.Default)), Task.FromResult(Metadata.Empty), () => Status.DefaultSuccess, () => Metadata.Empty, () => { }));

var a = callInvokerMock.AsyncUnaryCall(new Method<Box<int?>, Box<Nil>>(MethodType.Unary, "", "", new Marshaller<Box<int?>>(x => [], x => Box.Create<int?>(null)), new Marshaller<Box<Nil>>(x => [], x => Box.Create<Nil>(default))), "", new CallOptions(), GrpcMethodHelper.ToRaw<int?, Box<int?>>(null));

// Act & Assert
var client = MagicOnionClient.Create<IUnaryTestService>(callInvokerMock);
for (var i = 0; i < 100000; i++)
{
await client.OneNullableValueTypeParameterNonGenericReturnType(null);
}
}

[Fact]
public async Task GrpcMethodHelper_NullableValueType()
{
for (var i = 0; i < 100000; i++)
{
var boxed = GrpcMethodHelper.ToRaw<int?, Box<int?>>(null);
var unboxed = GrpcMethodHelper.FromRaw<Box<int?>, int?>(boxed);
Assert.NotNull(boxed);
Assert.Null(boxed.Value);
Assert.Null(unboxed);
}
}

[Fact]
public async Task GrpcMethodHelper_ValueType()
{
for (var i = 0; i < 100000; i++)
{
var boxed = GrpcMethodHelper.ToRaw<int, Box<int>>(12345);
var unboxed = GrpcMethodHelper.FromRaw<Box<int>, int>(boxed);
Assert.Equal(12345, boxed.Value);
Assert.Equal(12345, unboxed);
}
}

[Fact]
public async Task GrpcMethodHelper_RefType()
{
for (var i = 0; i < 100000; i++)
{
var boxed = GrpcMethodHelper.ToRaw<string?, string?>(null);
var unboxed = GrpcMethodHelper.FromRaw<string?, string?>(boxed);
Assert.NotNull(boxed);
Assert.Null(unboxed);
}
}

public interface IUnaryTestService : IService<IUnaryTestService>
{
UnaryResult<Nil> ParameterlessReturnNil();
Expand All @@ -480,6 +535,7 @@ public interface IUnaryTestService : IService<IUnaryTestService>
UnaryResult ParameterlessNonGenericReturnType();
UnaryResult OneRefTypeParameterNonGenericReturnType(string arg1);
UnaryResult OneValueTypeParameterNonGenericReturnType(int arg1);
UnaryResult OneNullableValueTypeParameterNonGenericReturnType(int? arg1);
UnaryResult TwoParametersNonGenericReturnType(int arg1, string arg2);
}

Expand Down
Loading