Skip to content

Commit f3b5fb5

Browse files
Fix schema settings serialization during export (#9744)
1 parent 9a60aee commit f3b5fb5

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

src/HotChocolate/AspNetCore/test/AspNetCore.CommandLine.Tests/SchemaExportCommandTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,62 @@ public async Task App_Should_WriteNamedSchemaToOutput_When_SchemaNameIsSpecified
143143
output.ToString().MatchSnapshot();
144144
}
145145

146+
[Fact]
147+
public async Task App_Should_PreserveUnescapedAngleBrackets_When_UpdatingExistingSettingsFile()
148+
{
149+
// arrange
150+
var services = new ServiceCollection();
151+
services.AddGraphQL()
152+
.AddQueryType(x => x.Name("Query").Field("foo").Resolve("bar"));
153+
154+
var hostMock = new Mock<IHost>();
155+
hostMock
156+
.Setup(x => x.Services)
157+
.Returns(services.BuildServiceProvider());
158+
159+
var host = hostMock.Object;
160+
var output = new StringWriter();
161+
var app = new App(host);
162+
var tempFile = CreateSchemaFileName();
163+
var settingsFile = tempFile + "-settings.json";
164+
165+
const string existingSettings =
166+
"""
167+
{
168+
"name": "default",
169+
"satisfiability": {
170+
"ignoredNonAccessibleFields": {
171+
"Foo.bar": [
172+
"Schema1:Query.foo<Bar>"
173+
]
174+
}
175+
}
176+
}
177+
""";
178+
179+
await File.WriteAllTextAsync(settingsFile, existingSettings);
180+
181+
// act
182+
await app.InvokeAsync($"schema export --output {tempFile}", output);
183+
184+
// assert
185+
var actual = await File.ReadAllTextAsync(settingsFile);
186+
actual.ReplaceLineEndings("\n").MatchInlineSnapshot(
187+
"""
188+
{
189+
"name": "_Default",
190+
"satisfiability": {
191+
"ignoredNonAccessibleFields": {
192+
"Foo.bar": [
193+
"Schema1:Query.foo<Bar>"
194+
]
195+
}
196+
}
197+
}
198+
199+
""");
200+
}
201+
146202
[Fact]
147203
public async Task App_Should_Return_ExitCode_1_If_Schema_Is_Invalid()
148204
{

src/HotChocolate/Core/src/Types/Execution/Internal/SchemaFileExporter.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text;
2+
using System.Text.Encodings.Web;
23
using System.Text.Json;
34
using System.Text.Json.Nodes;
45
using HotChocolate.Serialization;
@@ -7,6 +8,12 @@ namespace HotChocolate.Execution.Internal;
78

89
internal static class SchemaFileExporter
910
{
11+
private static readonly JsonWriterOptions s_writerOptions = new()
12+
{
13+
Indented = true,
14+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
15+
};
16+
1017
public static async Task<SchemaFileInfo> Export(
1118
string schemaFileName,
1219
IRequestExecutor executor,
@@ -84,7 +91,7 @@ private static async Task<bool> TryUpdateSettingsFile(
8491
obj["name"] = schemaName;
8592

8693
await using var writeStream = File.Create(fileName);
87-
await using var writer = new Utf8JsonWriter(writeStream, new JsonWriterOptions { Indented = true });
94+
await using var writer = new Utf8JsonWriter(writeStream, s_writerOptions);
8895
root.WriteTo(writer);
8996
await writer.FlushAsync(cancellationToken);
9097
await writeStream.WriteAsync(Encoding.UTF8.GetBytes(Environment.NewLine), cancellationToken);
@@ -105,7 +112,7 @@ private static async Task CreateNewSettingsFile(
105112
CancellationToken cancellationToken)
106113
{
107114
await using var settingsFileStream = File.Create(fileName);
108-
await using var jsonWriter = new Utf8JsonWriter(settingsFileStream, new JsonWriterOptions { Indented = true });
115+
await using var jsonWriter = new Utf8JsonWriter(settingsFileStream, s_writerOptions);
109116

110117
jsonWriter.WriteStartObject();
111118

0 commit comments

Comments
 (0)