Skip to content

Commit 9e86b1b

Browse files
authored
Fixed issue that closed stream when using StreamWriter in SyntaxPrinter.PrintToAsync (#7657)
1 parent 6263921 commit 9e86b1b

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

src/HotChocolate/Language/src/Language.SyntaxTree/Utilities/SyntaxPrinter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ public static async ValueTask PrintToAsync(
6060
#if NETSTANDARD2_0
6161
using var streamWriter = new StreamWriter(
6262
stream,
63-
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true));
63+
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true),
64+
-1,
65+
leaveOpen: true);
6466
#else
6567
await using var streamWriter = new StreamWriter(
6668
stream,
67-
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true));
69+
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true),
70+
leaveOpen: true);
6871
#endif
6972

7073
var syntaxWriter = StringSyntaxWriter.Rent();

src/HotChocolate/PersistedOperations/test/PersistedOperations.FileSystem.Tests/IntegrationTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.DependencyInjection;
33
using HotChocolate.Types;
44
using HotChocolate.Execution;
5+
using HotChocolate.Language;
56
using IO = System.IO;
67

78
namespace HotChocolate.PersistedOperations.FileSystem;
@@ -83,4 +84,56 @@ public async Task ExecutePersistedOperation_NotFound()
8384
File.Delete(cachedOperation);
8485
result.MatchSnapshot();
8586
}
87+
88+
[Fact]
89+
public async Task ExecuteAutomaticPersistedOperation()
90+
{
91+
// arrange
92+
var cacheDirectory = IO.Path.GetTempPath();
93+
var documentId = Guid.NewGuid().ToString("N");
94+
const string documentHash = "hash";
95+
96+
var executor =
97+
await new ServiceCollection()
98+
.AddGraphQL()
99+
.AddQueryType(c => c.Name("Query").Field("a").Resolve("b"))
100+
.AddFileSystemOperationDocumentStorage(cacheDirectory)
101+
.UseRequest(n => async c =>
102+
{
103+
await n(c);
104+
105+
if (c.IsPersistedDocument && c.Result is IOperationResult r)
106+
{
107+
c.Result = OperationResultBuilder
108+
.FromResult(r)
109+
.SetExtension("persistedDocument", true)
110+
.Build();
111+
}
112+
})
113+
.UseAutomaticPersistedOperationPipeline()
114+
.BuildRequestExecutorAsync();
115+
116+
// act
117+
var result = await executor.ExecuteAsync(
118+
OperationRequest
119+
.FromId(documentId)
120+
.WithDocument(new OperationDocument(Utf8GraphQLParser.Parse("{ __typename }")))
121+
.WithDocumentHash(documentHash)
122+
.WithExtensions(new Dictionary<string, object?>
123+
{
124+
{
125+
"persistedQuery",
126+
new Dictionary<string, object?>
127+
{
128+
{ "version", 1 },
129+
{ "md5Hash", documentHash }
130+
}
131+
}
132+
}));
133+
134+
File.Delete(IO.Path.Combine(cacheDirectory, documentHash + ".graphql"));
135+
136+
// assert
137+
result.MatchSnapshot();
138+
}
86139
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"data": {
3+
"__typename": "Query"
4+
},
5+
"extensions": {
6+
"persistedQuery": {
7+
"md5Hash": "hash",
8+
"persisted": true
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)