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

Make ServiceInvoker treat Nullable<T> parameters as a match for null arguments #604

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -16,6 +16,11 @@ public int Increment()
return countingService.Increment();
}

public int Increment(int? number)
{
return countingService.Increment(number);
}

public int GetCurrentValue()
{
return countingService.GetCurrentValue();
Expand Down
21 changes: 20 additions & 1 deletion source/Halibut.TestUtils.Contracts/CountingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ public class CountingService : ICountingService
int count;
public int Increment()
{
return Interlocked.Increment(ref count);
return Increment(1);
}

public int Increment(int? number)
{
var increment = number ?? 1;
var counter = 0;

for (var i = 0; i < increment; i++)
{
counter = Interlocked.Increment(ref count);
}

return counter;
}

public int GetCurrentValue()
Expand All @@ -26,6 +39,12 @@ public async Task<int> IncrementAsync(CancellationToken cancellationToken)
return service.Increment();
}

public async Task<int> IncrementAsync(int? number, CancellationToken cancellationToken)
{
await Task.CompletedTask;
return service.Increment(number);
}

public async Task<int> GetCurrentValueAsync(CancellationToken cancellationToken)
{
await Task.CompletedTask;
Expand Down
2 changes: 2 additions & 0 deletions source/Halibut.TestUtils.Contracts/ICountingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ namespace Halibut.TestUtils.Contracts
public interface ICountingService
{
public int Increment();
public int Increment(int? number);
public int GetCurrentValue();
}

public interface IAsyncCountingService
{
public Task<int> IncrementAsync(CancellationToken cancellationToken);
public Task<int> IncrementAsync(int? number, CancellationToken cancellationToken);
public Task<int> GetCurrentValueAsync(CancellationToken cancellationToken);
}
}
20 changes: 20 additions & 0 deletions source/Halibut.Tests/ServiceModel/ServiceInvokerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ public async Task AsyncInvokeWithNoParamsOnAsyncService()
response.Result.Should().Be(1);
}

[Test]
public async Task AsyncInvokeWithNullableParamsOnAsyncService()
{
var serviceFactory = new ServiceFactoryBuilder()
.WithConventionVerificationDisabled()
.WithService<ICountingService, IAsyncCountingService>(() => new AsyncCountingService())
.Build();

var sut = new ServiceInvoker(serviceFactory);
var request = new RequestMessage()
{
ServiceName = nameof(ICountingService),
MethodName = nameof(ICountingService.Increment),
Params = new object[] { null! },
};

var response = await sut.InvokeAsync(request);
response.Result.Should().Be(1);
}

[Test]
public async Task AsyncInvokeWithNoParams_AsyncServiceMissingSuffix()
{
Expand Down
5 changes: 5 additions & 0 deletions source/Halibut/Portability/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ public static bool IsValueType(this Type type)
{
return type.IsValueType;
}

public static bool IsNullable(this Type type)
{
return !type.IsValueType() || Nullable.GetUnderlyingType(type) != null;
}
}
}
2 changes: 1 addition & 1 deletion source/Halibut/ServiceModel/ServiceInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static MethodInfo SelectAsyncMethod(IList<MethodInfo> methods, RequestMessage re
{
var paramType = parameters[i].ParameterType;
var argType = argumentTypes[i];
if (argType == null && paramType.IsValueType())
if (argType == null && !paramType.IsNullable())
{
isMatch = false;
break;
Expand Down