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

add nullable annotation in generated code #194

Merged
merged 4 commits into from
Mar 31, 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
6 changes: 3 additions & 3 deletions src/TypedSignalR.Client/CodeAnalysis/MethodMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public MethodMetadata(IMethodSymbol methodSymbol)
.Select(x => new ParameterMetadata(x))
.ToArray();

ReturnType = methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
ReturnType = methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormatRule.FullyQualifiedNullableFormat);

INamedTypeSymbol? returnTypeSymbol = methodSymbol.ReturnType as INamedTypeSymbol;

Expand All @@ -36,8 +36,8 @@ public MethodMetadata(IMethodSymbol methodSymbol)
if (returnTypeSymbol.IsGenericType)
{
GenericReturnTypeArgument = returnTypeSymbol.TypeArguments.Length == 1
? returnTypeSymbol.TypeArguments[0].ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)
: string.Join(", ", returnTypeSymbol.TypeArguments.Select(x => x.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)));
? returnTypeSymbol.TypeArguments[0].ToDisplayString(SymbolDisplayFormatRule.FullyQualifiedNullableFormat)
: string.Join(", ", returnTypeSymbol.TypeArguments.Select(x => x.ToDisplayString(SymbolDisplayFormatRule.FullyQualifiedNullableFormat)));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/TypedSignalR.Client/CodeAnalysis/ParameterMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public ParameterMetadata(IParameterSymbol parameterSymbol)
? $"@{parameterSymbol.Name}"
: parameterSymbol.Name;

TypeName = parameterSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
TypeName = parameterSymbol.Type.ToDisplayString(SymbolDisplayFormatRule.FullyQualifiedNullableFormat);
}
}
11 changes: 11 additions & 0 deletions src/TypedSignalR.Client/CodeAnalysis/SymbolDisplayFormatRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.CodeAnalysis;

namespace TypedSignalR.Client.CodeAnalysis;

internal static class SymbolDisplayFormatRule
{
public static SymbolDisplayFormat FullyQualifiedFormat { get; } = SymbolDisplayFormat.FullyQualifiedFormat;

public static SymbolDisplayFormat FullyQualifiedNullableFormat { get; } = SymbolDisplayFormat.FullyQualifiedFormat
.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier);
}
2 changes: 1 addition & 1 deletion src/TypedSignalR.Client/CodeAnalysis/TypeMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public TypeMetadata(ITypeSymbol typeSymbol)
Methods = GetMethods(typeSymbol);

InterfaceName = typeSymbol.Name;
InterfaceFullName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
InterfaceFullName = typeSymbol.ToDisplayString(SymbolDisplayFormatRule.FullyQualifiedFormat);
CollisionFreeName = InterfaceFullName.Replace('.', '_').Replace(':', '_');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand Down Expand Up @@ -36,6 +36,7 @@
<Compile Include="..\TypedSignalR.Client.Tests\Hubs\ReceiverTest.cs" LinkBase="Hubs" />
<Compile Include="..\TypedSignalR.Client.Tests\Hubs\StreamingTest.cs" LinkBase="Hubs" />
<Compile Include="..\TypedSignalR.Client.Tests\Hubs\UnaryTest.cs" LinkBase="Hubs" />
<Compile Include="..\TypedSignalR.Client.Tests\Hubs\NullableHubTest.cs" LinkBase="Hubs" />
</ItemGroup>

</Project>
37 changes: 37 additions & 0 deletions tests/TypedSignalR.Client.Tests.Server/Hubs/NullableTestHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.SignalR;
using TypedSignalR.Client.Tests.Shared;

namespace TypedSignalR.Client.Tests.Server.Hubs;

public class NullableTestHub : Hub, INullableTestHub
{
public Task<int> GetStruct(int message)
{
return Task.FromResult(message + 7);
}

public Task<int?> GetNullableStruct(int? message)
{
if (message is null)
{
return Task.FromResult<int?>(null);
}

return Task.FromResult<int?>(message.Value + 99);
}

public Task<string> GetReferenceType(string message)
{
return Task.FromResult(message + "7");
}

public Task<string?> GetNullableReferenceType(string? message)
{
if (message is null)
{
return Task.FromResult<string?>(null);
}

return Task.FromResult<string?>(message + "99");
}
}
1 change: 1 addition & 0 deletions tests/TypedSignalR.Client.Tests.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
app.MapHub<ClientResultsTestHub>("/Hubs/ClientResultsTestHub");
app.MapHub<InheritTestHub>("/Hubs/InheritTestHub");
app.MapHub<InheritReceiverTestHub>("/Hubs/InheritReceiverTestHub");
app.MapHub<NullableTestHub>("/Hubs/NullableTestHub");

app.Run();
10 changes: 10 additions & 0 deletions tests/TypedSignalR.Client.Tests.Shared/INullableTestHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TypedSignalR.Client.Tests.Shared;

public interface INullableTestHub
{
Task<int> GetStruct(int message);
Task<int?> GetNullableStruct(int? message);

Task<string> GetReferenceType(string message);
Task<string?> GetNullableReferenceType(string? message);
}
125 changes: 125 additions & 0 deletions tests/TypedSignalR.Client.Tests/Hubs/NullableHubTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Connections;
using TypedSignalR.Client.Tests.Shared;
using Xunit;

namespace TypedSignalR.Client.Tests.Hubs;

public class NullableHubTest : IntegrationTestBase, IDisposable
{
private readonly CancellationTokenSource _cancellationTokenSource = new();

public void Dispose()
{
_cancellationTokenSource.Cancel();
}

[Fact]
public async Task GetStruct()
{
var hubConnection = CreateHubConnection("/Hubs/NullableTestHub", HttpTransportType.WebSockets);

var hubProxy = hubConnection.CreateHubProxy<INullableTestHub>(_cancellationTokenSource.Token);

await hubConnection.StartAsync(_cancellationTokenSource.Token);

var x = Random.Shared.Next();

var value = await hubProxy.GetStruct(x);

Assert.Equal(x + 7, value);

await hubConnection.StopAsync(_cancellationTokenSource.Token);
}

[Fact]
public async Task GetNullableStruct1()
{
var hubConnection = CreateHubConnection("/Hubs/NullableTestHub", HttpTransportType.WebSockets);

var hubProxy = hubConnection.CreateHubProxy<INullableTestHub>(_cancellationTokenSource.Token);

await hubConnection.StartAsync(_cancellationTokenSource.Token);

var x = Random.Shared.Next();

var value = await hubProxy.GetNullableStruct(x);

Assert.Equal(x + 99, value);

await hubConnection.StopAsync(_cancellationTokenSource.Token);
}

[Fact]
public async Task GetNullableStruct2()
{
var hubConnection = CreateHubConnection("/Hubs/NullableTestHub", HttpTransportType.WebSockets);

var hubProxy = hubConnection.CreateHubProxy<INullableTestHub>(_cancellationTokenSource.Token);

await hubConnection.StartAsync(_cancellationTokenSource.Token);

var value = await hubProxy.GetNullableStruct(null);

Assert.Null(value);

await hubConnection.StopAsync(_cancellationTokenSource.Token);
}

[Fact]
public async Task GetReferenceType()
{
var hubConnection = CreateHubConnection("/Hubs/NullableTestHub", HttpTransportType.WebSockets);

var hubProxy = hubConnection.CreateHubProxy<INullableTestHub>(_cancellationTokenSource.Token);

await hubConnection.StartAsync(_cancellationTokenSource.Token);

var message = Guid.NewGuid().ToString();

var value = await hubProxy.GetReferenceType(message);

Assert.Equal(message + "7", value);

await hubConnection.StopAsync(_cancellationTokenSource.Token);
}

[Fact]
public async Task GetNullableReferenceType1()
{
var hubConnection = CreateHubConnection("/Hubs/NullableTestHub", HttpTransportType.WebSockets);

var hubProxy = hubConnection.CreateHubProxy<INullableTestHub>(_cancellationTokenSource.Token);

await hubConnection.StartAsync(_cancellationTokenSource.Token);

var message = Guid.NewGuid().ToString();

var value = await hubProxy.GetNullableReferenceType(message);

Assert.Equal(message + "99", value);

await hubConnection.StopAsync(_cancellationTokenSource.Token);
}

[Fact]
public async Task GetNullableReferenceType2()
{
var hubConnection = CreateHubConnection("/Hubs/NullableTestHub", HttpTransportType.WebSockets);

var hubProxy = hubConnection.CreateHubProxy<INullableTestHub>(_cancellationTokenSource.Token);

await hubConnection.StartAsync(_cancellationTokenSource.Token);

var value = await hubProxy.GetNullableReferenceType(null);

Assert.Null(value);

await hubConnection.StopAsync(_cancellationTokenSource.Token);
}
}
Loading