From c10f7628c4505f90b0db5140bfb2ca1b3c54f554 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 29 Jul 2025 21:03:52 +0800 Subject: [PATCH] Remove `ObjectToInferredTypesConverter` from `SystemTextJsonOutputFormatter`. --- .../AspNetCore/Mvc/AbpMvcOptionsExtensions.cs | 18 +++++++++++++++++- .../ObjectToInferredTypesConverter.cs | 5 ----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcOptionsExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcOptionsExtensions.cs index 081342b913..cb63a9e6c8 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcOptionsExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcOptionsExtensions.cs @@ -1,5 +1,9 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Microsoft.Extensions.DependencyInjection; @@ -14,6 +18,7 @@ using Volo.Abp.AspNetCore.Mvc.Response; using Volo.Abp.AspNetCore.Mvc.Uow; using Volo.Abp.AspNetCore.Mvc.Validation; using Volo.Abp.Content; +using Volo.Abp.Json.SystemTextJson.JsonConverters; namespace Volo.Abp.AspNetCore.Mvc; @@ -32,6 +37,17 @@ internal static class AbpMvcOptionsExtensions private static void AddFormatters(MvcOptions options) { options.OutputFormatters.Insert(0, new RemoteStreamContentOutputFormatter()); + var systemTextJsonOutputFormatter = options.OutputFormatters + .Where(f => f is SystemTextJsonOutputFormatter) + .Cast().FirstOrDefault(); + + if (systemTextJsonOutputFormatter != null) + { + options.OutputFormatters.Remove(systemTextJsonOutputFormatter); + var jsonOptions = new JsonSerializerOptions(systemTextJsonOutputFormatter.SerializerOptions); + jsonOptions.Converters.RemoveAll(x => x is ObjectToInferredTypesConverter); + options.OutputFormatters.Add(new SystemTextJsonOutputFormatter(jsonOptions)); + } } private static void AddConventions(MvcOptions options, IServiceCollection services) diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/ObjectToInferredTypesConverter.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/ObjectToInferredTypesConverter.cs index 5daadb865b..74dbe5bc33 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/ObjectToInferredTypesConverter.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/ObjectToInferredTypesConverter.cs @@ -9,11 +9,6 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters; /// public class ObjectToInferredTypesConverter : JsonConverter { - public override bool CanConvert(Type typeToConvert) - { - return typeToConvert == typeof(object); - } - public override object Read( ref Utf8JsonReader reader, Type typeToConvert,