diff --git a/Directory.Packages.props b/Directory.Packages.props index b66012f..eee227f 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -1,32 +1,33 @@ - - - true - true - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/TypedSignalR.Client.TypeScript/Templates/ApiTemplate.cs b/src/TypedSignalR.Client.TypeScript/Templates/ApiTemplate.cs index 4db6087..364e252 100644 --- a/src/TypedSignalR.Client.TypeScript/Templates/ApiTemplate.cs +++ b/src/TypedSignalR.Client.TypeScript/Templates/ApiTemplate.cs @@ -144,7 +144,7 @@ public constructor( this.Write("\r\n"); foreach(var method in receiverType.Methods) { this.Write(" connection.on(\""); - this.Write(this.ToStringHelper.ToStringWithCulture(method.Name)); + this.Write(this.ToStringHelper.ToStringWithCulture(method.GetSignalRMethodName())); this.Write("\", __"); this.Write(this.ToStringHelper.ToStringWithCulture(method.Name.Format(Options.NamingStyle))); this.Write(");\r\n"); @@ -152,7 +152,7 @@ public constructor( this.Write("\r\n const methodList: ReceiverMethod[] = [\r\n"); for(int i = 0; i < receiverType.Methods.Count; i++) { this.Write(" { methodName: \""); - this.Write(this.ToStringHelper.ToStringWithCulture(receiverType.Methods[i].Name)); + this.Write(this.ToStringHelper.ToStringWithCulture(receiverType.Methods[i].GetSignalRMethodName())); this.Write("\", method: __"); this.Write(this.ToStringHelper.ToStringWithCulture(receiverType.Methods[i].Name.Format(Options.NamingStyle))); this.Write(" }"); diff --git a/src/TypedSignalR.Client.TypeScript/Templates/ApiTemplate.tt b/src/TypedSignalR.Client.TypeScript/Templates/ApiTemplate.tt index 2991442..8ebc77b 100644 --- a/src/TypedSignalR.Client.TypeScript/Templates/ApiTemplate.tt +++ b/src/TypedSignalR.Client.TypeScript/Templates/ApiTemplate.tt @@ -115,12 +115,12 @@ class <#= receiverType.Name #>_Binder implements ReceiverRegister<<#= receiverTy <# } #> <# foreach(var method in receiverType.Methods) { #> - connection.on("<#= method.Name #>", __<#= method.Name.Format(Options.NamingStyle) #>); + connection.on("<#= method.GetSignalRMethodName() #>", __<#= method.Name.Format(Options.NamingStyle) #>); <# } #> const methodList: ReceiverMethod[] = [ <# for(int i = 0; i < receiverType.Methods.Count; i++) { #> - { methodName: "<#= receiverType.Methods[i].Name #>", method: __<#= receiverType.Methods[i].Name.Format(Options.NamingStyle) #> }<#= i != receiverType.Methods.Count - 1 ? "," : "" #> + { methodName: "<#= receiverType.Methods[i].GetSignalRMethodName() #>", method: __<#= receiverType.Methods[i].Name.Format(Options.NamingStyle) #> }<#= i != receiverType.Methods.Count - 1 ? "," : "" #> <# } #> ] diff --git a/src/TypedSignalR.Client.TypeScript/Templates/MethodSymbolExtensions.cs b/src/TypedSignalR.Client.TypeScript/Templates/MethodSymbolExtensions.cs index d49e2b2..8b1158f 100644 --- a/src/TypedSignalR.Client.TypeScript/Templates/MethodSymbolExtensions.cs +++ b/src/TypedSignalR.Client.TypeScript/Templates/MethodSymbolExtensions.cs @@ -6,6 +6,23 @@ namespace TypedSignalR.Client.TypeScript.Templates; internal static class MethodSymbolExtensions { + public static string GetSignalRMethodName(this IMethodSymbol methodSymbol) + { + var hubMethodName = methodSymbol + .GetAttributes() + .FirstOrDefault(x => + x.AttributeClass?.ToDisplayString() == + "Microsoft.AspNetCore.SignalR.HubMethodNameAttribute"); + + if (hubMethodName?.ConstructorArguments.Length == 1 && + hubMethodName.ConstructorArguments[0].Value is string name) + { + return name; + } + + return methodSymbol.Name; + } + public static string TranslateReceiverMethodIntoLambdaExpressionSyntax(this IMethodSymbol receiverMethodSymbol, SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options) { if (receiverMethodSymbol.Parameters.Length == 0) @@ -100,39 +117,42 @@ private static string ReturnTypeToTypeScriptString(this IMethodSymbol methodSymb private static string CreateUnaryMethodString(IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options) { var name = methodSymbol.Name.Format(options.MethodStyle); + var signalRMethodName = methodSymbol.GetSignalRMethodName(); var parameters = methodSymbol.ParametersToTypeScriptString(specialSymbols, options); var returnType = methodSymbol.ReturnTypeToTypeScriptString(specialSymbols, options); var args = methodSymbol.ParametersToTypeScriptArgumentString(specialSymbols, options); return $@" public readonly {name} = async ({parameters}): {returnType} => {{ - return await this.connection.invoke(""{methodSymbol.Name}""{args}); + return await this.connection.invoke(""{signalRMethodName}""{args}); }}"; } private static string CreateServerToClientStreamingMethodString(IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options) { var name = methodSymbol.Name.Format(options.MethodStyle); + var signalRMethodName = methodSymbol.GetSignalRMethodName(); var parameters = methodSymbol.ParametersToTypeScriptString(specialSymbols, options); var returnType = methodSymbol.ReturnTypeToTypeScriptString(specialSymbols, options); var args = methodSymbol.ParametersToTypeScriptArgumentString(specialSymbols, options); return $@" public readonly {name} = ({parameters}): {returnType} => {{ - return this.connection.stream(""{methodSymbol.Name}""{args}); + return this.connection.stream(""{signalRMethodName}""{args}); }}"; } private static string CreateClientToServerStreamingMethodString(IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options) { var name = methodSymbol.Name.Format(options.MethodStyle); + var signalRMethodName = methodSymbol.GetSignalRMethodName(); var parameters = methodSymbol.ParametersToTypeScriptString(specialSymbols, options); var returnType = methodSymbol.ReturnTypeToTypeScriptString(specialSymbols, options); var args = methodSymbol.ParametersToTypeScriptArgumentString(specialSymbols, options); return $@" public readonly {name} = async ({parameters}): {returnType} => {{ - return await this.connection.send(""{methodSymbol.Name}""{args}); + return await this.connection.send(""{signalRMethodName}""{args}); }}"; } } diff --git a/tests/TypeScriptTests/src/json/unary.test.ts b/tests/TypeScriptTests/src/json/unary.test.ts index 8f1f153..b0ab18e 100644 --- a/tests/TypeScriptTests/src/json/unary.test.ts +++ b/tests/TypeScriptTests/src/json/unary.test.ts @@ -30,6 +30,9 @@ const testMethod = async () => { const r1 = await hubProxy.get(); expect(r1).toEqual("TypedSignalR.Client.TypeScript"); + const renamed = await hubProxy.getWithCustomName(); + expect(renamed).toEqual("HubMethodNameAttribute"); + const x = getRandomInt(1000); const y = getRandomInt(1000); diff --git a/tests/TypeScriptTests/src/msgpack/unary.test.ts b/tests/TypeScriptTests/src/msgpack/unary.test.ts index 882742c..8118d37 100644 --- a/tests/TypeScriptTests/src/msgpack/unary.test.ts +++ b/tests/TypeScriptTests/src/msgpack/unary.test.ts @@ -24,6 +24,9 @@ const testMethod = async () => { const r1 = await hubProxy.get(); expect(r1).toEqual("TypedSignalR.Client.TypeScript"); + const renamed = await hubProxy.getWithCustomName(); + expect(renamed).toEqual("HubMethodNameAttribute"); + const x = getRandomInt(1000); const y = getRandomInt(1000); diff --git a/tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/UnaryHub.cs b/tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/UnaryHub.cs index 160ab9b..28433be 100644 --- a/tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/UnaryHub.cs +++ b/tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/UnaryHub.cs @@ -47,6 +47,14 @@ public Task Get() return Task.FromResult("TypedSignalR.Client.TypeScript"); } + [HubMethodName("UnaryHub_GetWithCustomName")] + public Task GetWithCustomName() + { + _logger.Log(LogLevel.Information, "UnaryHub.GetWithCustomName"); + + return Task.FromResult("HubMethodNameAttribute"); + } + public Task RequestArray(MyRequestItem[] array) { var buffer = new MyResponseItem[array.Length]; diff --git a/tests/TypedSignalR.Client.TypeScript.Tests.Shared/IUnaryHub.cs b/tests/TypedSignalR.Client.TypeScript.Tests.Shared/IUnaryHub.cs index bb8baea..ed8e546 100644 --- a/tests/TypedSignalR.Client.TypeScript.Tests.Shared/IUnaryHub.cs +++ b/tests/TypedSignalR.Client.TypeScript.Tests.Shared/IUnaryHub.cs @@ -46,6 +46,8 @@ public class MyResponseItem2 public partial interface IUnaryHub { Task Get(); + [Microsoft.AspNetCore.SignalR.HubMethodName("UnaryHub_GetWithCustomName")] + Task GetWithCustomName(); Task Add(int x, int y); Task Cat(string x, string y); } diff --git a/tests/TypedSignalR.Client.TypeScript.Tests.Shared/TypedSignalR.Client.TypeScript.Tests.Shared.csproj b/tests/TypedSignalR.Client.TypeScript.Tests.Shared/TypedSignalR.Client.TypeScript.Tests.Shared.csproj index 484cf20..658a34a 100644 --- a/tests/TypedSignalR.Client.TypeScript.Tests.Shared/TypedSignalR.Client.TypeScript.Tests.Shared.csproj +++ b/tests/TypedSignalR.Client.TypeScript.Tests.Shared/TypedSignalR.Client.TypeScript.Tests.Shared.csproj @@ -10,4 +10,8 @@ + + + +