From 0763b5aacd30fc61c69dbb14ee82c8be2dbfaae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Saulius=20Menkevic=CC=8Cius?= Date: Sat, 26 Dec 2020 20:33:33 +0200 Subject: [PATCH] Fix invalid cast issue in omnisharp-roslyn by fixing LspHandlerDescriptor .ctor The issue (apparently) is that LspHandlerDescriptor is constructed for DelegatingRequest that is registered in LanguageServerHost.cs (in omnisharp-roslyn) via .OnRequest(). The constructor attempts to resolve RPC return type based on what generic type is set on IRequest that inherits DelegatingRequest. However DelegatingRequest implements both IRequest and IRequest and .LINQ query selects IRequest type as the return value -- while it should be IRequest. Fixed by ordering implemented IRequest<> interfaces by generic param type and taking the first that is not MediatR.Unit -- only then use the MediatR.Unit version. --- src/Protocol/Shared/LspHandlerDescriptor.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Protocol/Shared/LspHandlerDescriptor.cs b/src/Protocol/Shared/LspHandlerDescriptor.cs index 840ce951f..870dd9fc2 100644 --- a/src/Protocol/Shared/LspHandlerDescriptor.cs +++ b/src/Protocol/Shared/LspHandlerDescriptor.cs @@ -69,8 +69,11 @@ public LspHandlerDescriptor( CapabilityType = capabilityType; Response = @params?.GetInterfaces() - .FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IRequest<>)) - ?.GetGenericArguments()[0] ?? typeDescriptor?.ResponseType ?? typeof(Unit); + .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IRequest<>)) + .Select(x => x.GetGenericArguments()[0]) + .OrderBy(x => x == typeof(Unit)) + .FirstOrDefault() + ?? typeDescriptor?.ResponseType ?? typeof(Unit); // If multiple are implemented this behavior is unknown CanBeResolvedHandlerType = handler.GetType().GetTypeInfo()