Skip to content

Commit 4191eb5

Browse files
authored
Use System.Text.Json's copy ctor (#21016)
1 parent a0d2a0a commit 4191eb5

File tree

4 files changed

+62
-38
lines changed

4 files changed

+62
-38
lines changed

src/Mvc/Mvc.Core/src/Formatters/SystemTextJsonOutputFormatter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ internal static SystemTextJsonOutputFormatter CreateFormatter(JsonOptions jsonOp
4040
if (jsonSerializerOptions.Encoder is null)
4141
{
4242
// If the user hasn't explicitly configured the encoder, use the less strict encoder that does not encode all non-ASCII characters.
43-
jsonSerializerOptions = jsonSerializerOptions.Copy(JavaScriptEncoder.UnsafeRelaxedJsonEscaping);
43+
jsonSerializerOptions = new JsonSerializerOptions(jsonSerializerOptions)
44+
{
45+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
46+
};
4447
}
4548

4649
return new SystemTextJsonOutputFormatter(jsonSerializerOptions);

src/Mvc/Mvc.Core/src/Infrastructure/JsonSerializerOptionsCopyConstructor.cs

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

src/Mvc/Mvc.Core/test/Formatters/SystemTextJsonOutputFormatterTest.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.IO;
5+
using System.Text;
6+
using System.Text.Json.Serialization;
7+
using System.Threading.Tasks;
8+
using Microsoft.Extensions.Primitives;
9+
using Microsoft.Net.Http.Headers;
10+
using Xunit;
11+
412
namespace Microsoft.AspNetCore.Mvc.Formatters
513
{
614
public class SystemTextJsonOutputFormatterTest : JsonOutputFormatterTestBase
@@ -9,5 +17,51 @@ protected override TextOutputFormatter GetOutputFormatter()
917
{
1018
return SystemTextJsonOutputFormatter.CreateFormatter(new JsonOptions());
1119
}
20+
21+
[Fact]
22+
public async Task WriteResponseBodyAsync_AllowsConfiguringPreserveReferenceHandling()
23+
{
24+
// Arrange
25+
var formatter = GetOutputFormatter();
26+
((SystemTextJsonOutputFormatter)formatter).SerializerOptions.ReferenceHandling = ReferenceHandling.Preserve;
27+
var expectedContent = "{\"$id\":\"1\",\"name\":\"Person\",\"child\":{\"$id\":\"2\",\"name\":\"Child\",\"child\":null,\"parent\":{\"$ref\":\"1\"}},\"parent\":null}";
28+
var person = new Person
29+
{
30+
Name = "Person",
31+
Child = new Person { Name = "Child", },
32+
};
33+
person.Child.Parent = person;
34+
35+
var mediaType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
36+
var encoding = CreateOrGetSupportedEncoding(formatter, "utf-8", isDefaultEncoding: true);
37+
38+
var body = new MemoryStream();
39+
var actionContext = GetActionContext(mediaType, body);
40+
41+
var outputFormatterContext = new OutputFormatterWriteContext(
42+
actionContext.HttpContext,
43+
new TestHttpResponseStreamWriterFactory().CreateWriter,
44+
typeof(Person),
45+
person)
46+
{
47+
ContentType = new StringSegment(mediaType.ToString()),
48+
};
49+
50+
// Act
51+
await formatter.WriteResponseBodyAsync(outputFormatterContext, Encoding.GetEncoding("utf-8"));
52+
53+
// Assert
54+
var actualContent = encoding.GetString(body.ToArray());
55+
Assert.Equal(expectedContent, actualContent);
56+
}
57+
58+
private class Person
59+
{
60+
public string Name { get; set; }
61+
62+
public Person Child { get; set; }
63+
64+
public Person Parent { get; set; }
65+
}
1266
}
1367
}

src/Mvc/Mvc.ViewFeatures/src/Rendering/SystemTextJsonHelper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ private static JsonSerializerOptions GetHtmlSafeSerializerOptions(JsonSerializer
3333
return serializerOptions;
3434
}
3535

36-
return serializerOptions.Copy(JavaScriptEncoder.Default);
36+
return new JsonSerializerOptions(serializerOptions)
37+
{
38+
Encoder = JavaScriptEncoder.Default,
39+
};
3740
}
3841
}
3942
}

0 commit comments

Comments
 (0)