Skip to content

Commit 04d5a5f

Browse files
fixes tests
1 parent 4cae6dd commit 04d5a5f

32 files changed

+96
-81
lines changed

src/Client/Handlers/DelegateNotificationHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System;
1+
using System;
22
using System.Threading.Tasks;
33
using Newtonsoft.Json.Linq;
4+
using OmniSharp.Extensions.LanguageServer.Protocol;
45

56
namespace OmniSharp.Extensions.LanguageServer.Client.Handlers
67
{
@@ -50,7 +51,7 @@ public async Task Invoke(JObject notification)
5051
await Task.Yield();
5152

5253
Handler(
53-
notification != null ? notification.ToObject<TNotification>() : default(TNotification)
54+
notification != null ? notification.ToObject<TNotification>(Serializer.Instance.JsonSerializer /* Fix me: this is ugly */) : default(TNotification)
5455
);
5556
}
5657
}

src/Client/Handlers/DelegateRequestHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
1+
using System;
22
using System.Threading;
33
using System.Threading.Tasks;
44
using Newtonsoft.Json.Linq;
5+
using OmniSharp.Extensions.LanguageServer.Protocol;
56

67
namespace OmniSharp.Extensions.LanguageServer.Client.Handlers
78
{
@@ -52,7 +53,7 @@ public DelegateRequestHandler(string method, RequestHandler<TRequest> handler)
5253
public async Task<object> Invoke(JObject request, CancellationToken cancellationToken)
5354
{
5455
await Handler(
55-
request != null ? request.ToObject<TRequest>() : default(TRequest),
56+
request != null ? request.ToObject<TRequest>(Serializer.Instance.JsonSerializer /* Fix me: this is ugly */) : default(TRequest),
5657
cancellationToken
5758
);
5859

src/Client/Handlers/DelegateRequestResponseHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
1+
using System;
22
using System.Threading;
33
using System.Threading.Tasks;
44
using Newtonsoft.Json.Linq;
5+
using OmniSharp.Extensions.LanguageServer.Protocol;
56

67
namespace OmniSharp.Extensions.LanguageServer.Client.Handlers
78
{
@@ -55,7 +56,7 @@ public DelegateRequestResponseHandler(string method, RequestHandler<TRequest, TR
5556
public async Task<object> Invoke(JObject request, CancellationToken cancellationToken)
5657
{
5758
return await Handler(
58-
request != null ? request.ToObject<TRequest>() : default(TRequest),
59+
request != null ? request.ToObject<TRequest>(Serializer.Instance.JsonSerializer /* Fix me: this is ugly */) : default(TRequest),
5960
cancellationToken
6061
);
6162
}

src/Client/Handlers/JsonRpcNotificationHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
1+
using System;
22
using System.Threading.Tasks;
33
using Newtonsoft.Json.Linq;
44
using OmniSharp.Extensions.JsonRpc;
5+
using OmniSharp.Extensions.LanguageServer.Protocol;
56

67
namespace OmniSharp.Extensions.LanguageServer.Client.Handlers
78
{
@@ -47,7 +48,7 @@ public JsonRpcNotificationHandler(string method, INotificationHandler<TNotificat
4748
/// A <see cref="Task"/> representing the operation.
4849
/// </returns>
4950
public Task Invoke(JObject notification) => Handler.Handle(
50-
notification != null ? notification.ToObject<TNotification>() : default(TNotification)
51+
notification != null ? notification.ToObject<TNotification>(Serializer.Instance.JsonSerializer /* Fix me: this is ugly */) : default(TNotification)
5152
);
5253
}
5354
}

src/Client/Protocol/LspConnection.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public sealed class LspConnection
125125
/// </summary>
126126
Task _dispatchLoop;
127127

128-
private JsonSerializerSettings _jsonSerializerSettings;
128+
private readonly Serializer _serializer;
129129

130130
/// <summary>
131131
/// Create a new <see cref="LspConnection"/>.
@@ -160,7 +160,7 @@ public LspConnection(ILoggerFactory loggerFactory, Stream input, Stream output)
160160
_input = input;
161161
_output = output;
162162
// What does client version do? Do we have to negotaite this?
163-
_jsonSerializerSettings = new Serializer(ClientVersion.Lsp3).Settings;
163+
_serializer = new Serializer(ClientVersion.Lsp3);
164164
}
165165

166166
/// <summary>
@@ -336,7 +336,7 @@ public void SendNotification(string method, object notification)
336336
{
337337
// No Id means it's a notification.
338338
Method = method,
339-
Params = JObject.FromObject(notification)
339+
Params = JObject.FromObject(notification, _serializer.JsonSerializer)
340340
});
341341
}
342342

@@ -394,7 +394,7 @@ public void SendNotification(string method, object notification)
394394
{
395395
Id = requestId,
396396
Method = method,
397-
Params = request != null ? JObject.FromObject(request) : null
397+
Params = request != null ? JObject.FromObject(request, _serializer.JsonSerializer) : null
398398
});
399399

400400
await responseCompletion.Task;
@@ -457,13 +457,13 @@ public void SendNotification(string method, object notification)
457457
{
458458
Id = requestId,
459459
Method = method,
460-
Params = request != null ? JObject.FromObject(request) : null
460+
Params = request != null ? JObject.FromObject(request, _serializer.JsonSerializer) : null
461461
});
462462

463463
ServerMessage response = await responseCompletion.Task;
464464

465465
if (response.Result != null)
466-
return response.Result.ToObject<TResponse>();
466+
return response.Result.ToObject<TResponse>(_serializer.JsonSerializer);
467467
else
468468
return default(TResponse);
469469
}
@@ -659,7 +659,7 @@ async Task SendMessage<TMessage>(TMessage message)
659659
if (message == null)
660660
throw new ArgumentNullException(nameof(message));
661661

662-
string payload = JsonConvert.SerializeObject(message, _jsonSerializerSettings);
662+
string payload = JsonConvert.SerializeObject(message, _serializer.Settings);
663663
byte[] payloadBuffer = PayloadEncoding.GetBytes(payload);
664664

665665
byte[] headerBuffer = HeaderEncoding.GetBytes(
@@ -759,7 +759,7 @@ async Task<ServerMessage> ReceiveMessage()
759759
Log.LogDebug("Received entire payload ({ReceivedByteCount} bytes).", received);
760760

761761
string responseBody = PayloadEncoding.GetString(requestBuffer);
762-
ServerMessage message = JsonConvert.DeserializeObject<ServerMessage>(responseBody, _jsonSerializerSettings);
762+
ServerMessage message = JsonConvert.DeserializeObject<ServerMessage>(responseBody, _serializer.Settings);
763763

764764
Log.LogDebug("Read response body {ResponseBody}.", responseBody);
765765

@@ -892,7 +892,7 @@ private void DispatchRequest(ServerMessage requestMessage)
892892
{
893893
Id = requestMessage.Id,
894894
Method = requestMessage.Method,
895-
Result = handlerTask.Result != null ? JObject.FromObject(handlerTask.Result) : null
895+
Result = handlerTask.Result != null ? JObject.FromObject(handlerTask.Result, _serializer.JsonSerializer) : null
896896
});
897897
}
898898

src/JsonRpc/RequestRouter.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55
using System.Threading.Tasks;
66
using OmniSharp.Extensions.JsonRpc.Server;
77
using OmniSharp.Extensions.JsonRpc.Server.Messages;
8+
using OmniSharp.Extensions.LanguageServer.Protocol;
89

910
namespace OmniSharp.Extensions.JsonRpc
1011
{
1112
class RequestRouter : IRequestRouter
1213
{
1314
private readonly HandlerCollection _collection;
15+
private readonly ISerializer _serializer;
1416

15-
public RequestRouter(HandlerCollection collection)
17+
public RequestRouter(HandlerCollection collection, ISerializer serializer)
1618
{
1719
_collection = collection;
20+
_serializer = serializer;
1821
}
1922

2023
public IDisposable Add(IJsonRpcHandler handler)
@@ -36,7 +39,7 @@ public async Task RouteNotification(IHandlerDescriptor handler, Notification not
3639
}
3740
else
3841
{
39-
var @params = notification.Params.ToObject(handler.Params);
42+
var @params = notification.Params.ToObject(handler.Params, _serializer.JsonSerializer);
4043
result = ReflectionRequestHandlers.HandleNotification(handler, @params);
4144
}
4245
await result.ConfigureAwait(false);
@@ -64,7 +67,7 @@ protected virtual async Task<ErrorResponse> RouteRequest(IHandlerDescriptor hand
6467
object @params;
6568
try
6669
{
67-
@params = request.Params.ToObject(handler.Params);
70+
@params = request.Params.ToObject(handler.Params, _serializer.JsonSerializer);
6871
}
6972
catch
7073
{

src/JsonRpc/ResponseRouter.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
using System.Collections.Concurrent;
1+
using System.Collections.Concurrent;
22
using System.Threading.Tasks;
33
using Newtonsoft.Json.Linq;
4+
using OmniSharp.Extensions.LanguageServer.Protocol;
45

56
namespace OmniSharp.Extensions.JsonRpc
67
{
78
public class ResponseRouter : IResponseRouter
89
{
910
private readonly IOutputHandler _outputHandler;
11+
private readonly ISerializer _serializer;
1012
private readonly object _lock = new object();
1113
private long _id = 0;
1214
private readonly ConcurrentDictionary<long, TaskCompletionSource<JToken>> _requests = new ConcurrentDictionary<long, TaskCompletionSource<JToken>>();
1315

14-
public ResponseRouter(IOutputHandler outputHandler)
16+
public ResponseRouter(IOutputHandler outputHandler, ISerializer serializer)
1517
{
1618
_outputHandler = outputHandler;
19+
_serializer = serializer;
1720
}
1821

1922
public void SendNotification<T>(string method, T @params)
@@ -44,7 +47,7 @@ public async Task<TResponse> SendRequest<T, TResponse>(string method, T @params)
4447
try
4548
{
4649
var result = await tcs.Task;
47-
return result.ToObject<TResponse>();
50+
return result.ToObject<TResponse>(_serializer.JsonSerializer);
4851
}
4952
finally
5053
{
@@ -85,4 +88,4 @@ public TaskCompletionSource<JToken> GetRequest(long id)
8588
return source;
8689
}
8790
}
88-
}
91+
}

src/JsonRpc/RpcErrorConverter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
1717
{
1818
var obj = JObject.Load(reader);
1919

20-
var messageDataType = objectType == typeof(RpcError)
21-
? typeof(object)
22-
: objectType.GetTypeInfo().GetGenericArguments()[0];
20+
var messageDataType = objectType.GetTypeInfo().IsGenericType
21+
? objectType.GetTypeInfo().GetGenericArguments()[0]
22+
: typeof(object);
2323

2424
object requestId = null;
2525
if (obj.TryGetValue("id", out var id))
@@ -33,7 +33,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
3333
if (obj.TryGetValue("error", out var dataToken))
3434
{
3535
var errorMessageType = typeof(ErrorMessage<>).MakeGenericType(messageDataType);
36-
data = dataToken.ToObject(errorMessageType);
36+
data = dataToken.ToObject(errorMessageType, serializer);
3737
}
3838

3939
return Activator.CreateInstance(objectType, requestId, data, obj["protocolVersion"].ToString());
@@ -48,4 +48,4 @@ public override bool CanConvert(Type objectType)
4848
public override bool CanWrite { get; } = false;
4949
public override bool CanRead { get; } = true;
5050
}
51-
}
51+
}

src/Protocol/Serialization/Converters/CompletionListConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using Newtonsoft.Json;
44
using Newtonsoft.Json.Linq;
@@ -35,17 +35,17 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
3535
{
3636
if (reader.TokenType == JsonToken.StartArray)
3737
{
38-
var array = JArray.Load(reader).ToObject<IEnumerable<CompletionItem>>();
38+
var array = JArray.Load(reader).ToObject<IEnumerable<CompletionItem>>(serializer);
3939
return new CompletionList(array);
4040
}
4141

4242
var result = JObject.Load(reader);
43-
var items = result["items"].ToObject<IEnumerable<CompletionItem>>();
43+
var items = result["items"].ToObject<IEnumerable<CompletionItem>>(serializer);
4444
return new CompletionList(items, result["isIncomplete"].Value<bool>());
4545
}
4646

4747
public override bool CanRead => true;
4848

4949
public override bool CanConvert(Type objectType) => objectType == typeof(CompletionList);
5050
}
51-
}
51+
}

src/Protocol/Serialization/Converters/LocationOrLocationsConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using Newtonsoft.Json;
@@ -25,11 +25,11 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
2525
{
2626
if (reader.TokenType == JsonToken.StartArray)
2727
{
28-
return new LocationOrLocations(JArray.Load(reader).ToObject<IEnumerable<Location>>());
28+
return new LocationOrLocations(JArray.Load(reader).ToObject<IEnumerable<Location>>(serializer));
2929
}
3030
else if (reader.TokenType == JsonToken.StartObject)
3131
{
32-
return new LocationOrLocations(JObject.Load(reader).ToObject<Location>());
32+
return new LocationOrLocations(JObject.Load(reader).ToObject<Location>(serializer));
3333
}
3434

3535
return new LocationOrLocations();
@@ -39,4 +39,4 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
3939

4040
public override bool CanConvert(Type objectType) => objectType == typeof(LocationOrLocations);
4141
}
42-
}
42+
}

src/Protocol/Serialization/Converters/MarkedStringCollectionConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using Newtonsoft.Json;
@@ -25,11 +25,11 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
2525
{
2626
if (reader.TokenType == JsonToken.StartArray)
2727
{
28-
return new MarkedStringContainer(JArray.Load(reader).ToObject<IEnumerable<MarkedString>>());
28+
return new MarkedStringContainer(JArray.Load(reader).ToObject<IEnumerable<MarkedString>>(serializer));
2929
}
3030
else if (reader.TokenType == JsonToken.StartObject)
3131
{
32-
return new MarkedStringContainer(JObject.Load(reader).ToObject<MarkedString>());
32+
return new MarkedStringContainer(JObject.Load(reader).ToObject<MarkedString>(serializer));
3333
}
3434
else if (reader.TokenType == JsonToken.String)
3535
{

src/Protocol/Serialization/Converters/StringOrMarkupContentConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace OmniSharp.Extensions.LanguageServer.Protocol.Converters
77
{
8-
public class StringOrMarkupContentConverter: JsonConverter
8+
public class StringOrMarkupContentConverter : JsonConverter
99
{
1010
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
1111
{
@@ -27,7 +27,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
2727
var result = JObject.Load(reader);
2828
return new StringOrMarkupContent(
2929
new MarkupContent() {
30-
Kind = result["kind"]?.Value<MarkupKind>() ?? MarkupKind.Plaintext,
30+
Kind = Enum.TryParse<MarkupKind>(result["kind"]?.Value<string>(), true, out var kind) ? kind : MarkupKind.Plaintext,
3131
Value = result["value"]?.Value<string>()
3232
}
3333
);

src/Protocol/Serialization/Converters/TextDocumentSyncConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using Newtonsoft.Json;
33
using Newtonsoft.Json.Linq;
44
using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities;
@@ -28,11 +28,11 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
2828
return new TextDocumentSync((TextDocumentSyncKind)Convert.ToInt32(reader.Value));
2929
}
3030

31-
return new TextDocumentSync(JObject.Load(reader).ToObject<TextDocumentSyncOptions>());
31+
return new TextDocumentSync(JObject.Load(reader).ToObject<TextDocumentSyncOptions>(serializer));
3232
}
3333

3434
public override bool CanRead => true;
3535

3636
public override bool CanConvert(Type objectType) => objectType == typeof(TextDocumentSync);
3737
}
38-
}
38+
}

src/Protocol/Serialization/Serializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace OmniSharp.Extensions.LanguageServer.Protocol
1111
{
1212
public class Serializer : ISerializer
1313
{
14+
public static Serializer Instance { get; } = new Serializer();
1415
public Serializer() : this(ClientVersion.Lsp3) { }
1516
public Serializer(ClientVersion clientVersion)
1617
{

src/Server/LanguageServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ internal LanguageServer(
6464
new ExecuteCommandMatcher(_loggerFactory.CreateLogger<ExecuteCommandMatcher>())
6565
};
6666

67-
_requestRouter = new LspRequestRouter(_collection, loggerFactory, _handlerMactherCollection);
68-
_responseRouter = new ResponseRouter(outputHandler);
67+
_requestRouter = new LspRequestRouter(_collection, loggerFactory, _handlerMactherCollection, _serializer);
68+
_responseRouter = new ResponseRouter(outputHandler, _serializer);
6969
_connection = new Connection(input, outputHandler, reciever, requestProcessIdentifier, _requestRouter, _responseRouter, loggerFactory, serializer);
7070

7171
_exitHandler = new ExitHandler(_shutdownHandler);

0 commit comments

Comments
 (0)