Skip to content

Commit 64c732c

Browse files
committed
Move payload deserialisation out of handlers.
This is so the common JsonSerializerSettings can be used by the calling code. NOTE: This code is currently non-functional - handlers have been modified to take an Object instead of a JObject but the calling code does not yet perform deserialisation.
1 parent c601b27 commit 64c732c

12 files changed

+65
-13
lines changed

src/Client/Handlers/DelegateEmptyNotificationHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public DelegateEmptyNotificationHandler(string method, NotificationHandler handl
3232
/// </summary>
3333
public NotificationHandler Handler { get; }
3434

35+
/// <summary>
36+
/// The expected CLR type of the notification body (<c>null</c>, since the handler does not use the request body).
37+
/// </summary>
38+
public override Type BodyType => null;
39+
3540
/// <summary>
3641
/// Invoke the handler.
3742
/// </summary>

src/Client/Handlers/DelegateHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@ protected DelegateHandler(string method)
2626
/// The name of the method handled by the handler.
2727
/// </summary>
2828
public string Method { get; }
29+
30+
/// <summary>
31+
/// The expected CLR type of the request / notification body (if any; <c>null</c> if the handler does not use the request body).
32+
/// </summary>
33+
public abstract Type BodyType { get; }
2934
}
3035
}

src/Client/Handlers/DelegateNotificationHandler.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public DelegateNotificationHandler(string method, NotificationHandler<TNotificat
3838
/// </summary>
3939
public NotificationHandler<TNotification> Handler { get; }
4040

41+
/// <summary>
42+
/// The expected CLR type of the notification body.
43+
/// </summary>
44+
public override Type BodyType => typeof(TNotification);
45+
4146
/// <summary>
4247
/// Invoke the handler.
4348
/// </summary>
@@ -47,12 +52,12 @@ public DelegateNotificationHandler(string method, NotificationHandler<TNotificat
4752
/// <returns>
4853
/// A <see cref="Task"/> representing the operation.
4954
/// </returns>
50-
public async Task Invoke(JObject notification)
55+
public async Task Invoke(object notification)
5156
{
5257
await Task.Yield();
5358

5459
Handler(
55-
notification != null ? notification.ToObject<TNotification>(Serializer.Instance.JsonSerializer /* Fix me: this is ugly */) : default(TNotification)
60+
(TNotification)notification
5661
);
5762
}
5863
}

src/Client/Handlers/DelegateRequestHandler.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public DelegateRequestHandler(string method, RequestHandler<TRequest> handler)
3939
/// </summary>
4040
public RequestHandler<TRequest> Handler { get; }
4141

42+
/// <summary>
43+
/// The expected CLR type of the request body.
44+
/// </summary>
45+
public override Type BodyType => typeof(TRequest);
46+
4247
/// <summary>
4348
/// Invoke the handler.
4449
/// </summary>
@@ -51,10 +56,10 @@ public DelegateRequestHandler(string method, RequestHandler<TRequest> handler)
5156
/// <returns>
5257
/// A <see cref="Task{TResult}"/> representing the operation.
5358
/// </returns>
54-
public async Task<object> Invoke(JObject request, CancellationToken cancellationToken)
59+
public async Task<object> Invoke(object request, CancellationToken cancellationToken)
5560
{
5661
await Handler(
57-
request != null ? request.ToObject<TRequest>(Serializer.Instance.JsonSerializer /* Fix me: this is ugly */) : default(TRequest),
62+
(TRequest)request,
5863
cancellationToken
5964
);
6065

src/Client/Handlers/DelegateRequestResponseHandler.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public DelegateRequestResponseHandler(string method, RequestHandler<TRequest, TR
4242
/// </summary>
4343
public RequestHandler<TRequest, TResponse> Handler { get; }
4444

45+
/// <summary>
46+
/// The expected CLR type of the request body.
47+
/// </summary>
48+
public override Type BodyType => typeof(TRequest);
49+
4550
/// <summary>
4651
/// Invoke the handler.
4752
/// </summary>
@@ -54,10 +59,10 @@ public DelegateRequestResponseHandler(string method, RequestHandler<TRequest, TR
5459
/// <returns>
5560
/// A <see cref="Task{TResult}"/> representing the operation.
5661
/// </returns>
57-
public async Task<object> Invoke(JObject request, CancellationToken cancellationToken)
62+
public async Task<object> Invoke(object request, CancellationToken cancellationToken)
5863
{
5964
return await Handler(
60-
request != null ? request.ToObject<TRequest>(Serializer.Instance.JsonSerializer /* Fix me: this is ugly */) : default(TRequest),
65+
(TRequest)request,
6166
cancellationToken
6267
);
6368
}

src/Client/Handlers/DynamicRegistrationHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Threading;
23
using System.Threading.Tasks;
34
using Newtonsoft.Json.Linq;
@@ -31,6 +32,11 @@ public DynamicRegistrationHandler()
3132
/// </summary>
3233
public string Method => "client/registerCapability";
3334

35+
/// <summary>
36+
/// The expected CLR type of the request / notification body (if any; <c>null</c> if the handler does not use the request body).
37+
/// </summary>
38+
public Type BodyType => null;
39+
3440
/// <summary>
3541
/// Invoke the handler.
3642
/// </summary>
@@ -43,7 +49,7 @@ public DynamicRegistrationHandler()
4349
/// <returns>
4450
/// A <see cref="Task{TResult}"/> representing the operation.
4551
/// </returns>
46-
public Task<object> Invoke(JObject request, CancellationToken cancellationToken)
52+
public Task<object> Invoke(object request, CancellationToken cancellationToken)
4753
{
4854
// For now, we don't really support dynamic registration but OmniSharp's implementation sends a request even when dynamic registrations are not supported.
4955

src/Client/Handlers/IHandler.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace OmniSharp.Extensions.LanguageServer.Client.Handlers
1+
using System;
2+
3+
namespace OmniSharp.Extensions.LanguageServer.Client.Handlers
24
{
35
/// <summary>
46
/// Represents a client-side message handler.
@@ -9,5 +11,10 @@ public interface IHandler
911
/// The name of the method handled by the handler.
1012
/// </summary>
1113
string Method { get; }
14+
15+
/// <summary>
16+
/// The expected CLR type of the request / notification body (if any; <c>null</c> if the handler does not use the request body).
17+
/// </summary>
18+
Type BodyType { get; }
1219
}
1320
}

src/Client/Handlers/IInvokeNotificationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public interface IInvokeNotificationHandler
1818
/// <returns>
1919
/// A <see cref="Task"/> representing the operation.
2020
/// </returns>
21-
Task Invoke(JObject notification);
21+
Task Invoke(object notification);
2222
}
2323
}

src/Client/Handlers/IInvokeRequestHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ public interface IInvokeRequestHandler
2222
/// <returns>
2323
/// A <see cref="Task{TResult}"/> representing the operation.
2424
/// </returns>
25-
Task<object> Invoke(JObject request, CancellationToken cancellationToken);
25+
Task<object> Invoke(object request, CancellationToken cancellationToken);
2626
}
2727
}

src/Client/Handlers/JsonRpcEmptyNotificationHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public JsonRpcEmptyNotificationHandler(string method, INotificationHandler handl
3333
/// </summary>
3434
public INotificationHandler Handler { get; }
3535

36+
/// <summary>
37+
/// The expected CLR type of the notification body (<c>null</c>, since the handler does not use the request body).
38+
/// </summary>
39+
public override Type BodyType => null;
40+
3641
/// <summary>
3742
/// Invoke the handler.
3843
/// </summary>

src/Client/Handlers/JsonRpcHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,10 @@ protected JsonRpcHandler(string method)
2727
/// The name of the method handled by the handler.
2828
/// </summary>
2929
public string Method { get; }
30+
31+
/// <summary>
32+
/// The expected CLR type of the request / notification body (if any; <c>null</c> if the handler does not use the request body).
33+
/// </summary>
34+
public abstract Type BodyType { get; }
3035
}
3136
}

src/Client/Handlers/JsonRpcNotificationHandler.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Threading.Tasks;
3-
using Newtonsoft.Json.Linq;
43
using OmniSharp.Extensions.JsonRpc;
54
using OmniSharp.Extensions.LanguageServer.Protocol;
65
using OmniSharp.Extensions.LanguageServer.Protocol.Serialization;
@@ -39,6 +38,11 @@ public JsonRpcNotificationHandler(string method, INotificationHandler<TNotificat
3938
/// </summary>
4039
public INotificationHandler<TNotification> Handler { get; }
4140

41+
/// <summary>
42+
/// The expected CLR type of the notification body.
43+
/// </summary>
44+
public override Type BodyType => typeof(TNotification);
45+
4246
/// <summary>
4347
/// Invoke the handler.
4448
/// </summary>
@@ -48,8 +52,8 @@ public JsonRpcNotificationHandler(string method, INotificationHandler<TNotificat
4852
/// <returns>
4953
/// A <see cref="Task"/> representing the operation.
5054
/// </returns>
51-
public Task Invoke(JObject notification) => Handler.Handle(
52-
notification != null ? notification.ToObject<TNotification>(Serializer.Instance.JsonSerializer /* Fix me: this is ugly */) : default(TNotification)
55+
public Task Invoke(object notification) => Handler.Handle(
56+
(TNotification)notification
5357
);
5458
}
5559
}

0 commit comments

Comments
 (0)