Skip to content

Bug: Subscription Message Serialziation #169

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

Merged
merged 2 commits into from
Apr 25, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ env:
jobs:
build:
name: Sanity Build
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nuget-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ env:
jobs:
deployment:
name: Pack & Deploy
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04

steps:
- uses: actions/checkout@v3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"StarWars: .NET 8": {
"StarWars: .NET 9": {
"commandName": "Project",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public GqltwsClientProxy(
_serializerOptions.Converters.Add(new GqltwsServerDataMessageConverter(schema, responseWriter));
_serializerOptions.Converters.Add(new GqltwsServerCompleteMessageConverter());
_serializerOptions.Converters.Add(new GqltwsServerErrorMessageConverter(schema));
_serializerOptions.Converters.Add(new GqltwsMessageConverter());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public GraphqlWsLegacyClientProxy(
_serializeOptions.Converters.Add(new GraphqlWsLegacyServerDataMessageConverter(schema, responseWriter));
_serializeOptions.Converters.Add(new GraphqlWsLegacyServerCompleteMessageConverter());
_serializeOptions.Converters.Add(new GraphqlWsLegacyServerErrorMessageConverter(schema));
_serializeOptions.Converters.Add(new GraphqlWsLegacyMessageConverter());
}

/// <inheritdoc />
Expand Down Expand Up @@ -305,7 +306,7 @@ private async Task AcknowledgeNewConnectionAsync()
/// <inheritdoc />
protected override async Task ExecuteKeepAliveAsync(CancellationToken cancelToken = default)
{
await this.SendMessageAsync(new GraphqlWsLegacyKeepAliveOperationMessage());
await this.SendMessageAsync(new GraphqlWsLegacyKeepAliveOperationMessage(), cancelToken);
}

/// <inheritdoc />
Expand Down
8 changes: 5 additions & 3 deletions src/graphql-aspnet/Common/TimerAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public void Start()
}
finally
{
_semaphore.Release();
if (!_disposed)
_semaphore.Release();
}
}

Expand Down Expand Up @@ -121,7 +122,8 @@ public async Task StopAsync()
finally
{
this.IsRunning = false;
_semaphore.Release();
if (!_disposed)
_semaphore.Release();
}
}

Expand Down Expand Up @@ -195,4 +197,4 @@ public void Dispose()
/// <value><c>true</c> if the timer is running; otherwise, <c>false</c>.</value>
public bool IsRunning { get; private set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public static class GqltwsClientAsserts
/// <param name="connection">The connection.</param>
/// <param name="type">The type of message to check for.</param>
/// <param name="dequeue">if true, the message is removed from the queue.</param>
internal static void AssertGqltwsResponse(
/// <returns>The raw string data (usually json formatted) of the message data recevied by the connection proxy </returns>
internal static string AssertGqltwsResponse(
this MockClientConnection connection,
GqltwsMessageType type,
bool dequeue = true)
{
connection.AssertGqltwsResponse(type, null, false, null, false, dequeue);
return connection.AssertGqltwsResponse(type, null, false, null, false, dequeue);
}

/// <summary>
Expand All @@ -40,13 +41,14 @@ internal static void AssertGqltwsResponse(
/// <param name="type">The type of message to check for.</param>
/// <param name="id">The id returned by the server, if supplied.</param>
/// <param name="dequeue">if true, the message is removed from the queue.</param>
internal static void AssertGqltwsResponse(
/// <returns>The raw string data (usually json formatted) of the message data recevied by the connection proxy </returns>
internal static string AssertGqltwsResponse(
this MockClientConnection connection,
GqltwsMessageType type,
string id,
bool dequeue = true)
{
connection.AssertGqltwsResponse(type, id, true, null, false, dequeue);
return connection.AssertGqltwsResponse(type, id, true, null, false, dequeue);
}

/// <summary>
Expand All @@ -58,17 +60,18 @@ internal static void AssertGqltwsResponse(
/// <param name="id">The expected identifier of the subscription that rendered the data.</param>
/// <param name="expectedPayloadJson">The expected payload of the message, converted to a json string.</param>
/// <param name="dequeue">if set to <c>true</c> if the message should be removed from the queue.</param>
internal static void AssertGqltwsResponse(
/// <returns>The raw string data (usually json formatted) of the message data recevied by the connection proxy </returns>
internal static string AssertGqltwsResponse(
this MockClientConnection connection,
GqltwsMessageType type,
string id,
string expectedPayloadJson,
bool dequeue = true)
{
connection.AssertGqltwsResponse(type, id, true, expectedPayloadJson, true, dequeue);
return connection.AssertGqltwsResponse(type, id, true, expectedPayloadJson, true, dequeue);
}

private static void AssertGqltwsResponse(
private static string AssertGqltwsResponse(
this MockClientConnection connection,
GqltwsMessageType type,
string id,
Expand All @@ -81,14 +84,14 @@ private static void AssertGqltwsResponse(
Assert.Fail("No messages queued.");

var message = dequeue ? connection.DequeueNextReceivedMessage() : connection.PeekNextReceivedMessage();
var str = Encoding.UTF8.GetString(message.Data);
var rawMessageData = Encoding.UTF8.GetString(message.Data);

var options = new JsonSerializerOptions();
options.PropertyNameCaseInsensitive = true;
options.AllowTrailingCommas = true;
options.Converters.Add(new GqltwsResponseMessageConverter());

var convertedMessage = JsonSerializer.Deserialize<GqltwsResponseMessage>(str, options);
var convertedMessage = JsonSerializer.Deserialize<GqltwsResponseMessage>(rawMessageData, options);

Assert.IsNotNull(convertedMessage, "Could not deserialize response message");
Assert.AreEqual(type, convertedMessage.Type, $"Expected message type of {type.ToString()} but got {convertedMessage.Type.ToString()}");
Expand All @@ -103,6 +106,8 @@ private static void AssertGqltwsResponse(

if (compareId)
Assert.AreEqual(id, convertedMessage.Id);

return rawMessageData;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -746,5 +746,23 @@ public async Task Deserialize_InvalidMessage_ProcessesUnknownMessage()
connection.AssertServerClosedConnection((ConnectionCloseStatus)GqltwsConstants.CustomCloseEventIds.InvalidMessageType);
graphqlWsClient.Dispose();
}

[Test]
public async Task SendConnectionInitMessage_RespondsWithCorrectAckMessage()
{
using var restorePoint = new GraphQLGlobalSubscriptionRestorePoint();
(var socketClient, var client, var router) = this.CreateConnection();

socketClient.QueueClientMessage(new GqltwsClientConnectionInitMessage());
socketClient.QueueConnectionClosedByClient();

await client.StartConnectionAsync();

var rawMessageData = socketClient.AssertGqltwsResponse(GqltwsMessageType.CONNECTION_ACK);
socketClient.AssertClientClosedConnection();

var expectedData = "{ \"type\": \"connection_ack\" }";
CommonAssertions.AreEqualJsonStrings(expectedData, rawMessageData);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public static class GraphqlWsLegacyClientAsserts
/// <param name="connection">The connection.</param>
/// <param name="type">The type of message to check for.</param>
/// <param name="dequeue">if true, the message is removed from the queue.</param>
internal static void AssertGraphqlWsLegacyResponse(
/// <returns>The raw string data (usually json formatted) of the message data recevied by the connection proxy </returns>
internal static string AssertGraphqlWsLegacyResponse(
this MockClientConnection connection,
GraphqlWsLegacyMessageType type,
bool dequeue = true)
{
connection.AssertGraphqlWsLegacyResponse(type, null, false, null, false, dequeue);
return connection.AssertGraphqlWsLegacyResponse(type, null, false, null, false, dequeue);
}

/// <summary>
Expand All @@ -40,13 +41,14 @@ internal static void AssertGraphqlWsLegacyResponse(
/// <param name="type">The type of message to check for.</param>
/// <param name="id">The id returned by the server, if supplied.</param>
/// <param name="dequeue">if true, the message is removed from the queue.</param>
internal static void AssertGraphqlWsLegacyResponse(
/// <returns>The raw string data (usually json formatted) of the message data recevied by the connection proxy </returns>
internal static string AssertGraphqlWsLegacyResponse(
this MockClientConnection connection,
GraphqlWsLegacyMessageType type,
string id,
bool dequeue = true)
{
connection.AssertGraphqlWsLegacyResponse(type, id, true, null, false, dequeue);
return connection.AssertGraphqlWsLegacyResponse(type, id, true, null, false, dequeue);
}

/// <summary>
Expand All @@ -58,17 +60,18 @@ internal static void AssertGraphqlWsLegacyResponse(
/// <param name="id">The expected identifier of the subscription that rendered the data.</param>
/// <param name="expectedPayloadJson">The expected payload of the message, converted to a json string.</param>
/// <param name="dequeue">if set to <c>true</c> if the message should be removed from the queue.</param>
internal static void AssertGraphqlWsLegacyResponse(
/// <returns>The raw string data (usually json formatted) of the message data recevied by the connection proxy </returns>
internal static string AssertGraphqlWsLegacyResponse(
this MockClientConnection connection,
GraphqlWsLegacyMessageType type,
string id,
string expectedPayloadJson,
bool dequeue = true)
{
connection.AssertGraphqlWsLegacyResponse(type, id, true, expectedPayloadJson, true, dequeue);
return connection.AssertGraphqlWsLegacyResponse(type, id, true, expectedPayloadJson, true, dequeue);
}

private static void AssertGraphqlWsLegacyResponse(
private static string AssertGraphqlWsLegacyResponse(
this MockClientConnection connection,
GraphqlWsLegacyMessageType type,
string id,
Expand All @@ -81,14 +84,14 @@ private static void AssertGraphqlWsLegacyResponse(
Assert.Fail("No messages queued.");

var message = dequeue ? connection.DequeueNextReceivedMessage() : connection.PeekNextReceivedMessage();
var str = Encoding.UTF8.GetString(message.Data);
var rawData = Encoding.UTF8.GetString(message.Data);

var options = new JsonSerializerOptions();
options.PropertyNameCaseInsensitive = true;
options.AllowTrailingCommas = true;
options.Converters.Add(new GraphqlWsLegacyResponseMessageConverter());

var convertedMessage = JsonSerializer.Deserialize<GraphqlWsLegacyResponseMessage>(str, options);
var convertedMessage = JsonSerializer.Deserialize<GraphqlWsLegacyResponseMessage>(rawData, options);

Assert.IsNotNull(convertedMessage, "Could not deserialized response message");
Assert.AreEqual(type, convertedMessage.Type, $"Expected message type of {type.ToString()} but got {convertedMessage.Type.ToString()}");
Expand All @@ -103,6 +106,8 @@ private static void AssertGraphqlWsLegacyResponse(

if (compareId)
Assert.AreEqual(id, convertedMessage.Id);

return rawData;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -501,5 +501,23 @@ public async Task Deserialize_InvalidMessage_ProcessesUnknownMessage()
socketClient.AssertGraphqlWsLegacyResponse(GraphqlWsLegacyMessageType.ERROR);
socketClient.AssertClientClosedConnection();
}

[Test]
public async Task SendConnectionInitMessage_RespondsWithCorrectAckMessage()
{
using var restorePoint = new GraphQLGlobalSubscriptionRestorePoint();
(var socketClient, var client, var router) = this.CreateConnection();

socketClient.QueueClientMessage(new GraphqlWsLegacyClientConnectionInitMessage());
socketClient.QueueConnectionClosedByClient();

await client.StartConnectionAsync();

var rawMessageData = socketClient.AssertGraphqlWsLegacyResponse(GraphqlWsLegacyMessageType.CONNECTION_ACK);
socketClient.AssertClientClosedConnection();

var expectedData = "{ \"type\": \"connection_ack\" }";
CommonAssertions.AreEqualJsonStrings(expectedData, rawMessageData);
}
}
}