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

Support inheritance #163

Merged
merged 2 commits into from
Jan 12, 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
17 changes: 16 additions & 1 deletion src/TypedSignalR.Client.TypeScript/RoslynExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,24 @@ public static IEnumerable<ISymbol> GetPublicFieldsAndProperties(this INamedTypeS

public static IEnumerable<IMethodSymbol> GetMethods(this INamedTypeSymbol source)
{
return source.GetMembers()
var methods = source.GetMembers()
.OfType<IMethodSymbol>()
.Where(static x => x.MethodKind == MethodKind.Ordinary);

var allInterfaces = source.AllInterfaces;

if (allInterfaces.IsEmpty)
{
return methods;
}

var allMethods = allInterfaces
.SelectMany(static x => x.GetMembers())
.OfType<IMethodSymbol>()
.Where(static x => x.MethodKind is MethodKind.Ordinary)
.Concat(methods);

return allMethods;
}

public static IEnumerable<ISymbol> IgnoreStatic(this IEnumerable<ISymbol> source)
Expand Down
63 changes: 63 additions & 0 deletions tests/TypeScriptTests/src/json/Inherit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { HubConnectionBuilder } from '@microsoft/signalr'
import { getHubProxyFactory } from '../generated/json/TypedSignalR.Client'
import { MyEnum, MyRequestItem, MyRequestItem2, UserDefinedType } from '../generated/json/TypedSignalR.Client.TypeScript.Tests.Shared';
import crypto from 'crypto'

const getRandomInt = (max: number) => {
return Math.floor(Math.random() * max);
}

const toUTCString = (date: string | Date): string => {
if (typeof date === 'string') {
const d = new Date(date);
return d.toUTCString();
}

return date.toUTCString();
}

const testMethod = async () => {
const connection = new HubConnectionBuilder()
.withUrl("http://localhost:5000/hubs/InheritHub")
.build();

const hubProxy = getHubProxyFactory("IInheritHub")
.createHubProxy(connection);

try {
await connection.start();

const r1 = await hubProxy.get();
expect(r1).toEqual("TypedSignalR.Client.TypeScript");

const x = getRandomInt(1000);
const y = getRandomInt(1000);

const r2 = await hubProxy.add(x, y);
expect(r2).toEqual(x + y);

const s1 = "revue";
const s2 = "starlight";

const r3 = await hubProxy.cat(s1, s2);;

expect(r3).toEqual(s1 + s2);

const instance: UserDefinedType = {
dateTime: new Date(),
guid: crypto.randomUUID()
}

const r4 = await hubProxy.echo(instance);

instance.dateTime = toUTCString(instance.dateTime)
r4.dateTime = toUTCString(r4.dateTime)

expect(r4).toEqual(instance)
}
finally {
await connection.stop();
}
}

test('unary.test', testMethod);
2 changes: 1 addition & 1 deletion tests/TypeScriptTests/src/json/unary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const testMethod = async () => {

const r4 = await hubProxy.echo(instance);

instance.dateTime = toUTCString(r4.dateTime)
instance.dateTime = toUTCString(instance.dateTime)
r4.dateTime = toUTCString(r4.dateTime)

expect(r4).toEqual(instance)
Expand Down
62 changes: 62 additions & 0 deletions tests/TypeScriptTests/src/msgpack/Inherit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { HubConnectionBuilder } from '@microsoft/signalr'
import { getHubProxyFactory } from '../generated/msgpack/TypedSignalR.Client'
import { UserDefinedType } from '../generated/msgpack/TypedSignalR.Client.TypeScript.Tests.Shared';
import crypto from 'crypto'
import { MessagePackHubProtocol } from '@microsoft/signalr-protocol-msgpack';

const getRandomInt = (max: number) => {
return Math.floor(Math.random() * max);
}

const toUTCString = (date: string | Date): string => {
if (typeof date === 'string') {
const d = new Date(date);
return d.toUTCString();
}

return date.toUTCString();
}

const testMethod = async () => {
const connection = new HubConnectionBuilder()
.withUrl("http://localhost:5000/hubs/InheritHub")
.withHubProtocol(new MessagePackHubProtocol())
.build();

const hubProxy = getHubProxyFactory("IInheritHub")
.createHubProxy(connection);

try {
await connection.start();

const r1 = await hubProxy.get();
expect(r1).toEqual("TypedSignalR.Client.TypeScript");

const x = getRandomInt(1000);
const y = getRandomInt(1000);

const r2 = await hubProxy.add(x, y);
expect(r2).toEqual(x + y);

const s1 = "revue";
const s2 = "starlight";

const r3 = await hubProxy.cat(s1, s2);;

expect(r3).toEqual(s1 + s2);

const instance: UserDefinedType = {
DateTime: new Date(),
Guid: crypto.randomUUID()
}

const r4 = await hubProxy.echo(instance);

expect(r4).toEqual(instance)
}
finally {
await connection.stop();
}
}

test('unary.test', testMethod);
3 changes: 0 additions & 3 deletions tests/TypeScriptTests/src/msgpack/unary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ const testMethod = async () => {

const r4 = await hubProxy.echo(instance);

instance.DateTime = r4.DateTime
r4.DateTime = r4.DateTime

expect(r4).toEqual(instance)

const r5 = await hubProxy.echoMyEnum(MyEnum.Four);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.SignalR;
using TypedSignalR.Client.TypeScript.Tests.Shared;

namespace TypedSignalR.Client.TypeScript.Tests.Server.Hubs;

public class InheritHub : Hub<IInheritHubReceiver>, IInheritHub
{
public Task<int> Add(int x, int y)
{
return Task.FromResult(x + y);
}

public Task<string> Cat(string x, string y)
{
return Task.FromResult(x + y);
}

public Task<UserDefinedType> Echo(UserDefinedType instance)
{
return Task.FromResult(instance);
}

public Task<string> Get()
{
return Task.FromResult("TypedSignalR.Client.TypeScript");
}
}
19 changes: 19 additions & 0 deletions tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/UnaryHub.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using TypedSignalR.Client.TypeScript.Tests.Shared;

namespace TypedSignalR.Client.TypeScript.Tests.Server.Hubs;
Expand Down Expand Up @@ -75,4 +76,22 @@ public Task<List<MyResponseItem2>> RequestList(List<MyRequestItem2> list)

return Task.FromResult(buffer);
}

public override Task OnConnectedAsync()
{
_logger.Log(LogLevel.Information, "UnaryHub.OnConnectedAsync");

return base.OnConnectedAsync();
}

public override Task OnDisconnectedAsync(Exception? exception)
{
//_logger.Log(LogLevel.Information, "UnaryHub.OnDisconnectedAsync");
if (exception is not null)
{
_logger.LogError(exception, "UnaryHub.OnDisconnectedAsync");
}

return base.OnDisconnectedAsync(exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@
app.MapHub<StreamingHub>("/hubs/StreamingHub");
app.MapHub<ClientResultsTestHub>("/hubs/ClientResultsTestHub");
app.MapHub<NestedTypeHub>("/hubs/NestedTypeHub");
app.MapHub<InheritHub>("/hubs/InheritHub");

app.Run();
50 changes: 50 additions & 0 deletions tests/TypedSignalR.Client.TypeScript.Tests.Shared/IInheritHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TypedSignalR.Client.TypeScript.Tests.Shared;

public interface IHubBaseBase
{
Task<string> Get();
}

public interface IHubBase1 : IHubBaseBase
{
Task<int> Add(int x, int y);
}

public interface IHubBase2 : IHubBaseBase
{
Task<string> Cat(string x, string y);
}

[Hub]
public interface IInheritHub : IHubBase1, IHubBase2
{
Task<UserDefinedType> Echo(UserDefinedType instance);
}


public interface IReceiverBaseBase
{
Task ReceiveMessage(string message, int value);
}

public interface IReceiverBase1 : IReceiverBaseBase
{
Task ReceiveCustomMessage(UserDefinedType userDefined);
}

public interface IReceiverBase2 : IReceiverBaseBase
{
Task Notify();
}

[Receiver]
public interface IInheritHubReceiver : IReceiverBase1, IReceiverBase2
{
Task ReceiveMessage2(string message, int value);
}