Skip to content

Commit e39d168

Browse files
committed
Changing IsPolymorphicSafe
1 parent fba0039 commit e39d168

File tree

5 files changed

+10
-7
lines changed

5 files changed

+10
-7
lines changed

src/Http/Http.Extensions/src/RequestDelegateFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
10551055
{
10561056
var jsonTypeInfo = factoryContext.JsonSerializerOptions.GetReadOnlyTypeInfo(typeArg);
10571057

1058-
if (jsonTypeInfo.IsPolymorphicSafe() == true)
1058+
if (jsonTypeInfo.HasKnownPolymorphism())
10591059
{
10601060
return Expression.Call(
10611061
ExecuteTaskOfTFastMethod.MakeGenericMethod(typeArg),
@@ -1096,7 +1096,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
10961096
{
10971097
var jsonTypeInfo = factoryContext.JsonSerializerOptions.GetReadOnlyTypeInfo(typeArg);
10981098

1099-
if (jsonTypeInfo.IsPolymorphicSafe() == true)
1099+
if (jsonTypeInfo.HasKnownPolymorphism())
11001100
{
11011101
return Expression.Call(
11021102
ExecuteValueTaskOfTFastMethod.MakeGenericMethod(typeArg),
@@ -1140,7 +1140,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
11401140
{
11411141
var jsonTypeInfo = factoryContext.JsonSerializerOptions.GetReadOnlyTypeInfo(returnType);
11421142

1143-
if (jsonTypeInfo.IsPolymorphicSafe() == true)
1143+
if (jsonTypeInfo.HasKnownPolymorphism())
11441144
{
11451145
return Expression.Call(
11461146
JsonResultWriteResponseOfTFastAsyncMethod.MakeGenericMethod(returnType),

src/Http/Http.Results/src/HttpResultsHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static Task WriteResultAsJsonAsync<TValue>(
3535
var jsonTypeInfo = (JsonTypeInfo<TValue>)jsonSerializerOptions.GetTypeInfo(typeof(TValue));
3636

3737
Type? runtimeType;
38-
if (jsonTypeInfo.IsPolymorphicSafe() || jsonTypeInfo.Type == (runtimeType = value.GetType()))
38+
if (jsonTypeInfo.IsValid(runtimeType = value.GetType()))
3939
{
4040
Log.WritingResultAsJson(logger, jsonTypeInfo.Type.Name);
4141
return httpContext.Response.WriteAsJsonAsync(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public sealed override async Task WriteResponseBodyAsync(OutputFormatterWriteCon
7272
{
7373
var declaredTypeJsonInfo = SerializerOptions.GetTypeInfo(context.ObjectType);
7474

75-
if (declaredTypeJsonInfo.IsPolymorphicSafe() || context.Object is null || runtimeType == declaredTypeJsonInfo.Type)
75+
if (declaredTypeJsonInfo.IsValid(runtimeType))
7676
{
7777
jsonTypeInfo = declaredTypeJsonInfo;
7878
}

src/Shared/Json/JsonSerializerExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ namespace Microsoft.AspNetCore.Http;
99

1010
internal static class JsonSerializerExtensions
1111
{
12-
public static bool IsPolymorphicSafe(this JsonTypeInfo jsonTypeInfo)
12+
public static bool HasKnownPolymorphism(this JsonTypeInfo jsonTypeInfo)
1313
=> jsonTypeInfo.Type.IsSealed || jsonTypeInfo.Type.IsValueType || jsonTypeInfo.PolymorphismOptions is not null;
1414

15+
public static bool IsValid(this JsonTypeInfo jsonTypeInfo, Type? runtimeType)
16+
=> runtimeType is null || jsonTypeInfo.Type == runtimeType || jsonTypeInfo.HasKnownPolymorphism();
17+
1518
public static JsonTypeInfo GetReadOnlyTypeInfo(this JsonSerializerOptions options, Type type)
1619
{
1720
options.MakeReadOnly();

src/Shared/RouteHandlers/ExecuteHandlerHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static Task WriteJsonResponseAsync<T>(HttpResponse response, T? value, Js
3737
{
3838
var runtimeType = value?.GetType();
3939

40-
if (runtimeType is null || jsonTypeInfo.Type == runtimeType || jsonTypeInfo.IsPolymorphicSafe())
40+
if (jsonTypeInfo.IsValid(runtimeType))
4141
{
4242
// In this case the polymorphism is not
4343
// relevant for us and will be handled by STJ, if needed.

0 commit comments

Comments
 (0)