Skip to content

Commit 142a43c

Browse files
authored
Fix memory issue with WriteToStringAsync (graphql-dotnet#909)
* Fix memory issue with WriteToStringAsync * Remove global.json with choco install in appveyor.yml * Throw errors on invalid arguments
1 parent f2c48cb commit 142a43c

File tree

6 files changed

+27
-23
lines changed

6 files changed

+27
-23
lines changed

appveyor.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
version: 2.3.0.{build}
1+
version: 2.4.0.{build}
22
pull_requests:
33
do_not_increment_build_number: true
44
skip_tags: true
5-
skip_branch_with_pr: true
5+
skip_branch_with_pr: false
66
test: off
77

8+
os: Visual Studio 2017
9+
810
environment:
911
CI: true
1012

@@ -15,6 +17,7 @@ nuget:
1517

1618
install:
1719
- ps: Install-Product node LTS
20+
- ps: choco install dotnetcore-sdk --no-progress --confirm --version 2.1.403
1821
- node --version
1922
- npm --version
2023
- dotnet --version
@@ -38,4 +41,3 @@ deploy:
3841
branch:
3942
- master
4043
- dev
41-
- instance

global.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-dotnet",
3-
"version": "2.3.0",
3+
"version": "2.4.0",
44
"description": "GraphQL for .NET",
55
"main": "index.js",
66
"scripts": {

src/GraphQL/GraphQL.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>GraphQL for .NET</Description>
5-
<VersionPrefix>2.3.0</VersionPrefix>
5+
<VersionPrefix>2.4.0</VersionPrefix>
66
<Authors>Joe McBride</Authors>
77
<TargetFrameworks>netstandard2.0;netstandard1.3;net45</TargetFrameworks>
88
<AssemblyName>GraphQL</AssemblyName>

src/GraphQL/Http/DocumentWriter.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IDocumentWriter
1313

1414
Task<IByteResult> WriteAsync<T>(T value);
1515

16-
[Obsolete("This method is obsolete and will be removed in the next major version. Use WriteAsync instead.")]
16+
[Obsolete("This method is obsolete and will be removed in the next major version. Use WriteAsync<T>(Stream, T) instead.")]
1717
string Write(object value);
1818
}
1919

@@ -23,7 +23,7 @@ public class DocumentWriter : IDocumentWriter
2323
private readonly JsonArrayPool _jsonArrayPool = new JsonArrayPool(ArrayPool<char>.Shared);
2424
private readonly int _maxArrayLength = 1048576;
2525
private readonly JsonSerializer _serializer;
26-
private static readonly Encoding Utf8Encoding = new UTF8Encoding(false);
26+
internal static readonly Encoding Utf8Encoding = new UTF8Encoding(false);
2727

2828
public DocumentWriter()
2929
: this(indent: false)
@@ -77,6 +77,13 @@ public async Task<IByteResult> WriteAsync<T>(T value)
7777

7878
public string Write(object value)
7979
{
80+
if (value == null) throw new ArgumentNullException(nameof(value));
81+
82+
if (!(value is ExecutionResult))
83+
{
84+
throw new ArgumentOutOfRangeException($"Expected {nameof(value)} to be a GraphQL.ExecutionResult, got {value.GetType().FullName}");
85+
}
86+
8087
return this.WriteToStringAsync((ExecutionResult) value).GetAwaiter().GetResult();
8188
}
8289
}
@@ -88,19 +95,19 @@ public static class DocumentWriterExtensions
8895
/// </summary>
8996
/// <param name="writer"></param>
9097
/// <param name="value"></param>
91-
/// <param name="encoding"></param>
9298
/// <returns></returns>
93-
public static async Task<string> WriteToStringAsync(this IDocumentWriter writer,
94-
ExecutionResult value,
95-
Encoding encoding = null)
99+
public static async Task<string> WriteToStringAsync(
100+
this IDocumentWriter writer,
101+
ExecutionResult value)
96102
{
97-
var resolvedEncoding = encoding ?? Encoding.UTF8;
98-
using (var buffer = await writer.WriteAsync(value).ConfigureAwait(false))
103+
using(var stream = new MemoryStream())
99104
{
100-
return buffer.Result.Array != null
101-
? resolvedEncoding.GetString(buffer.Result.Array, buffer.Result.Offset,
102-
buffer.Result.Count)
103-
: null;
105+
await writer.WriteAsync(stream, value);
106+
stream.Position = 0;
107+
using (var reader = new StreamReader(stream, DocumentWriter.Utf8Encoding))
108+
{
109+
return await reader.ReadToEndAsync();
110+
}
104111
}
105112
}
106113
}

src/GraphQL/Http/HttpResponseStreamWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public HttpResponseStreamWriter(
6060
}
6161
if (!_stream.CanWrite)
6262
{
63-
throw new ArgumentException("Stream is now writable", nameof(stream));
63+
throw new ArgumentException("Stream is not writable", nameof(stream));
6464
}
6565

6666
_charBufferSize = bufferSize;

0 commit comments

Comments
 (0)