Skip to content

Fixed large json deserialization regression #8415

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.IO.Pipelines;
using System.Runtime.CompilerServices;
using System.Text.Json;
using HotChocolate.Buffers;
using HotChocolate.Transport.Http;
using StrawberryShake.Internal;
using static StrawberryShake.Properties.Resources;
Expand Down Expand Up @@ -88,8 +88,8 @@ public async IAsyncEnumerator<Response<JsonDocument>> GetAsyncEnumerator(
return null;
}

using var buffer = new PooledArrayWriter();
using var writer = new Utf8JsonWriter(buffer);
var pipe = new Pipe();
using var writer = new Utf8JsonWriter(pipe.Writer);

writer.WriteStartObject();
WriteProperty(writer, "data", result.Data);
Expand All @@ -111,8 +111,9 @@ public async IAsyncEnumerator<Response<JsonDocument>> GetAsyncEnumerator(
writer.WriteEndObject();

writer.Flush();
pipe.Writer.Complete();

return JsonDocument.Parse(buffer.GetWrittenMemory());
return JsonDocument.Parse(pipe.Reader.AsStream());
}

private static void WriteProperty(Utf8JsonWriter writer, string propertyName, JsonElement value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,51 @@ public async Task Execute_AnyScalarDefaultSerialization_Test()
result.Data?.Json.ToString().MatchSnapshot();
}

[Fact]
public async Task Execute_AnyScalarDefaultSerialization_TestLarge()
{
// arrange
using var cts = new CancellationTokenSource(20_000);
using var host = TestServerHelper.CreateServer(
builder =>
{
builder.AddTypeExtension<LargeQueryResolvers>();
},
out var port);
var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient(
AnyScalarDefaultSerializationClient.ClientName,
c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"));
serviceCollection.AddWebSocketClient(
AnyScalarDefaultSerializationClient.ClientName,
c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));
serviceCollection.AddAnyScalarDefaultSerializationClient();
IServiceProvider services = serviceCollection.BuildServiceProvider();
var client =
services.GetRequiredService<AnyScalarDefaultSerializationClient>();

// act
IOperationResult<IGetJsonResult> result = await client.GetJson.ExecuteAsync(cts.Token);

// assert
Assert.Empty(result.Errors);
result.Data?.Json.ToString().MatchSnapshot();
}

[ExtendObjectType(OperationTypeNames.Query)]
public class QueryResolvers
{
[GraphQLType(typeof(NonNullType<AnyType>))]
public Dictionary<string, object> GetJson() => new() { { "abc", "def" } };
}

[ExtendObjectType(OperationTypeNames.Query)]
public class LargeQueryResolvers
{
[GraphQLType(typeof(NonNullType<AnyType>))]
public string[] GetJson() => [
$"{0,10000}",
$"{1,10000}",
];
}
}
Loading
Loading