camelCasing objects serialized by JsonHubProtocol

Fixes: #859
This commit is contained in:
Pawel Kadluczka 2017-09-21 15:21:17 -07:00 committed by Pawel Kadluczka
parent 6ed3f6d1aa
commit 4db78685dc
4 changed files with 43 additions and 10 deletions

View File

@ -35,15 +35,15 @@ connection.on('SetUsersOnline', usersOnline => {
connection.on('UsersJoined', users => { connection.on('UsersJoined', users => {
users.forEach(user => { users.forEach(user => {
appendLine('User ' + user.Name + ' joined the chat'); appendLine('User ' + user.name + ' joined the chat');
addUserOnline(user); addUserOnline(user);
}); });
}); });
connection.on('UsersLeft', users => { connection.on('UsersLeft', users => {
users.forEach(user => { users.forEach(user => {
appendLine('User ' + user.Name + ' left the chat'); appendLine('User ' + user.name + ' left the chat');
document.getElementById(user.ConnectionId).outerHTML = ''; document.getElementById(user.connectionId).outerHTML = '';
}); });
}); });
@ -78,12 +78,12 @@ function appendLine(line, color) {
}; };
function addUserOnline(user) { function addUserOnline(user) {
if (document.getElementById(user.ConnectionId)) { if (document.getElementById(user.connectionId)) {
return; return;
} }
var userLi = document.createElement('li'); var userLi = document.createElement('li');
userLi.innerText = `${user.Name} (${user.ConnectionId})`; userLi.innerText = `${user.name} (${user.connectionId})`;
userLi.id = user.ConnectionId; userLi.id = user.connectionId;
document.getElementById('users').appendChild(userLi); document.getElementById('users').appendChild(userLi);
} }

View File

@ -7,6 +7,7 @@ using System.IO;
using Microsoft.AspNetCore.SignalR.Internal.Formatters; using Microsoft.AspNetCore.SignalR.Internal.Formatters;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
namespace Microsoft.AspNetCore.SignalR.Internal.Protocol namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
{ {
@ -34,7 +35,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
/// NOT be changed using this serializer. /// NOT be changed using this serializer.
/// </summary> /// </summary>
public JsonHubProtocol() public JsonHubProtocol()
: this(new JsonSerializer()) : this(JsonSerializer.Create(CreateDefaultSerializerSettings()))
{ } { }
/// <summary> /// <summary>
@ -258,5 +259,10 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
return new CompletionMessage(invocationId, error, result: payload, hasResult: true); return new CompletionMessage(invocationId, error, result: payload, hasResult: true);
} }
} }
public static JsonSerializerSettings CreateDefaultSerializerSettings()
{
return new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
}
} }
} }

View File

@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.SignalR
{ {
public class HubOptions public class HubOptions
{ {
public JsonSerializerSettings JsonSerializerSettings { get; set; } = new JsonSerializerSettings(); public JsonSerializerSettings JsonSerializerSettings { get; set; } = JsonHubProtocol.CreateDefaultSerializerSettings();
public SerializationContext MessagePackSerializationContext { get; set; } = MessagePackHubProtocol.CreateDefaultSerializationContext(); public SerializationContext MessagePackSerializationContext { get; set; } = MessagePackHubProtocol.CreateDefaultSerializationContext();
} }
} }

View File

@ -1008,7 +1008,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
{ {
o.JsonSerializerSettings = new JsonSerializerSettings o.JsonSerializerSettings = new JsonSerializerSettings
{ {
ContractResolver = new CamelCasePropertyNamesContractResolver() ContractResolver = new DefaultContractResolver()
}; };
}); });
}); });
@ -1026,7 +1026,34 @@ namespace Microsoft.AspNetCore.SignalR.Tests
var message = (InvocationMessage)await client.ReadAsync().OrTimeout(); var message = (InvocationMessage)await client.ReadAsync().OrTimeout();
var customItem = message.Arguments[0].ToString(); var customItem = message.Arguments[0].ToString();
// Originally "Message" and "paramName" // by default properties serialized by JsonHubProtocol are using camelCasing
Assert.Contains("Message", customItem);
Assert.Contains("paramName", customItem);
client.Dispose();
await endPointLifetime.OrTimeout();
}
}
[Fact]
public async Task JsonHubProtocolUsesCamelCasingByDefault()
{
var serviceProvider = CreateServiceProvider();
var endPoint = serviceProvider.GetService<HubEndPoint<MethodHub>>();
using (var client = new TestClient())
{
var endPointLifetime = endPoint.OnConnectedAsync(client.Connection);
await client.Connected.OrTimeout();
await client.SendInvocationAsync(nameof(MethodHub.BroadcastItem)).OrTimeout();
var message = (InvocationMessage)await client.ReadAsync().OrTimeout();
var customItem = message.Arguments[0].ToString();
// originally Message, paramName
Assert.Contains("message", customItem); Assert.Contains("message", customItem);
Assert.Contains("paramName", customItem); Assert.Contains("paramName", customItem);