From 171f7971c4e21e894ba93c2f06a44ae575a7b2cb Mon Sep 17 00:00:00 2001 From: Super Date: Fri, 24 Oct 2025 22:56:18 +0800 Subject: [PATCH 01/17] Redo `AbpDateTimeModelBinder` to align with the current TZ For https://github.com/abpframework/abp/issues/24048. --- .../ModelBinding/AbpDateTimeModelBinder.cs | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs index 35a06c9c1d..35e1777f4c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs @@ -20,9 +20,35 @@ public class AbpDateTimeModelBinder : IModelBinder public async Task BindModelAsync(ModelBindingContext bindingContext) { await _dateTimeModelBinder.BindModelAsync(bindingContext); - if (bindingContext.Result.IsModelSet && bindingContext.Result.Model is DateTime dateTime) + + if (!bindingContext.Result.IsModelSet || bindingContext.Result.Model is not DateTime dateTime) { - bindingContext.Result = ModelBindingResult.Success(_clock.Normalize(dateTime)); + return; } + + // If the DateTime has no timezone info (most cases from input) + if (dateTime.Kind == DateTimeKind.Unspecified) + { + // Try to get user's timezone + var userTz = _currentTimezoneProvider.TimeZone; + if (!userTz.IsNullOrWhiteSpace()) + { + try + { + var tzInfo = _timezoneProvider.GetTimeZoneInfo(userTz); + // Treat the input as user's local time and convert to UTC + var utc = TimeZoneInfo.ConvertTimeToUtc(dateTime, tzInfo); + bindingContext.Result = ModelBindingResult.Success(utc); + return; + } + catch + { + // fallback to default clock normalization if invalid TZ + } + } + } + + // fallback: original behavior + bindingContext.Result = ModelBindingResult.Success(_clock.Normalize(dateTime)); } } From fcf835a8af7f3ef67281080206b2f4526a870622 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 8 Dec 2025 20:02:26 +0100 Subject: [PATCH 02/17] Use frozen collections where suitable --- .../AbpDatePickerBaseTagHelperService.cs | 31 ++++++++++--------- ...UiObjectExtensionPropertyInfoExtensions.cs | 5 +-- .../AbpBackgroundWorkersTickerQOptions.cs | 8 ++--- ...UiObjectExtensionPropertyInfoExtensions.cs | 9 +++--- .../ProjectModification/NpmPackagesUpdater.cs | 7 +---- .../Abp/Logging/DefaultInitLoggerFactory.cs | 4 +-- .../Volo/Abp/Reflection/TypeHelper.cs | 9 +++--- ...PostConfigureAbpRabbitMqEventBusOptions.cs | 13 ++++---- .../Generators/ProxyScriptingJsFuncHelper.cs | 5 +-- .../LocalizationResourceDictionary.cs | 2 +- .../MemoryDb/MemoryDatabaseCollection.cs | 2 +- .../Volo/Abp/MongoDB/MongoModelBuilder.cs | 2 +- .../Abp/Specifications/ParameterRebinder.cs | 2 +- .../MultiLingualObjectManager_Tests.cs | 5 +-- .../app/VoloDocs.Web/Pages/Error.cshtml.cs | 5 +-- 15 files changed, 57 insertions(+), 52 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/DatePicker/AbpDatePickerBaseTagHelperService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/DatePicker/AbpDatePickerBaseTagHelperService.cs index 5088c08293..3f7b6b0afa 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/DatePicker/AbpDatePickerBaseTagHelperService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/DatePicker/AbpDatePickerBaseTagHelperService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Frozen; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; @@ -23,7 +24,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form.DatePicker; public abstract class AbpDatePickerBaseTagHelperService : AbpTagHelperService where TTagHelper : AbpDatePickerBaseTagHelper { - protected readonly Dictionary> SupportedInputTypes; + protected readonly FrozenDictionary> SupportedInputTypes; protected readonly IJsonSerializer JsonSerializer; protected readonly IHtmlGenerator Generator; @@ -103,7 +104,7 @@ public abstract class AbpDatePickerBaseTagHelperService : AbpTagHelp return string.Empty; } } - }; + }.ToFrozenDictionary(); } protected virtual T? GetAttribute() where T : Attribute @@ -136,7 +137,7 @@ public abstract class AbpDatePickerBaseTagHelperService : AbpTagHelp ? await ProcessButtonAndGetContentAsync(context, output, "calendar", "open") : ""; var clearButtonContent = TagHelper.ClearButton == true || (!TagHelper.ClearButton.HasValue && TagHelper.AutoUpdateInput != true) - ? await ProcessButtonAndGetContentAsync(context, output, "times", "clear", visible:!TagHelper.SingleOpenAndClearButton) + ? await ProcessButtonAndGetContentAsync(context, output, "times", "clear", visible: !TagHelper.SingleOpenAndClearButton) : ""; var labelContent = await GetLabelAsHtmlAsync(context, output, TagHelperOutput); @@ -269,7 +270,7 @@ public abstract class AbpDatePickerBaseTagHelperService : AbpTagHelp { var attrList = new TagHelperAttributeList(); - if(options == null) + if (options == null) { return attrList; } @@ -401,29 +402,29 @@ public abstract class AbpDatePickerBaseTagHelperService : AbpTagHelp attrList.Add("data-visible-date-format", options.VisibleDateFormat); } - if(!options.InputDateFormat.IsNullOrEmpty()) + if (!options.InputDateFormat.IsNullOrEmpty()) { attrList.Add("data-input-date-format", options.InputDateFormat); } - if(options.Ranges != null && options.Ranges.Any()) + if (options.Ranges != null && options.Ranges.Any()) { var ranges = options.Ranges.ToDictionary(r => r.Label, r => r.Dates); attrList.Add("data-ranges", JsonSerializer.Serialize(ranges)); } - if(options.AlwaysShowCalendars != null) + if (options.AlwaysShowCalendars != null) { attrList.Add("data-always-show-calendars", options.AlwaysShowCalendars.ToString()!.ToLowerInvariant()); } - if(options.ShowCustomRangeLabel == false) + if (options.ShowCustomRangeLabel == false) { attrList.Add("data-show-custom-range-label", options.ShowCustomRangeLabel.ToString()!.ToLowerInvariant()); } - if(options.Options != null) + if (options.Options != null) { attrList.Add("data-options", JsonSerializer.Serialize(options.Options)); } @@ -443,7 +444,7 @@ public abstract class AbpDatePickerBaseTagHelperService : AbpTagHelp attrList.Add("id", options.PickerId); } - if(!options.SingleOpenAndClearButton) + if (!options.SingleOpenAndClearButton) { attrList.Add("data-single-open-and-clear-button", options.SingleOpenAndClearButton.ToString().ToLowerInvariant()); } @@ -614,7 +615,8 @@ public abstract class AbpDatePickerBaseTagHelperService : AbpTagHelp { return string.Empty; } - var labelTagHelper = new LabelTagHelper(Generator) { + var labelTagHelper = new LabelTagHelper(Generator) + { ViewContext = TagHelper.ViewContext, For = modelExpression }; @@ -764,7 +766,8 @@ public abstract class AbpDatePickerBaseTagHelperService : AbpTagHelp TagHelper.Size = attribute.Size; } - return TagHelper.Size switch { + return TagHelper.Size switch + { AbpFormControlSize.Small => "form-control-sm", AbpFormControlSize.Medium => "form-control-md", AbpFormControlSize.Large => "form-control-lg", @@ -785,14 +788,14 @@ public abstract class AbpDatePickerBaseTagHelperService : AbpTagHelp protected virtual async Task GetValidationAsHtmlByInputAsync(TagHelperContext context, TagHelperOutput output, - [NotNull]ModelExpression @for) + [NotNull] ModelExpression @for) { var validationMessageTagHelper = new ValidationMessageTagHelper(Generator) { For = @for, ViewContext = TagHelper.ViewContext }; var attributeList = new TagHelperAttributeList { { "class", "text-danger" } }; - if(!output.Attributes.TryGetAttribute("name", out var nameAttribute) || nameAttribute == null || nameAttribute.Value == null) + if (!output.Attributes.TryGetAttribute("name", out var nameAttribute) || nameAttribute == null || nameAttribute.Value == null) { if (nameAttribute != null) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/ObjectExtending/MvcUiObjectExtensionPropertyInfoExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/ObjectExtending/MvcUiObjectExtensionPropertyInfoExtensions.cs index 62b86bc1c5..ae5d30a2ac 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/ObjectExtending/MvcUiObjectExtensionPropertyInfoExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/ObjectExtending/MvcUiObjectExtensionPropertyInfoExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Frozen; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; @@ -8,7 +9,7 @@ namespace Volo.Abp.ObjectExtending; public static class MvcUiObjectExtensionPropertyInfoExtensions { - private static readonly HashSet NumberTypes = new HashSet { + private static readonly FrozenSet NumberTypes = new HashSet { typeof(int), typeof(long), typeof(byte), @@ -33,7 +34,7 @@ public static class MvcUiObjectExtensionPropertyInfoExtensions typeof(float?), typeof(double?), typeof(decimal?) - }; + }.ToFrozenSet(); public static string? GetInputFormatOrNull(this IBasicObjectExtensionPropertyInfo property) { diff --git a/framework/src/Volo.Abp.BackgroundWorkers.TickerQ/Volo/Abp/BackgroundWorkers/TickerQ/AbpBackgroundWorkersTickerQOptions.cs b/framework/src/Volo.Abp.BackgroundWorkers.TickerQ/Volo/Abp/BackgroundWorkers/TickerQ/AbpBackgroundWorkersTickerQOptions.cs index 6d48a21262..438686d29f 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.TickerQ/Volo/Abp/BackgroundWorkers/TickerQ/AbpBackgroundWorkersTickerQOptions.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.TickerQ/Volo/Abp/BackgroundWorkers/TickerQ/AbpBackgroundWorkersTickerQOptions.cs @@ -5,11 +5,11 @@ namespace Volo.Abp.BackgroundWorkers.TickerQ; public class AbpBackgroundWorkersTickerQOptions { - private readonly Dictionary _onfigurations; + private readonly Dictionary _configurations; public AbpBackgroundWorkersTickerQOptions() { - _onfigurations = new Dictionary(); + _configurations = new Dictionary(); } public void AddConfiguration(AbpBackgroundWorkersCronTickerConfiguration configuration) @@ -19,7 +19,7 @@ public class AbpBackgroundWorkersTickerQOptions public void AddConfiguration(Type workerType, AbpBackgroundWorkersCronTickerConfiguration configuration) { - _onfigurations[workerType] = configuration; + _configurations[workerType] = configuration; } public AbpBackgroundWorkersCronTickerConfiguration? GetConfigurationOrNull() @@ -29,6 +29,6 @@ public class AbpBackgroundWorkersTickerQOptions public AbpBackgroundWorkersCronTickerConfiguration? GetConfigurationOrNull(Type workerType) { - return _onfigurations.GetValueOrDefault(workerType); + return _configurations.GetValueOrDefault(workerType); } } diff --git a/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiObjectExtensionPropertyInfoExtensions.cs b/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiObjectExtensionPropertyInfoExtensions.cs index 3026682f62..d535c87c00 100644 --- a/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiObjectExtensionPropertyInfoExtensions.cs +++ b/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiObjectExtensionPropertyInfoExtensions.cs @@ -1,5 +1,6 @@ using Blazorise; using System; +using System.Collections.Frozen; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; @@ -11,7 +12,7 @@ namespace Volo.Abp.BlazoriseUI; public static class BlazoriseUiObjectExtensionPropertyInfoExtensions { - private static readonly HashSet NumberTypes = new HashSet { + private static readonly FrozenSet NumberTypes = new HashSet { typeof(int), typeof(long), typeof(byte), @@ -36,13 +37,13 @@ public static class BlazoriseUiObjectExtensionPropertyInfoExtensions typeof(float?), typeof(double?), typeof(decimal?) - }; + }.ToFrozenSet(); - private static readonly HashSet TextEditSupportedAttributeTypes = new HashSet { + private static readonly FrozenSet TextEditSupportedAttributeTypes = new HashSet { typeof(EmailAddressAttribute), typeof(UrlAttribute), typeof(PhoneAttribute) - }; + }.ToFrozenSet(); public static string? GetDateEditInputFormatOrNull(this IBasicObjectExtensionPropertyInfo property) { diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs index b8ba1ea113..9f2cb4d221 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs @@ -3,14 +3,12 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NuGet.Versioning; -using Volo.Abp.Cli.Http; using Volo.Abp.Cli.LIbs; using Volo.Abp.Cli.Utils; using Volo.Abp.DependencyInjection; @@ -28,14 +26,12 @@ public class NpmPackagesUpdater : ITransientDependency private readonly PackageJsonFileFinder _packageJsonFileFinder; private readonly NpmGlobalPackagesChecker _npmGlobalPackagesChecker; - private readonly Dictionary _fileVersionStorage = new Dictionary(); - private readonly CliHttpClientFactory _cliHttpClientFactory; + private readonly Dictionary _fileVersionStorage = []; public NpmPackagesUpdater( PackageJsonFileFinder packageJsonFileFinder, NpmGlobalPackagesChecker npmGlobalPackagesChecker, ICancellationTokenProvider cancellationTokenProvider, - CliHttpClientFactory cliHttpClientFactory, IInstallLibsService installLibsService, ICmdHelper cmdHelper) { @@ -44,7 +40,6 @@ public class NpmPackagesUpdater : ITransientDependency CancellationTokenProvider = cancellationTokenProvider; InstallLibsService = installLibsService; CmdHelper = cmdHelper; - _cliHttpClientFactory = cliHttpClientFactory; Logger = NullLogger.Instance; } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLoggerFactory.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLoggerFactory.cs index 1fa5d45090..638888493d 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLoggerFactory.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Logging/DefaultInitLoggerFactory.cs @@ -5,10 +5,10 @@ namespace Volo.Abp.Logging; public class DefaultInitLoggerFactory : IInitLoggerFactory { - private readonly Dictionary _cache = new Dictionary(); + private readonly Dictionary _cache = []; public virtual IInitLogger Create() { - return (IInitLogger)_cache.GetOrAdd(typeof(T), () => new DefaultInitLogger()); ; + return (IInitLogger)_cache.GetOrAdd(typeof(T), () => new DefaultInitLogger()); } } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs index 0118fd9874..87c9419cfa 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Frozen; using System.Collections.Generic; using System.Collections.Immutable; using System.ComponentModel; @@ -13,14 +14,14 @@ namespace Volo.Abp.Reflection; public static class TypeHelper { - private static readonly HashSet FloatingTypes = new HashSet + private static readonly FrozenSet FloatingTypes = new HashSet { typeof(float), typeof(double), typeof(decimal) - }; + }.ToFrozenSet(); - private static readonly HashSet NonNullablePrimitiveTypes = new HashSet + private static readonly FrozenSet NonNullablePrimitiveTypes = new HashSet { typeof(byte), typeof(short), @@ -37,7 +38,7 @@ public static class TypeHelper typeof(DateTimeOffset), typeof(TimeSpan), typeof(Guid) - }; + }.ToFrozenSet(); public static bool IsNonNullablePrimitiveType(Type type) { diff --git a/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/PostConfigureAbpRabbitMqEventBusOptions.cs b/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/PostConfigureAbpRabbitMqEventBusOptions.cs index f4e69c46c6..2c400e01da 100644 --- a/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/PostConfigureAbpRabbitMqEventBusOptions.cs +++ b/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/PostConfigureAbpRabbitMqEventBusOptions.cs @@ -1,3 +1,4 @@ +using System.Collections.Frozen; using System.Collections.Generic; using Microsoft.Extensions.Options; @@ -5,8 +6,8 @@ namespace Volo.Abp.EventBus.RabbitMq; public class PostConfigureAbpRabbitMqEventBusOptions : IPostConfigureOptions { - private readonly HashSet _uint64QueueArguments = - [ + private readonly FrozenSet _uint64QueueArguments = new HashSet() + { "x-delivery-limit", "x-expires", "x-message-ttl", @@ -16,12 +17,12 @@ public class PostConfigureAbpRabbitMqEventBusOptions : IPostConfigureOptions _boolQueueArguments = - [ + private readonly FrozenSet _boolQueueArguments = new HashSet() + { "x-single-active-consumer" - ]; + }.ToFrozenSet(); public virtual void PostConfigure(string? name, AbpRabbitMqEventBusOptions options) { diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs index 33c5b0d05f..21547bd05d 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Frozen; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,7 +11,7 @@ internal static class ProxyScriptingJsFuncHelper { private const string ValidJsVariableNameChars = "abcdefghijklmnopqrstuxwvyzABCDEFGHIJKLMNOPQRSTUXWVYZ0123456789_"; - private static readonly HashSet ReservedWords = new HashSet { + private static readonly FrozenSet ReservedWords = new HashSet { "abstract", "else", "instanceof", @@ -71,7 +72,7 @@ internal static class ProxyScriptingJsFuncHelper "in", "static", "with" - }; + }.ToFrozenSet(); public static string NormalizeJsVariableName(string name, string additionalChars = "") { diff --git a/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResourceDictionary.cs b/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResourceDictionary.cs index f6487a85b7..0b8d3d7f63 100644 --- a/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResourceDictionary.cs +++ b/framework/src/Volo.Abp.Localization/Volo/Abp/Localization/LocalizationResourceDictionary.cs @@ -6,7 +6,7 @@ namespace Volo.Abp.Localization; public class LocalizationResourceDictionary : Dictionary { - private readonly Dictionary _resourcesByTypes = new(); + private readonly Dictionary _resourcesByTypes = []; public LocalizationResource Add(string? defaultCultureName = null) { diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseCollection.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseCollection.cs index 3e08c63417..7f4f9dbcdf 100644 --- a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseCollection.cs +++ b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseCollection.cs @@ -8,7 +8,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb; public class MemoryDatabaseCollection : IMemoryDatabaseCollection where TEntity : class, IEntity { - private readonly Dictionary _dictionary = new Dictionary(); + private readonly Dictionary _dictionary = []; private readonly IMemoryDbSerializer _memoryDbSerializer; diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs index d7ad79f7d0..a68f1584f8 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs @@ -17,7 +17,7 @@ public class MongoModelBuilder : IMongoModelBuilder { private readonly Dictionary _entityModelBuilders; - private static readonly object SyncObj = new object(); + private static readonly object SyncObj = new(); public MongoModelBuilder() { diff --git a/framework/src/Volo.Abp.Specifications/Volo/Abp/Specifications/ParameterRebinder.cs b/framework/src/Volo.Abp.Specifications/Volo/Abp/Specifications/ParameterRebinder.cs index d3553369b8..be0d8ab3e9 100644 --- a/framework/src/Volo.Abp.Specifications/Volo/Abp/Specifications/ParameterRebinder.cs +++ b/framework/src/Volo.Abp.Specifications/Volo/Abp/Specifications/ParameterRebinder.cs @@ -15,7 +15,7 @@ internal class ParameterRebinder : ExpressionVisitor internal ParameterRebinder(Dictionary map) { - _map = map ?? new Dictionary(); + _map = map ?? []; } internal static Expression ReplaceParameters(Dictionary map, diff --git a/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs b/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs index e442d80953..6b18822df3 100644 --- a/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs +++ b/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Frozen; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -18,12 +19,12 @@ public class MultiLingualObjectManager_Tests : AbpIntegratedTest _books; private readonly IMapperAccessor _mapperAccessor; - private readonly Dictionary _testTranslations = new() + private readonly FrozenDictionary _testTranslations = new Dictionary() { ["ar"] = "C# التعمق في", ["zh-Hans"] = "深入理解C#", ["en"] = "C# in Depth" - }; + }.ToFrozenDictionary(); public MultiLingualObjectManager_Tests() { diff --git a/modules/docs/app/VoloDocs.Web/Pages/Error.cshtml.cs b/modules/docs/app/VoloDocs.Web/Pages/Error.cshtml.cs index 5e5c4f639b..88ed671381 100644 --- a/modules/docs/app/VoloDocs.Web/Pages/Error.cshtml.cs +++ b/modules/docs/app/VoloDocs.Web/Pages/Error.cshtml.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Frozen; using System.Collections.Generic; using System.Net; using Microsoft.AspNetCore.Diagnostics; @@ -48,7 +49,7 @@ namespace VoloDocs.Web.Pages #region Error Messages /*For more ASCII arts http://patorjk.com/software/taag/#p=display&h=0&f=Big&t=400*/ - private readonly Dictionary _errorMessages = new Dictionary + private readonly FrozenDictionary _errorMessages = new Dictionary { { 400, @" @@ -131,7 +132,7 @@ Ooops! Our server is experiencing a mild case of the hiccups." Looks like we're having some server issues." } - }; + }.ToFrozenDictionary(); #endregion } } \ No newline at end of file From 9db10bbd578390b459a3c6557bdc4b47e1d0db80 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Tue, 9 Dec 2025 08:21:21 +0100 Subject: [PATCH 03/17] Cleaning --- ...UiObjectExtensionPropertyInfoExtensions.cs | 3 +- ...UiObjectExtensionPropertyInfoExtensions.cs | 6 +- ...PostConfigureAbpRabbitMqEventBusOptions.cs | 32 ++-- .../Generators/ProxyScriptingJsFuncHelper.cs | 3 +- .../MultiLingualObjectManager_Tests.cs | 166 +++++++++--------- 5 files changed, 107 insertions(+), 103 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/ObjectExtending/MvcUiObjectExtensionPropertyInfoExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/ObjectExtending/MvcUiObjectExtensionPropertyInfoExtensions.cs index ae5d30a2ac..49ff15f9f5 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/ObjectExtending/MvcUiObjectExtensionPropertyInfoExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/ObjectExtending/MvcUiObjectExtensionPropertyInfoExtensions.cs @@ -9,7 +9,8 @@ namespace Volo.Abp.ObjectExtending; public static class MvcUiObjectExtensionPropertyInfoExtensions { - private static readonly FrozenSet NumberTypes = new HashSet { + private static readonly FrozenSet NumberTypes = new HashSet + { typeof(int), typeof(long), typeof(byte), diff --git a/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiObjectExtensionPropertyInfoExtensions.cs b/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiObjectExtensionPropertyInfoExtensions.cs index d535c87c00..5794021488 100644 --- a/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiObjectExtensionPropertyInfoExtensions.cs +++ b/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiObjectExtensionPropertyInfoExtensions.cs @@ -12,7 +12,8 @@ namespace Volo.Abp.BlazoriseUI; public static class BlazoriseUiObjectExtensionPropertyInfoExtensions { - private static readonly FrozenSet NumberTypes = new HashSet { + private static readonly FrozenSet NumberTypes = new HashSet + { typeof(int), typeof(long), typeof(byte), @@ -39,7 +40,8 @@ public static class BlazoriseUiObjectExtensionPropertyInfoExtensions typeof(decimal?) }.ToFrozenSet(); - private static readonly FrozenSet TextEditSupportedAttributeTypes = new HashSet { + private static readonly FrozenSet TextEditSupportedAttributeTypes = new HashSet + { typeof(EmailAddressAttribute), typeof(UrlAttribute), typeof(PhoneAttribute) diff --git a/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/PostConfigureAbpRabbitMqEventBusOptions.cs b/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/PostConfigureAbpRabbitMqEventBusOptions.cs index 2c400e01da..76a74e2b3e 100644 --- a/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/PostConfigureAbpRabbitMqEventBusOptions.cs +++ b/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/PostConfigureAbpRabbitMqEventBusOptions.cs @@ -6,23 +6,23 @@ namespace Volo.Abp.EventBus.RabbitMq; public class PostConfigureAbpRabbitMqEventBusOptions : IPostConfigureOptions { - private readonly FrozenSet _uint64QueueArguments = new HashSet() - { - "x-delivery-limit", - "x-expires", - "x-message-ttl", - "x-max-length", - "x-max-length-bytes", - "x-quorum-initial-group-size", - "x-quorum-target-group-size", - "x-stream-filter-size-bytes", - "x-stream-max-segment-size-bytes", - }.ToFrozenSet(); + private readonly FrozenSet _uint64QueueArguments = new HashSet + { + "x-delivery-limit", + "x-expires", + "x-message-ttl", + "x-max-length", + "x-max-length-bytes", + "x-quorum-initial-group-size", + "x-quorum-target-group-size", + "x-stream-filter-size-bytes", + "x-stream-max-segment-size-bytes", + }.ToFrozenSet(); - private readonly FrozenSet _boolQueueArguments = new HashSet() - { - "x-single-active-consumer" - }.ToFrozenSet(); + private readonly FrozenSet _boolQueueArguments = new HashSet + { + "x-single-active-consumer" + }.ToFrozenSet(); public virtual void PostConfigure(string? name, AbpRabbitMqEventBusOptions options) { diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs index 21547bd05d..ebc296e697 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/ProxyScriptingJsFuncHelper.cs @@ -11,7 +11,8 @@ internal static class ProxyScriptingJsFuncHelper { private const string ValidJsVariableNameChars = "abcdefghijklmnopqrstuxwvyzABCDEFGHIJKLMNOPQRSTUXWVYZ0123456789_"; - private static readonly FrozenSet ReservedWords = new HashSet { + private static readonly FrozenSet ReservedWords = new HashSet + { "abstract", "else", "instanceof", diff --git a/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs b/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs index 6b18822df3..bb527d2fcd 100644 --- a/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs +++ b/framework/test/Volo.Abp.MultiLingualObjects.Tests/Volo/Abp/MultiLingualObjects/MultiLingualObjectManager_Tests.cs @@ -1,38 +1,38 @@ -using System; -using System.Collections.Frozen; -using System.Collections.Generic; +using System; +using System.Collections.Frozen; +using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; -using Shouldly; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; using Volo.Abp.AutoMapper; -using Volo.Abp.Localization; -using Volo.Abp.MultiLingualObjects.TestObjects; -using Volo.Abp.Testing; -using Xunit; - -namespace Volo.Abp.MultiLingualObjects; - -public class MultiLingualObjectManager_Tests : AbpIntegratedTest -{ - private readonly IMultiLingualObjectManager _multiLingualObjectManager; - private readonly MultiLingualBook _book; - private readonly List _books; - private readonly IMapperAccessor _mapperAccessor; - private readonly FrozenDictionary _testTranslations = new Dictionary() - { - ["ar"] = "C# التعمق في", - ["zh-Hans"] = "深入理解C#", - ["en"] = "C# in Depth" - }.ToFrozenDictionary(); +using Volo.Abp.Localization; +using Volo.Abp.MultiLingualObjects.TestObjects; +using Volo.Abp.Testing; +using Xunit; + +namespace Volo.Abp.MultiLingualObjects; + +public class MultiLingualObjectManager_Tests : AbpIntegratedTest +{ + private readonly IMultiLingualObjectManager _multiLingualObjectManager; + private readonly MultiLingualBook _book; + private readonly List _books; + private readonly IMapperAccessor _mapperAccessor; + private readonly FrozenDictionary _testTranslations = new Dictionary + { + ["ar"] = "C# التعمق في", + ["zh-Hans"] = "深入理解C#", + ["en"] = "C# in Depth" + }.ToFrozenDictionary(); - public MultiLingualObjectManager_Tests() - { + public MultiLingualObjectManager_Tests() + { _multiLingualObjectManager = ServiceProvider.GetRequiredService(); //Single Lookup - _book = GetTestBook("en", "zh-Hans"); - //Bulk lookup + _book = GetTestBook("en", "zh-Hans"); + //Bulk lookup _books = new List { //has no translations @@ -46,14 +46,14 @@ public class MultiLingualObjectManager_Tests : AbpIntegratedTest(); + _mapperAccessor = ServiceProvider.GetRequiredService(); } MultiLingualBook GetTestBook(params string[] included) { - var id = Guid.NewGuid(); - //Single book - var res = new MultiLingualBook(id, 100); - + var id = Guid.NewGuid(); + //Single book + var res = new MultiLingualBook(id, 100); + foreach (var language in included) { res.Translations.Add(new MultiLingualBookTranslation @@ -66,45 +66,45 @@ public class MultiLingualObjectManager_Tests : AbpIntegratedTest(_book); - translation.ShouldNotBeNull(); - translation.Name.ShouldBe(_testTranslations["en"]); - } - } - - [Fact] - public async Task GetTranslationFromListAsync() - { - using (CultureHelper.Use("en-us")) - { - var translation = await _multiLingualObjectManager.GetTranslationAsync(_book.Translations); - translation.ShouldNotBeNull(); - translation.Name.ShouldBe(_testTranslations["en"]); - } - } - - [Fact] - public async Task Should_Get_Specified_Language() - { - using (CultureHelper.Use("zh-Hans")) - { - var translation = await _multiLingualObjectManager.GetTranslationAsync(_book, culture: "en"); - translation.ShouldNotBeNull(); - translation.Name.ShouldBe(_testTranslations["en"]); - } + [Fact] + public async Task GetTranslationAsync() + { + using (CultureHelper.Use("en-us")) + { + var translation = await _multiLingualObjectManager.GetTranslationAsync(_book); + translation.ShouldNotBeNull(); + translation.Name.ShouldBe(_testTranslations["en"]); + } + } + + [Fact] + public async Task GetTranslationFromListAsync() + { + using (CultureHelper.Use("en-us")) + { + var translation = await _multiLingualObjectManager.GetTranslationAsync(_book.Translations); + translation.ShouldNotBeNull(); + translation.Name.ShouldBe(_testTranslations["en"]); + } + } + + [Fact] + public async Task Should_Get_Specified_Language() + { + using (CultureHelper.Use("zh-Hans")) + { + var translation = await _multiLingualObjectManager.GetTranslationAsync(_book, culture: "en"); + translation.ShouldNotBeNull(); + translation.Name.ShouldBe(_testTranslations["en"]); + } } - [Fact] - public async Task GetBulkTranslationsAsync() - { - using (CultureHelper.Use("en-us")) - { + [Fact] + public async Task GetBulkTranslationsAsync() + { + using (CultureHelper.Use("en-us")) + { var translations = await _multiLingualObjectManager.GetBulkTranslationsAsync(_books); foreach (var (entity, translation) in translations) { @@ -118,26 +118,26 @@ public class MultiLingualObjectManager_Tests : AbpIntegratedTest x.Translations)); foreach (var translation in translations) { translation?.Name.ShouldBe(_testTranslations["en"]); } - } - } - - [Fact] + } + } + + [Fact] public async Task TestBulkMapping() { - using (CultureHelper.Use("en-us")) + using (CultureHelper.Use("en-us")) { var translations = await _multiLingualObjectManager.GetBulkTranslationsAsync(_books); var translationsDict = translations.ToDictionary(x => x.entity.Id, x => x.translation); @@ -153,5 +153,5 @@ public class MultiLingualObjectManager_Tests : AbpIntegratedTest x.Language == "en")?.Name, m.Name); } } - } -} + } +} From 12f4797179a927e906f9160d57e39e4d8d2b47a7 Mon Sep 17 00:00:00 2001 From: selman koc <64414348+skoc10@users.noreply.github.com> Date: Tue, 30 Dec 2025 11:12:31 +0300 Subject: [PATCH 04/17] Update version for nightly build 10.2.0preview --- common.props | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common.props b/common.props index 42e230791c..2b274e5d75 100644 --- a/common.props +++ b/common.props @@ -1,8 +1,8 @@ latest - 10.1.0-preview - 5.1.0-preview + 10.2.0-preview + 5.2.0-preview $(NoWarn);CS1591;CS0436 https://abp.io/assets/abp_nupkg.png https://abp.io/ From f253f9945e4be1b1c076aaefd5a5cbfb9fa2ab06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SAL=C4=B0H=20=C3=96ZKARA?= Date: Tue, 30 Dec 2025 15:19:43 +0300 Subject: [PATCH 05/17] Improve entity history handling for shared types and JSON Enhanced EntityHistoryHelper to correctly handle entities with shared CLR types and properties mapped to JSON. Added logic to determine property type from entry values when the type is object, and improved property change tracking for JSON-mapped navigation properties. --- .../EntityHistory/EntityHistoryHelper.cs | 55 ++++++++++++++++--- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs index f87a05e4a9..c23eee6450 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Reflection; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; +using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; @@ -108,12 +109,17 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency } var entityType = entity.GetType(); + var entityFullName = entityType.FullName!; + if (entityEntry.Metadata.HasSharedClrType) + { + entityFullName = entityEntry.Metadata.Name; + } var entityChange = new EntityChangeInfo { ChangeType = changeType, EntityEntry = entityEntry, EntityId = entityId, - EntityTypeFullName = entityType.FullName, + EntityTypeFullName = entityFullName, PropertyChanges = GetPropertyChanges(entityEntry), EntityTenantId = GetTenantId(entity) }; @@ -184,12 +190,14 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency var propertyEntry = entityEntry.Property(property.Name); if (ShouldSavePropertyHistory(propertyEntry, isCreated || isDeleted) && !IsSoftDeleted(entityEntry)) { + var propertyType = DeterminePropertyTypeFromEntry(property, propertyEntry); + propertyChanges.Add(new EntityPropertyChangeInfo { NewValue = isDeleted ? null : JsonSerializer.Serialize(propertyEntry.CurrentValue!).TruncateWithPostfix(EntityPropertyChangeInfo.MaxValueLength), OriginalValue = isCreated ? null : JsonSerializer.Serialize(propertyEntry.OriginalValue!).TruncateWithPostfix(EntityPropertyChangeInfo.MaxValueLength), PropertyName = property.Name, - PropertyTypeFullName = property.ClrType.GetFirstGenericArgumentIfNullable().FullName! + PropertyTypeFullName = propertyType.FullName! }); } } @@ -208,14 +216,22 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency if (AbpEfCoreNavigationHelper.IsNavigationEntryModified(entityEntry, index)) { var abpNavigationEntry = AbpEfCoreNavigationHelper.GetNavigationEntry(entityEntry, index); - var isCollection = navigationEntry.Metadata.IsCollection; - propertyChanges.Add(new EntityPropertyChangeInfo + if (navigationEntry.Metadata.TargetEntityType.IsMappedToJson() && navigationEntry is ReferenceEntry referenceEntry && referenceEntry.TargetEntry != null) + { + var jsonPropertyChanges = GetPropertyChanges(referenceEntry.TargetEntry); + propertyChanges.AddRange(jsonPropertyChanges); + } + else { - PropertyName = navigationEntry.Metadata.Name, - PropertyTypeFullName = navigationEntry.Metadata.ClrType.GetFirstGenericArgumentIfNullable().FullName!, - OriginalValue = GetNavigationPropertyValue(abpNavigationEntry?.OriginalValue, isCollection), - NewValue = GetNavigationPropertyValue(abpNavigationEntry?.CurrentValue, isCollection) - }); + var isCollection = navigationEntry.Metadata.IsCollection; + propertyChanges.Add(new EntityPropertyChangeInfo + { + PropertyName = navigationEntry.Metadata.Name, + PropertyTypeFullName = navigationEntry.Metadata.ClrType.GetFirstGenericArgumentIfNullable().FullName!, + OriginalValue = GetNavigationPropertyValue(abpNavigationEntry?.OriginalValue, isCollection), + NewValue = GetNavigationPropertyValue(abpNavigationEntry?.CurrentValue, isCollection) + }); + } } } } @@ -223,6 +239,27 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency return propertyChanges; } + protected virtual Type DeterminePropertyTypeFromEntry(IProperty property, PropertyEntry propertyEntry) + { + var propertyType = property.ClrType.GetFirstGenericArgumentIfNullable(); + + if (propertyType != typeof(object)) + { + return propertyType; + } + + if (propertyEntry.CurrentValue != null) + { + propertyType = propertyEntry.CurrentValue.GetType().GetFirstGenericArgumentIfNullable(); + } + else if (propertyEntry.OriginalValue != null) + { + propertyType = propertyEntry.OriginalValue.GetType().GetFirstGenericArgumentIfNullable(); + } + + return propertyType; + } + protected virtual string? GetNavigationPropertyValue(object? entity, bool isCollection) { switch (entity) From bdd0901dbad4618476f13642117a277c31317788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SAL=C4=B0H=20=C3=96ZKARA?= Date: Tue, 30 Dec 2025 15:41:38 +0300 Subject: [PATCH 06/17] Update framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../EntityHistory/EntityHistoryHelper.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs index c23eee6450..7554c1d281 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs @@ -239,6 +239,18 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency return propertyChanges; } + /// + /// Determines the CLR type of a property based on its EF Core metadata and the values in the given . + /// + /// The EF Core property metadata that provides the declared CLR type. + /// The property entry that contains the current and original values for the property. + /// + /// The most specific CLR type inferred for the property. This is normally the property's declared CLR type (with + /// nullable wrappers removed). If the declared type is , the type is inferred from the + /// runtime type of or, if that is null, from + /// . If both values are null, the declared CLR type + /// (which may remain ) is returned. + /// protected virtual Type DeterminePropertyTypeFromEntry(IProperty property, PropertyEntry propertyEntry) { var propertyType = property.ClrType.GetFirstGenericArgumentIfNullable(); From d041aeb1fa2fc1b5dd231d32ab7d5ecd0c0e77d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SAL=C4=B0H=20=C3=96ZKARA?= Date: Tue, 30 Dec 2025 15:58:32 +0300 Subject: [PATCH 07/17] Improve JSON navigation property change tracking Refactored EntityHistoryHelper to better handle property changes for navigation properties mapped to JSON. Now, nested property changes are prefixed with the navigation property name, and duplicate property names are handled to avoid conflicts. --- .../EntityHistory/EntityHistoryHelper.cs | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs index 7554c1d281..66e632dc8c 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs @@ -202,37 +202,47 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency } } - if (AbpEfCoreNavigationHelper != null) + if (AbpEfCoreNavigationHelper == null) { - foreach (var (navigationEntry, index) in entityEntry.Navigations.Select((value, i) => ( value, i ))) - { - var propertyInfo = navigationEntry.Metadata.PropertyInfo; - if (propertyInfo != null && - propertyInfo.IsDefined(typeof(DisableAuditingAttribute), true)) - { - continue; - } + return propertyChanges; + } - if (AbpEfCoreNavigationHelper.IsNavigationEntryModified(entityEntry, index)) + foreach (var (navigationEntry, index) in entityEntry.Navigations.Select((value, i) => ( value, i ))) + { + var propertyInfo = navigationEntry.Metadata.PropertyInfo; + if (propertyInfo != null && + propertyInfo.IsDefined(typeof(DisableAuditingAttribute), true)) + { + continue; + } + + if (navigationEntry.Metadata.TargetEntityType.IsMappedToJson() && navigationEntry is ReferenceEntry referenceEntry && referenceEntry.TargetEntry != null) + { + foreach (var propertyChange in GetPropertyChanges(referenceEntry.TargetEntry)) { - var abpNavigationEntry = AbpEfCoreNavigationHelper.GetNavigationEntry(entityEntry, index); - if (navigationEntry.Metadata.TargetEntityType.IsMappedToJson() && navigationEntry is ReferenceEntry referenceEntry && referenceEntry.TargetEntry != null) - { - var jsonPropertyChanges = GetPropertyChanges(referenceEntry.TargetEntry); - propertyChanges.AddRange(jsonPropertyChanges); - } - else + if (propertyChanges.Any(pc => pc.PropertyName == propertyChange.PropertyName)) { - var isCollection = navigationEntry.Metadata.IsCollection; - propertyChanges.Add(new EntityPropertyChangeInfo - { - PropertyName = navigationEntry.Metadata.Name, - PropertyTypeFullName = navigationEntry.Metadata.ClrType.GetFirstGenericArgumentIfNullable().FullName!, - OriginalValue = GetNavigationPropertyValue(abpNavigationEntry?.OriginalValue, isCollection), - NewValue = GetNavigationPropertyValue(abpNavigationEntry?.CurrentValue, isCollection) - }); + propertyChange.PropertyName = $"{referenceEntry.Metadata.Name}.{propertyChange.PropertyName}"; } + + propertyChanges.Add(propertyChange); } + + continue; + } + + if (AbpEfCoreNavigationHelper.IsNavigationEntryModified(entityEntry, index)) + { + var abpNavigationEntry = AbpEfCoreNavigationHelper.GetNavigationEntry(entityEntry, index); + + var isCollection = navigationEntry.Metadata.IsCollection; + propertyChanges.Add(new EntityPropertyChangeInfo + { + PropertyName = navigationEntry.Metadata.Name, + PropertyTypeFullName = navigationEntry.Metadata.ClrType.GetFirstGenericArgumentIfNullable().FullName!, + OriginalValue = GetNavigationPropertyValue(abpNavigationEntry?.OriginalValue, isCollection), + NewValue = GetNavigationPropertyValue(abpNavigationEntry?.CurrentValue, isCollection) + }); } } From fb5dbaee99ccd1b7a81c7952559e70102ee9cc26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SAL=C4=B0H=20=C3=96ZKARA?= Date: Tue, 30 Dec 2025 19:02:53 +0300 Subject: [PATCH 08/17] Fix entity type name for shared CLR types Update EntityHistoryHelper to set the entity type name only for shared CLR types that are not owned entities. This prevents incorrect entity name assignment for owned types. --- .../EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs index 66e632dc8c..2fcdb35480 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs @@ -110,7 +110,7 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency var entityType = entity.GetType(); var entityFullName = entityType.FullName!; - if (entityEntry.Metadata.HasSharedClrType) + if (entityEntry.Metadata.HasSharedClrType && !entityEntry.Metadata.IsOwned()) { entityFullName = entityEntry.Metadata.Name; } From 4d00ee9365dd8fd09716caaf86c6371c660ce61b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SAL=C4=B0H=20=C3=96ZKARA?= Date: Wed, 31 Dec 2025 11:40:14 +0300 Subject: [PATCH 09/17] Add support and tests for entity history with JSON properties Introduces AppEntityWithJsonProperty and related DbSet to test contexts, configures model to handle owned JSON properties, and adds tests to verify entity history tracking for nested JSON property changes and shared entities. Refactors EntityHistoryHelper to improve navigation property change handling. --- .../EntityHistory/EntityHistoryHelper.cs | 10 +- .../AbpEntityFrameworkCoreTestModule.cs | 7 + .../Auditing/EntityHistoryHelper_Tests.cs | 130 ++++++++++++++++++ .../TestMigrationsDbContext.cs | 23 ++++ .../EntityFrameworkCore/TestAppDbContext.cs | 25 +++- .../Domain/AppEntityWithJsonProperty.cs | 28 ++++ 6 files changed, 217 insertions(+), 6 deletions(-) create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs create mode 100644 framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs index 2fcdb35480..e659689da1 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs @@ -202,11 +202,6 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency } } - if (AbpEfCoreNavigationHelper == null) - { - return propertyChanges; - } - foreach (var (navigationEntry, index) in entityEntry.Navigations.Select((value, i) => ( value, i ))) { var propertyInfo = navigationEntry.Metadata.PropertyInfo; @@ -230,6 +225,11 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency continue; } + + if (AbpEfCoreNavigationHelper == null) + { + return propertyChanges; + } if (AbpEfCoreNavigationHelper.IsNavigationEntryModified(entityEntry, index)) { diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs index a7b5c1e602..e4183a5a19 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs @@ -5,6 +5,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Auditing; using Volo.Abp.Autofac; using Volo.Abp.Domain.Repositories; using Volo.Abp.EntityFrameworkCore.Domain; @@ -86,6 +87,12 @@ public class AbpEntityFrameworkCoreTestModule : AbpModule abpDbContextConfigurationContext.DbContextOptions.UseSqlite(sqliteConnection).AddAbpDbContextOptionsExtension(); }); }); + + Configure(options => + { + options.EntityHistorySelectors.Add(new NamedTypeSelector(nameof(AppEntityWithJsonProperty), type => type == typeof(AppEntityWithJsonProperty))); + options.EntityHistorySelectors.Add(new NamedTypeSelector(nameof(TestSharedEntity), type => type == typeof(TestSharedEntity))); + }); } public override void OnPreApplicationInitialization(ApplicationInitializationContext context) diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs new file mode 100644 index 0000000000..65873dd514 --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Volo.Abp.Auditing; +using Volo.Abp.Data; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.EntityFrameworkCore.EntityHistory; +using Volo.Abp.TestApp.Domain; +using Volo.Abp.TestApp.EntityFrameworkCore; +using Volo.Abp.Uow; +using Xunit; + +namespace Volo.Abp.EntityFrameworkCore.Auditing; + +public class EntityHistoryHelper_Tests : EntityFrameworkCoreTestBase +{ + private readonly IEntityHistoryHelper _entityHistoryHelper; + private readonly IRepository _appEntityWithJsonRepository; + private readonly IRepository _testSharedEntityRepository; + private readonly IUnitOfWorkManager _unitOfWorkManager; + + public EntityHistoryHelper_Tests() + { + _entityHistoryHelper = GetRequiredService(); + _appEntityWithJsonRepository = GetRequiredService>(); + _testSharedEntityRepository = GetRequiredService>(); + _unitOfWorkManager = GetRequiredService(); + } + + [Fact] + public async Task CreateChangeList_Should_Track_Nested_Json_Property_Changes_As_Separate_Property_Changes() + { + // Arrange & Act + EntityChangeInfo entityChange = null; + + await WithUnitOfWorkAsync(async () => + { + var entity = new AppEntityWithJsonProperty(Guid.NewGuid(), "Test Entity") + { + Data = new JsonPropertyObject() + { + { "Name", "String Name" }, + { "Value", "String Value"} + }, + Count = 10 + }; + + await _appEntityWithJsonRepository.InsertAsync(entity); + + var dbContext = await GetDbContextAsync(); + + var entries = dbContext.ChangeTracker.Entries().ToList(); + var entityChanges = _entityHistoryHelper.CreateChangeList(entries); + + entityChange = entityChanges.FirstOrDefault(x => x.EntityTypeFullName.Contains(nameof(AppEntityWithJsonProperty))); + }); + + // Assert + entityChange.ShouldNotBeNull(); + var dataPropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == nameof(AppEntityWithJsonProperty.Data)); + dataPropertyChange.ShouldBeNull(); + var jsonNamePropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == nameof(AppEntityWithJsonProperty.Data) + "." + "Name"); + jsonNamePropertyChange.ShouldNotBeNull(); + jsonNamePropertyChange.PropertyTypeFullName.ShouldBe(typeof(string).FullName); + jsonNamePropertyChange.NewValue.ShouldBe("\"String Name\""); + + var jsonValuePropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == "Value"); + jsonValuePropertyChange.ShouldNotBeNull(); + jsonValuePropertyChange.PropertyTypeFullName.ShouldBe(typeof(string).FullName); + jsonValuePropertyChange.NewValue.ShouldBe("\"String Value\""); + } + + [Fact] + public async Task CreateChangeList_Should_Track_Shared_Entities_With_Their_Respective_Entity_Names() + { + // Arrange & Act + List entityChanges = null; + + await WithUnitOfWorkAsync(async () => + { + var entity = new TestSharedEntity(Guid.NewGuid()) + { + TenantId = null, + IsDeleted = false, + Name = "Test Person1", + Age = 10, + Birthday = DateTime.Now + }.SetProperty("testProperty", "Test Value1"); + + _testSharedEntityRepository.SetEntityName("TestSharedEntity1"); + await _testSharedEntityRepository.InsertAsync(entity); + + var entity2 = new TestSharedEntity(Guid.NewGuid()) + { + TenantId = null, + IsDeleted = false, + Name = "Test Person2", + Age = 20, + Birthday = DateTime.Now + }.SetProperty("testProperty", "Test Value2"); + + _testSharedEntityRepository.SetEntityName("TestSharedEntity2"); + await _testSharedEntityRepository.InsertAsync(entity2); + + var dbContext = await GetDbContextAsync(); + + var entries = dbContext.ChangeTracker.Entries().ToList(); + entityChanges = _entityHistoryHelper.CreateChangeList(entries); + }); + + entityChanges.ShouldContain(x => x.EntityTypeFullName == "TestSharedEntity1"); + entityChanges.ShouldContain(x => x.EntityTypeFullName == "TestSharedEntity2"); + } + + private async Task GetDbContextAsync() + { + var uow = _unitOfWorkManager.Current; + if (uow == null) + { + throw new InvalidOperationException("No active unit of work found"); + } + + var dbContextProvider = uow.ServiceProvider.GetRequiredService>(); + return await dbContextProvider.GetDbContextAsync(); + } +} + diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs index 4c6efa5a02..6e102706fc 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs @@ -37,6 +37,8 @@ public class TestMigrationsDbContext : AbpDbContext public DbSet TestSharedEntity => Set("TestSharedEntity1"); public DbSet TestSharedEntity2 => Set("TestSharedEntity2"); + + public DbSet EntitiesWithObjectProperty { get; set; } public TestMigrationsDbContext(DbContextOptions options) : base(options) @@ -140,5 +142,26 @@ public class TestMigrationsDbContext : AbpDbContext { b.ConfigureByConvention(); }); + + modelBuilder.Entity(b => + { + b.ConfigureByConvention(); + b.OwnsOne(x => x.Data, b2 => + { + b2.ToJson(); + + b2.Property("Name") + .HasConversion( + v => v.ToString(), + v => v + ); + + b2.Property("Value") + .HasConversion( + v => v.ToString(), + v => v + ); + }); + }); } } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs index a8e68dfca8..b0b0a55c03 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs @@ -40,9 +40,11 @@ public class TestAppDbContext : AbpDbContext, IThirdDbContext, public DbSet Blogs { get; set; } public DbSet BlogPosts { get; set; } - + public DbSet TestSharedEntity => Set("TestSharedEntity1"); public DbSet TestSharedEntity2 => Set("TestSharedEntity2"); + + public DbSet EntitiesWithObjectProperty { get; set; } public TestAppDbContext(DbContextOptions options) : base(options) @@ -166,6 +168,27 @@ public class TestAppDbContext : AbpDbContext, IThirdDbContext, b.ConfigureByConvention(); }); + modelBuilder.Entity(b => + { + b.ConfigureByConvention(); + b.OwnsOne(x => x.Data, b2 => + { + b2.ToJson(); + + b2.Property("Name") + .HasConversion( + v => v.ToString(), + v => v + ); + + b2.Property("Value") + .HasConversion( + v => v.ToString(), + v => v + ); + }); + }); + modelBuilder.TryConfigureObjectExtensions(); } } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs new file mode 100644 index 0000000000..ab830e3e85 --- /dev/null +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using Volo.Abp.Domain.Entities.Auditing; + +namespace Volo.Abp.TestApp.Domain; + +public class AppEntityWithJsonProperty : FullAuditedAggregateRoot +{ + public string Name { get; set; } + + public JsonPropertyObject Data { get; set; } + + public int Count { get; set; } + + public AppEntityWithJsonProperty() + { + } + + public AppEntityWithJsonProperty(Guid id, string name) : base(id) + { + Name = name; + } +} + +public class JsonPropertyObject : Dictionary +{ +} + From 96a7303fc6a73eea9eb81f1e01040c68ad5c628d Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 31 Dec 2025 18:17:36 +0800 Subject: [PATCH 10/17] Revert "Add support and tests for entity history with JSON properties" This reverts commit 4d00ee9365dd8fd09716caaf86c6371c660ce61b. --- .../EntityHistory/EntityHistoryHelper.cs | 10 +- .../AbpEntityFrameworkCoreTestModule.cs | 7 - .../Auditing/EntityHistoryHelper_Tests.cs | 130 ------------------ .../TestMigrationsDbContext.cs | 23 ---- .../EntityFrameworkCore/TestAppDbContext.cs | 25 +--- .../Domain/AppEntityWithJsonProperty.cs | 28 ---- 6 files changed, 6 insertions(+), 217 deletions(-) delete mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs delete mode 100644 framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs index e659689da1..2fcdb35480 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs @@ -202,6 +202,11 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency } } + if (AbpEfCoreNavigationHelper == null) + { + return propertyChanges; + } + foreach (var (navigationEntry, index) in entityEntry.Navigations.Select((value, i) => ( value, i ))) { var propertyInfo = navigationEntry.Metadata.PropertyInfo; @@ -225,11 +230,6 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency continue; } - - if (AbpEfCoreNavigationHelper == null) - { - return propertyChanges; - } if (AbpEfCoreNavigationHelper.IsNavigationEntryModified(entityEntry, index)) { diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs index e4183a5a19..a7b5c1e602 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs @@ -5,7 +5,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Auditing; using Volo.Abp.Autofac; using Volo.Abp.Domain.Repositories; using Volo.Abp.EntityFrameworkCore.Domain; @@ -87,12 +86,6 @@ public class AbpEntityFrameworkCoreTestModule : AbpModule abpDbContextConfigurationContext.DbContextOptions.UseSqlite(sqliteConnection).AddAbpDbContextOptionsExtension(); }); }); - - Configure(options => - { - options.EntityHistorySelectors.Add(new NamedTypeSelector(nameof(AppEntityWithJsonProperty), type => type == typeof(AppEntityWithJsonProperty))); - options.EntityHistorySelectors.Add(new NamedTypeSelector(nameof(TestSharedEntity), type => type == typeof(TestSharedEntity))); - }); } public override void OnPreApplicationInitialization(ApplicationInitializationContext context) diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs deleted file mode 100644 index 65873dd514..0000000000 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Auditing/EntityHistoryHelper_Tests.cs +++ /dev/null @@ -1,130 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; -using Shouldly; -using Volo.Abp.Auditing; -using Volo.Abp.Data; -using Volo.Abp.Domain.Repositories; -using Volo.Abp.EntityFrameworkCore.EntityHistory; -using Volo.Abp.TestApp.Domain; -using Volo.Abp.TestApp.EntityFrameworkCore; -using Volo.Abp.Uow; -using Xunit; - -namespace Volo.Abp.EntityFrameworkCore.Auditing; - -public class EntityHistoryHelper_Tests : EntityFrameworkCoreTestBase -{ - private readonly IEntityHistoryHelper _entityHistoryHelper; - private readonly IRepository _appEntityWithJsonRepository; - private readonly IRepository _testSharedEntityRepository; - private readonly IUnitOfWorkManager _unitOfWorkManager; - - public EntityHistoryHelper_Tests() - { - _entityHistoryHelper = GetRequiredService(); - _appEntityWithJsonRepository = GetRequiredService>(); - _testSharedEntityRepository = GetRequiredService>(); - _unitOfWorkManager = GetRequiredService(); - } - - [Fact] - public async Task CreateChangeList_Should_Track_Nested_Json_Property_Changes_As_Separate_Property_Changes() - { - // Arrange & Act - EntityChangeInfo entityChange = null; - - await WithUnitOfWorkAsync(async () => - { - var entity = new AppEntityWithJsonProperty(Guid.NewGuid(), "Test Entity") - { - Data = new JsonPropertyObject() - { - { "Name", "String Name" }, - { "Value", "String Value"} - }, - Count = 10 - }; - - await _appEntityWithJsonRepository.InsertAsync(entity); - - var dbContext = await GetDbContextAsync(); - - var entries = dbContext.ChangeTracker.Entries().ToList(); - var entityChanges = _entityHistoryHelper.CreateChangeList(entries); - - entityChange = entityChanges.FirstOrDefault(x => x.EntityTypeFullName.Contains(nameof(AppEntityWithJsonProperty))); - }); - - // Assert - entityChange.ShouldNotBeNull(); - var dataPropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == nameof(AppEntityWithJsonProperty.Data)); - dataPropertyChange.ShouldBeNull(); - var jsonNamePropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == nameof(AppEntityWithJsonProperty.Data) + "." + "Name"); - jsonNamePropertyChange.ShouldNotBeNull(); - jsonNamePropertyChange.PropertyTypeFullName.ShouldBe(typeof(string).FullName); - jsonNamePropertyChange.NewValue.ShouldBe("\"String Name\""); - - var jsonValuePropertyChange = entityChange.PropertyChanges.FirstOrDefault(x => x.PropertyName == "Value"); - jsonValuePropertyChange.ShouldNotBeNull(); - jsonValuePropertyChange.PropertyTypeFullName.ShouldBe(typeof(string).FullName); - jsonValuePropertyChange.NewValue.ShouldBe("\"String Value\""); - } - - [Fact] - public async Task CreateChangeList_Should_Track_Shared_Entities_With_Their_Respective_Entity_Names() - { - // Arrange & Act - List entityChanges = null; - - await WithUnitOfWorkAsync(async () => - { - var entity = new TestSharedEntity(Guid.NewGuid()) - { - TenantId = null, - IsDeleted = false, - Name = "Test Person1", - Age = 10, - Birthday = DateTime.Now - }.SetProperty("testProperty", "Test Value1"); - - _testSharedEntityRepository.SetEntityName("TestSharedEntity1"); - await _testSharedEntityRepository.InsertAsync(entity); - - var entity2 = new TestSharedEntity(Guid.NewGuid()) - { - TenantId = null, - IsDeleted = false, - Name = "Test Person2", - Age = 20, - Birthday = DateTime.Now - }.SetProperty("testProperty", "Test Value2"); - - _testSharedEntityRepository.SetEntityName("TestSharedEntity2"); - await _testSharedEntityRepository.InsertAsync(entity2); - - var dbContext = await GetDbContextAsync(); - - var entries = dbContext.ChangeTracker.Entries().ToList(); - entityChanges = _entityHistoryHelper.CreateChangeList(entries); - }); - - entityChanges.ShouldContain(x => x.EntityTypeFullName == "TestSharedEntity1"); - entityChanges.ShouldContain(x => x.EntityTypeFullName == "TestSharedEntity2"); - } - - private async Task GetDbContextAsync() - { - var uow = _unitOfWorkManager.Current; - if (uow == null) - { - throw new InvalidOperationException("No active unit of work found"); - } - - var dbContextProvider = uow.ServiceProvider.GetRequiredService>(); - return await dbContextProvider.GetDbContextAsync(); - } -} - diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs index 6e102706fc..4c6efa5a02 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs @@ -37,8 +37,6 @@ public class TestMigrationsDbContext : AbpDbContext public DbSet TestSharedEntity => Set("TestSharedEntity1"); public DbSet TestSharedEntity2 => Set("TestSharedEntity2"); - - public DbSet EntitiesWithObjectProperty { get; set; } public TestMigrationsDbContext(DbContextOptions options) : base(options) @@ -142,26 +140,5 @@ public class TestMigrationsDbContext : AbpDbContext { b.ConfigureByConvention(); }); - - modelBuilder.Entity(b => - { - b.ConfigureByConvention(); - b.OwnsOne(x => x.Data, b2 => - { - b2.ToJson(); - - b2.Property("Name") - .HasConversion( - v => v.ToString(), - v => v - ); - - b2.Property("Value") - .HasConversion( - v => v.ToString(), - v => v - ); - }); - }); } } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs index b0b0a55c03..a8e68dfca8 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs @@ -40,11 +40,9 @@ public class TestAppDbContext : AbpDbContext, IThirdDbContext, public DbSet Blogs { get; set; } public DbSet BlogPosts { get; set; } - + public DbSet TestSharedEntity => Set("TestSharedEntity1"); public DbSet TestSharedEntity2 => Set("TestSharedEntity2"); - - public DbSet EntitiesWithObjectProperty { get; set; } public TestAppDbContext(DbContextOptions options) : base(options) @@ -168,27 +166,6 @@ public class TestAppDbContext : AbpDbContext, IThirdDbContext, b.ConfigureByConvention(); }); - modelBuilder.Entity(b => - { - b.ConfigureByConvention(); - b.OwnsOne(x => x.Data, b2 => - { - b2.ToJson(); - - b2.Property("Name") - .HasConversion( - v => v.ToString(), - v => v - ); - - b2.Property("Value") - .HasConversion( - v => v.ToString(), - v => v - ); - }); - }); - modelBuilder.TryConfigureObjectExtensions(); } } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs deleted file mode 100644 index ab830e3e85..0000000000 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/AppEntityWithJsonProperty.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using Volo.Abp.Domain.Entities.Auditing; - -namespace Volo.Abp.TestApp.Domain; - -public class AppEntityWithJsonProperty : FullAuditedAggregateRoot -{ - public string Name { get; set; } - - public JsonPropertyObject Data { get; set; } - - public int Count { get; set; } - - public AppEntityWithJsonProperty() - { - } - - public AppEntityWithJsonProperty(Guid id, string name) : base(id) - { - Name = name; - } -} - -public class JsonPropertyObject : Dictionary -{ -} - From 6f57ec45680bef671f13d0416c789f8b8def6f65 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 31 Dec 2025 19:11:25 +0800 Subject: [PATCH 11/17] Add audit support for JSON property changes. --- .../EntityHistory/EntityHistoryHelper.cs | 17 ++-- .../Abp/Auditing/AbpAuditingTestModule.cs | 2 + .../App/Entities/AppEntityWithJsonProperty.cs | 27 +++++ .../AbpAuditingTestDbContext.cs | 21 ++++ .../Volo/Abp/Auditing/Auditing_Tests.cs | 98 +++++++++++++++++++ 5 files changed, 157 insertions(+), 8 deletions(-) create mode 100644 framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithJsonProperty.cs diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs index 2fcdb35480..75e82976b4 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EntityHistory/EntityHistoryHelper.cs @@ -187,6 +187,11 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency foreach (var property in properties) { + if (entityEntry.Metadata.IsMappedToJson() && property.GetJsonPropertyName() == null) + { + continue; + } + var propertyEntry = entityEntry.Property(property.Name); if (ShouldSavePropertyHistory(propertyEntry, isCreated || isDeleted) && !IsSoftDeleted(entityEntry)) { @@ -215,26 +220,22 @@ public class EntityHistoryHelper : IEntityHistoryHelper, ITransientDependency { continue; } - + if (navigationEntry.Metadata.TargetEntityType.IsMappedToJson() && navigationEntry is ReferenceEntry referenceEntry && referenceEntry.TargetEntry != null) { foreach (var propertyChange in GetPropertyChanges(referenceEntry.TargetEntry)) { - if (propertyChanges.Any(pc => pc.PropertyName == propertyChange.PropertyName)) - { - propertyChange.PropertyName = $"{referenceEntry.Metadata.Name}.{propertyChange.PropertyName}"; - } - + propertyChange.PropertyName = $"{referenceEntry.Metadata.Name}.{propertyChange.PropertyName}"; propertyChanges.Add(propertyChange); } - + continue; } if (AbpEfCoreNavigationHelper.IsNavigationEntryModified(entityEntry, index)) { var abpNavigationEntry = AbpEfCoreNavigationHelper.GetNavigationEntry(entityEntry, index); - + var isCollection = navigationEntry.Metadata.IsCollection; propertyChanges.Add(new EntityPropertyChangeInfo { diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/AbpAuditingTestModule.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/AbpAuditingTestModule.cs index 4e14dbc262..800b73544d 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/AbpAuditingTestModule.cs +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/AbpAuditingTestModule.cs @@ -59,6 +59,8 @@ public class AbpAuditingTestModule : AbpModule "AppEntityWithValueObject", type => type == typeof(AppEntityWithValueObject) || type == typeof(AppEntityWithValueObjectAddress)) ); + + options.EntityHistorySelectors.Add(new NamedTypeSelector(nameof(AppEntityWithJsonProperty), type => type == typeof(AppEntityWithJsonProperty))); }); context.Services.AddType(); diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithJsonProperty.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithJsonProperty.cs new file mode 100644 index 0000000000..de07544629 --- /dev/null +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/Entities/AppEntityWithJsonProperty.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using Volo.Abp.Domain.Entities.Auditing; + +namespace Volo.Abp.Auditing.App.Entities; + +public class AppEntityWithJsonProperty : FullAuditedAggregateRoot +{ + public string Name { get; set; } + + public JsonPropertyObject Data { get; set; } + + public int Count { get; set; } + + public AppEntityWithJsonProperty() + { + } + + public AppEntityWithJsonProperty(Guid id, string name) : base(id) + { + Name = name; + } +} + +public class JsonPropertyObject : Dictionary +{ +} diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/EntityFrameworkCore/AbpAuditingTestDbContext.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/EntityFrameworkCore/AbpAuditingTestDbContext.cs index 91698ea5ec..e8950880d7 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/EntityFrameworkCore/AbpAuditingTestDbContext.cs +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/App/EntityFrameworkCore/AbpAuditingTestDbContext.cs @@ -30,6 +30,7 @@ public class AbpAuditingTestDbContext : AbpDbContext public DbSet AppEntityWithNavigations { get; set; } public DbSet AppEntityWithNavigationChildOneToMany { get; set; } public DbSet AppEntityWithNavigationsAndDisableAuditing { get; set; } + public DbSet EntitiesWithObjectProperty { get; set; } public AbpAuditingTestDbContext(DbContextOptions options) : base(options) @@ -56,5 +57,25 @@ public class AbpAuditingTestDbContext : AbpDbContext b.HasMany(x => x.ManyToMany).WithMany(x => x.ManyToMany).UsingEntity(); }); + modelBuilder.Entity(b => + { + b.ConfigureByConvention(); + b.OwnsOne(x => x.Data, b2 => + { + b2.ToJson(); + + b2.Property("Name") + .HasConversion( + v => v.ToString(), + v => v + ); + + b2.Property("Value") + .HasConversion( + v => v.ToString(), + v => v + ); + }); + }); } } diff --git a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs index a88cf70292..637b9b4d97 100644 --- a/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs +++ b/framework/test/Volo.Abp.Auditing.Tests/Volo/Abp/Auditing/Auditing_Tests.cs @@ -720,6 +720,104 @@ public class Auditing_Tests : AbpAuditingTestBase x.EntityChanges[1].PropertyChanges[0].PropertyName == nameof(AppEntityWithNavigationChildManyToMany.ManyToMany) && x.EntityChanges[1].PropertyChanges[0].PropertyTypeFullName == typeof(List).FullName)); +#pragma warning restore 4014 + } + + [Fact] + public async Task Should_Write_AuditLog_For_Json_Property_Changes() + { + var entityId = Guid.NewGuid(); + var repository = ServiceProvider.GetRequiredService>(); + + using (var scope = _auditingManager.BeginScope()) + { + using (var uow = _unitOfWorkManager.Begin()) + { + var entity = new AppEntityWithJsonProperty(entityId, "Test Entity") + { + Data = new JsonPropertyObject() + { + { "Name", "String Name" }, + { "Value", "String Value"} + }, + Count = 10 + }; + + await repository.InsertAsync(entity); + + await uow.CompleteAsync(); + await scope.SaveAsync(); + } + } + +#pragma warning disable 4014 + AuditingStore.Received().SaveAsync(Arg.Is(x => x.EntityChanges.Count == 1 && + x.EntityChanges[0].ChangeType == EntityChangeType.Created && + x.EntityChanges[0].EntityTypeFullName == typeof(AppEntityWithJsonProperty).FullName && + x.EntityChanges[0].PropertyChanges.Count == 4 && + + x.EntityChanges[0].PropertyChanges[0].OriginalValue == null && + x.EntityChanges[0].PropertyChanges[0].NewValue == "10" && + x.EntityChanges[0].PropertyChanges[0].PropertyName == nameof(AppEntityWithJsonProperty.Count) && + x.EntityChanges[0].PropertyChanges[0].PropertyTypeFullName == typeof(int).FullName && + + x.EntityChanges[0].PropertyChanges[1].OriginalValue == null && + x.EntityChanges[0].PropertyChanges[1].NewValue == "\"Test Entity\"" && + x.EntityChanges[0].PropertyChanges[1].PropertyName == nameof(AppEntityWithJsonProperty.Name) && + x.EntityChanges[0].PropertyChanges[1].PropertyTypeFullName == typeof(string).FullName && + + x.EntityChanges[0].PropertyChanges[2].OriginalValue == null && + x.EntityChanges[0].PropertyChanges[2].NewValue == "\"String Name\"" && + x.EntityChanges[0].PropertyChanges[2].PropertyName == "Data.Name" && + x.EntityChanges[0].PropertyChanges[2].PropertyTypeFullName == typeof(string).FullName && + + x.EntityChanges[0].PropertyChanges[3].OriginalValue == null && + x.EntityChanges[0].PropertyChanges[3].NewValue == "\"String Value\"" && + x.EntityChanges[0].PropertyChanges[3].PropertyName == "Data.Value" && + x.EntityChanges[0].PropertyChanges[3].PropertyTypeFullName == typeof(string).FullName)); + AuditingStore.ClearReceivedCalls(); +#pragma warning restore 4014 + + + using (var scope = _auditingManager.BeginScope()) + { + using (var uow = _unitOfWorkManager.Begin()) + { + var entity = await repository.GetAsync(entityId); + + entity.Name = "Updated Test Entity"; + + entity.Data["Name"] = "Updated String Name"; + entity.Data["Value"] = "Updated String Value"; + + await repository.UpdateAsync(entity); + + await uow.CompleteAsync(); + await scope.SaveAsync(); + } + } + +#pragma warning disable 4014 + AuditingStore.Received().SaveAsync(Arg.Is(x => x.EntityChanges.Count == 1 && + x.EntityChanges[0].ChangeType == EntityChangeType.Updated && + x.EntityChanges[0].EntityTypeFullName == typeof(AppEntityWithJsonProperty).FullName && + x.EntityChanges[0].PropertyChanges.Count == 3 && + + x.EntityChanges[0].PropertyChanges[0].OriginalValue == "\"Test Entity\"" && + x.EntityChanges[0].PropertyChanges[0].NewValue == "\"Updated Test Entity\"" && + x.EntityChanges[0].PropertyChanges[0].PropertyName == nameof(AppEntityWithJsonProperty.Name) && + x.EntityChanges[0].PropertyChanges[0].PropertyTypeFullName == typeof(string).FullName && + + x.EntityChanges[0].PropertyChanges[1].OriginalValue == "\"String Name\"" && + x.EntityChanges[0].PropertyChanges[1].NewValue == "\"Updated String Name\"" && + x.EntityChanges[0].PropertyChanges[1].PropertyName == "Data.Name" && + x.EntityChanges[0].PropertyChanges[1].PropertyTypeFullName == typeof(string).FullName && + + x.EntityChanges[0].PropertyChanges[2].OriginalValue == "\"String Value\"" && + x.EntityChanges[0].PropertyChanges[2].NewValue == "\"Updated String Value\"" && + x.EntityChanges[0].PropertyChanges[2].PropertyName == "Data.Value" && + x.EntityChanges[0].PropertyChanges[2].PropertyTypeFullName == typeof(string).FullName)); + AuditingStore.ClearReceivedCalls(); #pragma warning restore 4014 } } From 5d84359a97fc779084a03022ddb10b605800c73b Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 2 Jan 2026 16:03:25 +0800 Subject: [PATCH 12/17] Refactor token validation in CookieAuthenticationOptionsExtensions Resolve #24468 --- .../CookieAuthenticationOptionsExtensions.cs | 76 +------------------ .../CookieAuthenticationOptionsExtensions.cs | 36 ++++++--- 2 files changed, 30 insertions(+), 82 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Server/Microsoft/AspNetCore/Authentication/Cookies/CookieAuthenticationOptionsExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Components.Server/Microsoft/AspNetCore/Authentication/Cookies/CookieAuthenticationOptionsExtensions.cs index 0dd2c33cb8..2aead07ce7 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.Server/Microsoft/AspNetCore/Authentication/Cookies/CookieAuthenticationOptionsExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.Server/Microsoft/AspNetCore/Authentication/Cookies/CookieAuthenticationOptionsExtensions.cs @@ -1,86 +1,16 @@ using System; -using System.Threading.Tasks; -using Duende.IdentityModel.Client; -using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using Volo.Abp.Threading; namespace Microsoft.AspNetCore.Authentication.Cookies; public static class CookieAuthenticationOptionsExtensions { /// - /// Introspect access token on validating the principal. + /// Check the access_token is expired or inactive. /// - /// - /// - /// + [Obsolete("Use CheckTokenExpiration method instead.")] public static CookieAuthenticationOptions IntrospectAccessToken(this CookieAuthenticationOptions options, string oidcAuthenticationScheme = "oidc") { - options.Events.OnValidatePrincipal = async principalContext => - { - if (principalContext.Principal == null || principalContext.Principal.Identity == null || !principalContext.Principal.Identity.IsAuthenticated) - { - return; - } - - var logger = principalContext.HttpContext.RequestServices.GetRequiredService>(); - - var accessToken = principalContext.Properties.GetTokenValue("access_token"); - if (!accessToken.IsNullOrWhiteSpace()) - { - var openIdConnectOptions = await GetOpenIdConnectOptions(principalContext, oidcAuthenticationScheme); - var response = await openIdConnectOptions.Backchannel.IntrospectTokenAsync(new TokenIntrospectionRequest - { - Address = openIdConnectOptions.Configuration?.IntrospectionEndpoint ?? openIdConnectOptions.Authority!.EnsureEndsWith('/') + "connect/introspect", - ClientId = openIdConnectOptions.ClientId!, - ClientSecret = openIdConnectOptions.ClientSecret, - Token = accessToken - }); - - if (response.IsError) - { - logger.LogError(response.Error); - await SignOutAsync(principalContext); - return; - } - - if (!response.IsActive) - { - logger.LogError("The access_token is not active."); - await SignOutAsync(principalContext); - return; - } - - logger.LogInformation("The access_token is active."); - } - else - { - logger.LogError("The access_token is not found in the cookie properties, Please make sure SaveTokens of OpenIdConnectOptions is set as true."); - await SignOutAsync(principalContext); - } - }; - - return options; - } - - private async static Task GetOpenIdConnectOptions(CookieValidatePrincipalContext principalContext, string oidcAuthenticationScheme) - { - var openIdConnectOptions = principalContext.HttpContext.RequestServices.GetRequiredService>().Get(oidcAuthenticationScheme); - if (openIdConnectOptions.Configuration == null && openIdConnectOptions.ConfigurationManager != null) - { - var cancellationTokenProvider = principalContext.HttpContext.RequestServices.GetRequiredService(); - openIdConnectOptions.Configuration = await openIdConnectOptions.ConfigurationManager.GetConfigurationAsync(cancellationTokenProvider.Token); - } - - return openIdConnectOptions; - } - - private async static Task SignOutAsync(CookieValidatePrincipalContext principalContext) - { - principalContext.RejectPrincipal(); - await principalContext.HttpContext.SignOutAsync(principalContext.Scheme.Name); + return options.CheckTokenExpiration(oidcAuthenticationScheme, null, TimeSpan.FromMinutes(1)); } } diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs index c37e75ef32..e7732ea45b 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs @@ -20,6 +20,7 @@ public static class CookieAuthenticationOptionsExtensions { advance ??= TimeSpan.FromMinutes(3); validationInterval ??= TimeSpan.FromMinutes(1); + var previousHandler = options.Events.OnValidatePrincipal; options.Events.OnValidatePrincipal = async principalContext => { if (principalContext.Principal == null || principalContext.Principal.Identity == null || !principalContext.Principal.Identity.IsAuthenticated) @@ -30,25 +31,37 @@ public static class CookieAuthenticationOptionsExtensions var logger = principalContext.HttpContext.RequestServices.GetRequiredService>(); var tokenExpiresAt = principalContext.Properties.GetString(".Token.expires_at"); - if (!tokenExpiresAt.IsNullOrWhiteSpace() && DateTimeOffset.TryParseExact(tokenExpiresAt, "o", null, DateTimeStyles.RoundtripKind, out var expiresAt) && - expiresAt < DateTimeOffset.UtcNow.Subtract(advance.Value)) + if (!tokenExpiresAt.IsNullOrWhiteSpace() && DateTimeOffset.TryParseExact(tokenExpiresAt, "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var expiresAt) && + expiresAt <= DateTimeOffset.UtcNow.Add(advance.Value)) { - logger.LogInformation("The access_token is expired."); + logger.LogInformation("The access_token expires within {AdvanceSeconds}s; signing out.", advance.Value.TotalSeconds); await SignOutAsync(principalContext); return; } if (principalContext.Properties.IssuedUtc != null && DateTimeOffset.UtcNow.Subtract(principalContext.Properties.IssuedUtc.Value) > validationInterval) { - logger.LogInformation($"Check the access_token is active every {validationInterval.Value.TotalSeconds} seconds."); + logger.LogInformation("Checking access_token activity every {Seconds} seconds.", validationInterval.Value.TotalSeconds); var accessToken = principalContext.Properties.GetTokenValue("access_token"); if (!accessToken.IsNullOrWhiteSpace()) { var openIdConnectOptions = await GetOpenIdConnectOptions(principalContext, oidcAuthenticationScheme); + var introspectionEndpoint = openIdConnectOptions.Configuration?.IntrospectionEndpoint; + if (introspectionEndpoint.IsNullOrWhiteSpace() && !openIdConnectOptions.Authority.IsNullOrWhiteSpace()) + { + introspectionEndpoint = openIdConnectOptions.Authority.EnsureEndsWith('/') + "connect/introspect"; + } + + if (introspectionEndpoint.IsNullOrWhiteSpace()) + { + logger.LogWarning("No introspection endpoint configured. Skipping token activity check."); + return; + } + var response = await openIdConnectOptions.Backchannel.IntrospectTokenAsync(new TokenIntrospectionRequest { - Address = openIdConnectOptions.Configuration?.IntrospectionEndpoint ?? openIdConnectOptions.Authority!.EnsureEndsWith('/') + "connect/introspect", + Address = introspectionEndpoint, ClientId = openIdConnectOptions.ClientId!, ClientSecret = openIdConnectOptions.ClientSecret, Token = accessToken @@ -56,7 +69,7 @@ public static class CookieAuthenticationOptionsExtensions if (response.IsError) { - logger.LogError(response.Error); + logger.LogError("Token introspection error: {Error}", response.Error); await SignOutAsync(principalContext); return; } @@ -73,16 +86,21 @@ public static class CookieAuthenticationOptionsExtensions } else { - logger.LogError("The access_token is not found in the cookie properties, Please make sure SaveTokens of OpenIdConnectOptions is set as true."); + logger.LogError("The access_token is not found in the cookie properties. Ensure SaveTokens of OpenIdConnectOptions is true."); await SignOutAsync(principalContext); } } + + if (previousHandler != null) + { + await previousHandler(principalContext); + } }; return options; } - private async static Task GetOpenIdConnectOptions(CookieValidatePrincipalContext principalContext, string oidcAuthenticationScheme) + private static async Task GetOpenIdConnectOptions(CookieValidatePrincipalContext principalContext, string oidcAuthenticationScheme) { var openIdConnectOptions = principalContext.HttpContext.RequestServices.GetRequiredService>().Get(oidcAuthenticationScheme); var cancellationTokenProvider = principalContext.HttpContext.RequestServices.GetRequiredService(); @@ -94,7 +112,7 @@ public static class CookieAuthenticationOptionsExtensions return openIdConnectOptions; } - private async static Task SignOutAsync(CookieValidatePrincipalContext principalContext) + private static async Task SignOutAsync(CookieValidatePrincipalContext principalContext) { principalContext.RejectPrincipal(); await principalContext.HttpContext.SignOutAsync(principalContext.Scheme.Name); From 71bc239f8830587e9e32297595c83848fc00d208 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 2 Jan 2026 16:19:56 +0800 Subject: [PATCH 13/17] Refactor token expiration handling in cookie auth options --- .../CookieAuthenticationOptionsExtensions.cs | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs index e7732ea45b..7d6955f08b 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/CookieAuthenticationOptionsExtensions.cs @@ -14,7 +14,7 @@ namespace Microsoft.Extensions.DependencyInjection; public static class CookieAuthenticationOptionsExtensions { /// - /// Check the access_token is expired or inactive. + /// Check if the access_token is expired or inactive. /// public static CookieAuthenticationOptions CheckTokenExpiration(this CookieAuthenticationOptions options, string oidcAuthenticationScheme = "oidc", TimeSpan? advance = null, TimeSpan? validationInterval = null) { @@ -25,6 +25,7 @@ public static class CookieAuthenticationOptionsExtensions { if (principalContext.Principal == null || principalContext.Principal.Identity == null || !principalContext.Principal.Identity.IsAuthenticated) { + await InvokePreviousHandlerAsync(principalContext, previousHandler); return; } @@ -35,7 +36,7 @@ public static class CookieAuthenticationOptionsExtensions expiresAt <= DateTimeOffset.UtcNow.Add(advance.Value)) { logger.LogInformation("The access_token expires within {AdvanceSeconds}s; signing out.", advance.Value.TotalSeconds); - await SignOutAsync(principalContext); + await SignOutAndInvokePreviousHandlerAsync(principalContext, previousHandler); return; } @@ -56,6 +57,7 @@ public static class CookieAuthenticationOptionsExtensions if (introspectionEndpoint.IsNullOrWhiteSpace()) { logger.LogWarning("No introspection endpoint configured. Skipping token activity check."); + await InvokePreviousHandlerAsync(principalContext, previousHandler); return; } @@ -70,14 +72,14 @@ public static class CookieAuthenticationOptionsExtensions if (response.IsError) { logger.LogError("Token introspection error: {Error}", response.Error); - await SignOutAsync(principalContext); + await SignOutAndInvokePreviousHandlerAsync(principalContext, previousHandler); return; } if (!response.IsActive) { logger.LogError("The access_token is not active."); - await SignOutAsync(principalContext); + await SignOutAndInvokePreviousHandlerAsync(principalContext, previousHandler); return; } @@ -91,10 +93,7 @@ public static class CookieAuthenticationOptionsExtensions } } - if (previousHandler != null) - { - await previousHandler(principalContext); - } + await InvokePreviousHandlerAsync(principalContext, previousHandler); }; return options; @@ -117,4 +116,15 @@ public static class CookieAuthenticationOptionsExtensions principalContext.RejectPrincipal(); await principalContext.HttpContext.SignOutAsync(principalContext.Scheme.Name); } + + private static Task InvokePreviousHandlerAsync(CookieValidatePrincipalContext principalContext, Func? previousHandler) + { + return previousHandler != null ? previousHandler(principalContext) : Task.CompletedTask; + } + + private static async Task SignOutAndInvokePreviousHandlerAsync(CookieValidatePrincipalContext principalContext, Func? previousHandler) + { + await SignOutAsync(principalContext); + await InvokePreviousHandlerAsync(principalContext, previousHandler); + } } From 8d40a1c935ebcd8db475b3eeaa62352e51086d38 Mon Sep 17 00:00:00 2001 From: irem1demirci Date: Fri, 2 Jan 2026 16:34:04 +0300 Subject: [PATCH 14/17] Update post.md --- docs/en/Community-Articles/2025-09-02-training-campaign/post.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/Community-Articles/2025-09-02-training-campaign/post.md b/docs/en/Community-Articles/2025-09-02-training-campaign/post.md index 20f2bcf4bd..40314695aa 100644 --- a/docs/en/Community-Articles/2025-09-02-training-campaign/post.md +++ b/docs/en/Community-Articles/2025-09-02-training-campaign/post.md @@ -1,6 +1,6 @@ # IMPROVE YOUR ABP SKILLS WITH 33% OFF LIVE TRAININGS! -We have exciting news to share\! As you know, we offer live training packages to help you improve your skills and knowledge of ABP. From September 8th to 19th, we are giving you 33% OFF our live trainings, so you can learn more about the product at a discounted price\! +We have exciting news to share\! As you know, we offer live training packages to help you improve your skills and knowledge of ABP. For a limited time, we are giving you 33% OFF our live trainings, so you can learn more about the product at a discounted price\! #### Why Join ABP.IO Training? From 275b181fce9e908750eb9de7382726bf4ba264d6 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 3 Jan 2026 16:51:37 +0800 Subject: [PATCH 15/17] Enhance DateTime handling with timezone support in model binding and JSON converters --- .../ModelBinding/AbpDateTimeModelBinder.cs | 42 +++---- .../AbpDateTimeModelBinderProvider.cs | 5 +- .../Json/Newtonsoft/AbpDateTimeConverter.cs | 42 ++++--- .../JsonConverters/AbpDateTimeConverter.cs | 69 +++-------- .../AbpDateTimeConverterBase.cs | 110 ++++++++++++++++++ .../AbpNullableDateTimeConverter.cs | 67 +++-------- .../ModelBinding/ModelBindingController.cs | 40 +++++++ .../ModelBindingController_Tests.cs | 80 +++++++++++++ 8 files changed, 310 insertions(+), 145 deletions(-) create mode 100644 framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs index 35e1777f4c..c63c2c582f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs @@ -10,45 +10,41 @@ public class AbpDateTimeModelBinder : IModelBinder { private readonly DateTimeModelBinder _dateTimeModelBinder; private readonly IClock _clock; + private readonly ICurrentTimezoneProvider _currentTimezoneProvider; + private readonly ITimezoneProvider _timezoneProvider; - public AbpDateTimeModelBinder(IClock clock, DateTimeModelBinder dateTimeModelBinder) + public AbpDateTimeModelBinder(DateTimeModelBinder dateTimeModelBinder, IClock clock, ICurrentTimezoneProvider currentTimezoneProvider, ITimezoneProvider timezoneProvider) { - _clock = clock; _dateTimeModelBinder = dateTimeModelBinder; + _clock = clock; + _currentTimezoneProvider = currentTimezoneProvider; + _timezoneProvider = timezoneProvider; } public async Task BindModelAsync(ModelBindingContext bindingContext) { await _dateTimeModelBinder.BindModelAsync(bindingContext); - + if (!bindingContext.Result.IsModelSet || bindingContext.Result.Model is not DateTime dateTime) { return; } - - // If the DateTime has no timezone info (most cases from input) - if (dateTime.Kind == DateTimeKind.Unspecified) + + if (dateTime.Kind == DateTimeKind.Unspecified && + _clock.SupportsMultipleTimezone && + !_currentTimezoneProvider.TimeZone.IsNullOrWhiteSpace()) { - // Try to get user's timezone - var userTz = _currentTimezoneProvider.TimeZone; - if (!userTz.IsNullOrWhiteSpace()) + try { - try - { - var tzInfo = _timezoneProvider.GetTimeZoneInfo(userTz); - // Treat the input as user's local time and convert to UTC - var utc = TimeZoneInfo.ConvertTimeToUtc(dateTime, tzInfo); - bindingContext.Result = ModelBindingResult.Success(utc); - return; - } - catch - { - // fallback to default clock normalization if invalid TZ - } + var timezoneInfo = _timezoneProvider.GetTimeZoneInfo(_currentTimezoneProvider.TimeZone); + dateTime = new DateTimeOffset(dateTime, timezoneInfo.BaseUtcOffset).UtcDateTime; + } + catch + { + // ignored } } - - // fallback: original behavior + bindingContext.Result = ModelBindingResult.Success(_clock.Normalize(dateTime)); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinderProvider.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinderProvider.cs index a4c3ee2288..3be76251c3 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinderProvider.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinderProvider.cs @@ -50,6 +50,9 @@ public class AbpDateTimeModelBinderProvider : IModelBinderProvider { const DateTimeStyles supportedStyles = DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AdjustToUniversal; var dateTimeModelBinder = new DateTimeModelBinder(supportedStyles, context.Services.GetRequiredService()); - return new AbpDateTimeModelBinder(context.Services.GetRequiredService(), dateTimeModelBinder); + return new AbpDateTimeModelBinder(dateTimeModelBinder, + context.Services.GetRequiredService(), + context.Services.GetRequiredService(), + context.Services.GetRequiredService()); } } diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs index c95660e484..257a90e84c 100644 --- a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs @@ -19,11 +19,15 @@ public class AbpDateTimeConverter : DateTimeConverterBase, ITransientDependency private readonly CultureInfo _culture = CultureInfo.InvariantCulture; private readonly IClock _clock; private readonly AbpJsonOptions _options; + private readonly ICurrentTimezoneProvider _currentTimezoneProvider; + private readonly ITimezoneProvider _timezoneProvider; private bool _skipDateTimeNormalization; - public AbpDateTimeConverter(IClock clock, IOptions options) + public AbpDateTimeConverter(IClock clock, IOptions options, ICurrentTimezoneProvider currentTimezoneProvider, ITimezoneProvider timezoneProvider) { _clock = clock; + _currentTimezoneProvider = currentTimezoneProvider; + _timezoneProvider = timezoneProvider; _options = options.Value; } @@ -41,19 +45,14 @@ public class AbpDateTimeConverter : DateTimeConverterBase, ITransientDependency public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { var nullable = Nullable.GetUnderlyingType(objectType) != null; - if (reader.TokenType == JsonToken.Null) + switch (reader.TokenType) { - if (!nullable) - { + case JsonToken.Null when !nullable: throw new JsonSerializationException($"Cannot convert null value to {objectType.FullName}."); - } - - return null; - } - - if (reader.TokenType == JsonToken.Date) - { - return Normalize(reader.Value!.To()); + case JsonToken.Null: + return null; + case JsonToken.Date: + return Normalize(reader.Value!.To()); } if (reader.TokenType != JsonToken.String) @@ -108,7 +107,7 @@ public class AbpDateTimeConverter : DateTimeConverterBase, ITransientDependency } } - static internal bool ShouldNormalize(MemberInfo member, JsonProperty property) + internal static bool ShouldNormalize(MemberInfo member, JsonProperty property) { if (property.PropertyType != typeof(DateTime) && property.PropertyType != typeof(DateTime?)) @@ -121,6 +120,23 @@ public class AbpDateTimeConverter : DateTimeConverterBase, ITransientDependency protected virtual DateTime Normalize(DateTime dateTime) { + if (dateTime.Kind != DateTimeKind.Unspecified || + !_clock.SupportsMultipleTimezone || + _currentTimezoneProvider.TimeZone.IsNullOrWhiteSpace()) + { + return _skipDateTimeNormalization ? dateTime : _clock.Normalize(dateTime); + } + + try + { + var timezoneInfo = _timezoneProvider.GetTimeZoneInfo(_currentTimezoneProvider.TimeZone); + dateTime = new DateTimeOffset(dateTime, timezoneInfo.BaseUtcOffset).UtcDateTime; + } + catch + { + // ignored + } + return _skipDateTimeNormalization ? dateTime : _clock.Normalize(dateTime); diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs index 3feea8d299..d796c3183f 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverter.cs @@ -1,84 +1,43 @@ using System; -using System.Globalization; using System.Linq; using System.Text.Json; -using System.Text.Json.Serialization; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.Timing; namespace Volo.Abp.Json.SystemTextJson.JsonConverters; -public class AbpDateTimeConverter : JsonConverter, ITransientDependency +public class AbpDateTimeConverter : AbpDateTimeConverterBase, ITransientDependency { - private readonly IClock _clock; - private readonly AbpJsonOptions _options; - private bool _skipDateTimeNormalization; - - public AbpDateTimeConverter(IClock clock, IOptions abpJsonOptions) + public AbpDateTimeConverter( + IClock clock, + IOptions abpJsonOptions, + ICurrentTimezoneProvider currentTimezoneProvider, + ITimezoneProvider timezoneProvider) + : base(clock, abpJsonOptions, currentTimezoneProvider, timezoneProvider) { - _clock = clock; - _options = abpJsonOptions.Value; } public virtual AbpDateTimeConverter SkipDateTimeNormalization() { - _skipDateTimeNormalization = true; + IsSkipDateTimeNormalization = true; return this; } public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - if (_options.InputDateTimeFormats.Any()) - { - if (reader.TokenType == JsonTokenType.String) - { - foreach (var format in _options.InputDateTimeFormats) - { - var s = reader.GetString(); - if (DateTime.TryParseExact(s, format, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d1)) - { - return Normalize(d1); - } - } - } - else - { - throw new JsonException("Reader's TokenType is not String!"); - } - } - - if (reader.TryGetDateTime(out var d3)) - { - return Normalize(d3); - } - - var dateText = reader.GetString(); - if (!dateText.IsNullOrWhiteSpace()) + if (Options.InputDateTimeFormats.Any() && reader.TokenType != JsonTokenType.String) { - if (DateTime.TryParse(dateText, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d4)) - { - return Normalize(d4); - } + throw new JsonException("Reader's TokenType is not String!"); } - throw new JsonException("Can't get datetime from the reader!"); + return TryReadDateTime(ref reader, out var result) + ? result + : throw new JsonException("Can't get datetime from the reader!"); } public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { - if (_options.OutputDateTimeFormat.IsNullOrWhiteSpace()) - { - writer.WriteStringValue(Normalize(value)); - } - else - { - writer.WriteStringValue(Normalize(value).ToString(_options.OutputDateTimeFormat, CultureInfo.CurrentUICulture)); - } - } - - protected virtual DateTime Normalize(DateTime dateTime) - { - return _skipDateTimeNormalization ? dateTime : _clock.Normalize(dateTime); + WriteDateTime(writer, value); } } diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs new file mode 100644 index 0000000000..2f3557fcbd --- /dev/null +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs @@ -0,0 +1,110 @@ +using System; +using System.Globalization; +using System.Linq; +using System.Text.Json; +using System.Text.Json.Serialization; +using Microsoft.Extensions.Options; +using Volo.Abp.Timing; + +namespace Volo.Abp.Json.SystemTextJson.JsonConverters; + +public abstract class AbpDateTimeConverterBase : JsonConverter +{ + protected IClock Clock { get; } + protected AbpJsonOptions Options { get; } + protected ICurrentTimezoneProvider CurrentTimezoneProvider { get; } + protected ITimezoneProvider TimezoneProvider { get; } + protected bool IsSkipDateTimeNormalization { get; set; } + + protected AbpDateTimeConverterBase( + IClock clock, + IOptions abpJsonOptions, + ICurrentTimezoneProvider currentTimezoneProvider, + ITimezoneProvider timezoneProvider) + { + Clock = clock; + CurrentTimezoneProvider = currentTimezoneProvider; + TimezoneProvider = timezoneProvider; + Options = abpJsonOptions.Value; + } + + protected bool TryReadDateTime(ref Utf8JsonReader reader, out DateTime value) + { + value = default; + + if (Options.InputDateTimeFormats.Any()) + { + if (reader.TokenType != JsonTokenType.String) + { + return false; + } + + var s = reader.GetString(); + foreach (var format in Options.InputDateTimeFormats) + { + if (!DateTime.TryParseExact(s, format, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d1)) + { + continue; + } + + value = Normalize(d1); + return true; + } + } + + if (reader.TryGetDateTime(out var d2)) + { + value = Normalize(d2); + return true; + } + + var dateText = reader.GetString(); + if (dateText.IsNullOrWhiteSpace()) + { + return false; + } + + if (!DateTime.TryParse(dateText, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d3)) + { + return false; + } + + value = Normalize(d3); + return true; + + } + + protected void WriteDateTime(Utf8JsonWriter writer, DateTime value) + { + if (Options.OutputDateTimeFormat.IsNullOrWhiteSpace()) + { + writer.WriteStringValue(Normalize(value)); + } + else + { + writer.WriteStringValue(Normalize(value).ToString(Options.OutputDateTimeFormat, CultureInfo.CurrentUICulture)); + } + } + + protected virtual DateTime Normalize(DateTime dateTime) + { + if (dateTime.Kind != DateTimeKind.Unspecified || + !Clock.SupportsMultipleTimezone || + CurrentTimezoneProvider.TimeZone.IsNullOrWhiteSpace()) + { + return IsSkipDateTimeNormalization ? dateTime : Clock.Normalize(dateTime); + } + + try + { + var timezoneInfo = TimezoneProvider.GetTimeZoneInfo(CurrentTimezoneProvider.TimeZone); + dateTime = new DateTimeOffset(dateTime, timezoneInfo.BaseUtcOffset).UtcDateTime; + } + catch + { + // ignored + } + + return IsSkipDateTimeNormalization ? dateTime : Clock.Normalize(dateTime); + } +} diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs index e73f39d097..e1125b5040 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpNullableDateTimeConverter.cs @@ -1,65 +1,39 @@ using System; -using System.Globalization; using System.Linq; using System.Text.Json; -using System.Text.Json.Serialization; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.Timing; namespace Volo.Abp.Json.SystemTextJson.JsonConverters; -public class AbpNullableDateTimeConverter : JsonConverter, ITransientDependency +public class AbpNullableDateTimeConverter : AbpDateTimeConverterBase, ITransientDependency { - private readonly IClock _clock; - private readonly AbpJsonOptions _options; - private bool _skipDateTimeNormalization; - - public AbpNullableDateTimeConverter(IClock clock, IOptions abpJsonOptions) + public AbpNullableDateTimeConverter( + IClock clock, + IOptions abpJsonOptions, + ICurrentTimezoneProvider currentTimezoneProvider, + ITimezoneProvider timezoneProvider) + : base(clock, abpJsonOptions, currentTimezoneProvider, timezoneProvider) { - _clock = clock; - _options = abpJsonOptions.Value; } public virtual AbpNullableDateTimeConverter SkipDateTimeNormalization() { - _skipDateTimeNormalization = true; + IsSkipDateTimeNormalization = true; return this; } public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - if (_options.InputDateTimeFormats.Any()) - { - if (reader.TokenType == JsonTokenType.String) - { - foreach (var format in _options.InputDateTimeFormats) - { - var s = reader.GetString(); - if (DateTime.TryParseExact(s, format, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d1)) - { - return Normalize(d1); - } - } - } - else - { - throw new JsonException("Reader's TokenType is not String!"); - } - } - - if (reader.TryGetDateTime(out var d2)) + if (Options.InputDateTimeFormats.Any() && reader.TokenType != JsonTokenType.String) { - return Normalize(d2); + throw new JsonException("Reader's TokenType is not String!"); } - var dateText = reader.GetString(); - if (!dateText.IsNullOrWhiteSpace()) + if (TryReadDateTime(ref reader, out var result)) { - if (DateTime.TryParse(dateText, CultureInfo.CurrentUICulture, DateTimeStyles.None, out var d3)) - { - return Normalize(d3); - } + return result; } return null; @@ -70,22 +44,9 @@ public class AbpNullableDateTimeConverter : JsonConverter, ITransient if (value == null) { writer.WriteNullValue(); + return; } - else - { - if (_options.OutputDateTimeFormat.IsNullOrWhiteSpace()) - { - writer.WriteStringValue(Normalize(value.Value)); - } - else - { - writer.WriteStringValue(Normalize(value.Value).ToString(_options.OutputDateTimeFormat, CultureInfo.CurrentUICulture)); - } - } - } - protected virtual DateTime Normalize(DateTime dateTime) - { - return _skipDateTimeNormalization ? dateTime : _clock.Normalize(dateTime); + WriteDateTime(writer, value.Value); } } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController.cs index 6dfe3a7281..4093e47011 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController.cs @@ -13,12 +13,24 @@ public class ModelBindingController : AbpController return input.Kind.ToString().ToLower(); } + [HttpGet("DateTimeKind_WithResult")] + public string DateTimeKind_WithResult(DateTime input) + { + return input.Kind.ToString().ToLower() + "_" + input.ToString("O").ToLower(); + } + [HttpGet("NullableDateTimeKind")] public string NullableDateTimeKind(DateTime? input) { return input.Value.Kind.ToString().ToLower(); } + [HttpGet("NullableDateTimeKind_WithResult")] + public string NullableDateTimeKind_WithResult(DateTime? input) + { + return input.Value.Kind.ToString().ToLower() + "_" + input.Value.ToString("O").ToLower(); + } + [HttpGet("DisableDateTimeNormalizationDateTimeKind")] public string DisableDateTimeNormalizationDateTimeKind([DisableDateTimeNormalization] DateTime input) { @@ -40,6 +52,19 @@ public class ModelBindingController : AbpController input.InnerModel.Time4.Kind.ToString().ToLower(); } + [HttpGet("ComplexTypeDateTimeKind_WithResult")] + public string ComplexTypeDateTimeKind_WithResult(GetDateTimeKindModel input) + { + return input.Time1.Kind.ToString().ToLower() + "_" + + input.Time1.ToString("O").ToLower() + "_" + + input.Time2.Kind.ToString().ToLower() + "_" + + input.Time2.ToString("O").ToLower() + "_" + + input.Time3.Value.Kind.ToString().ToLower() + "_" + + input.Time3.Value.ToString("O").ToLower() + "_" + + input.InnerModel.Time4.Kind.ToString().ToLower() + "_" + + input.InnerModel.Time4.ToString("O").ToLower(); + } + //JSON input and output. [HttpPost("ComplexTypeDateTimeKind_JSON")] public string ComplexTypeDateTimeKind_JSON([FromBody] GetDateTimeKindModel input) @@ -50,6 +75,21 @@ public class ModelBindingController : AbpController input.InnerModel.Time4.Kind.ToString().ToLower(); } + //JSON input and output. + [HttpPost("ComplexTypeDateTimeKind_JSON_WithResult")] + public string ComplexTypeDateTimeKind_JSON_WithResult([FromBody] GetDateTimeKindModel input) + { + return input.Time1.Kind.ToString().ToLower() + "_" + + input.Time1.ToString("O").ToLower() + "_" + + input.Time2.Kind.ToString().ToLower() + "_" + + input.Time2.ToString("O").ToLower() + "_" + + input.Time3.Value.Kind.ToString().ToLower() + "_" + + input.Time3.Value.ToString("O").ToLower() + "_" + + input.InnerModel.Time4.Kind.ToString().ToLower() + "_" + + input.InnerModel.Time4.ToString("O").ToLower(); + } + + [HttpPost("Guid_Json_Test")] public GuidJsonModel Guid_Json_Test([FromBody] GuidJsonModel input) { diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs index edfdc4ff73..0d2820c59c 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs @@ -26,6 +26,28 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase resultAsString.ShouldBe(Kind.ToString().ToLower()); } + [Fact] + public async Task DateTimeKind_WithTimezone_Test() + { + var response = await Client.GetAsync("/api/model-Binding-test/DateTimeKind_WithResult?input=2010-01-01T00:00:00&__timezone=Europe/Istanbul"); + + response.StatusCode.ShouldBe(HttpStatusCode.OK); + var resultAsString = await response.Content.ReadAsStringAsync(); + + var dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + switch(Kind) + { + case DateTimeKind.Utc: + dateTime = new DateTime(2009, 12, 31, 21, 0, 0, DateTimeKind.Utc); //Turkey is UTC+3 + break; + case DateTimeKind.Local: + dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Local); + break; + } + + resultAsString.ShouldBe($"{Kind.ToString().ToLower()}_{dateTime.ToString("O").ToLower()}"); + } + [Fact] public async Task NullableDateTimeKind_Test() { @@ -37,6 +59,29 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase resultAsString.ShouldBe(Kind.ToString().ToLower()); } + [Fact] + public async Task NullableDateTimeKind_WithTimezone_Test() + { + var response = + await Client.GetAsync("/api/model-Binding-test/NullableDateTimeKind_WithResult?input=2010-01-01T00:00:00&__timezone=Europe/Istanbul"); + + response.StatusCode.ShouldBe(HttpStatusCode.OK); + var resultAsString = await response.Content.ReadAsStringAsync(); + + var dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + switch(Kind) + { + case DateTimeKind.Utc: + dateTime = new DateTime(2009, 12, 31, 21, 0, 0, DateTimeKind.Utc); //Turkey is UTC+3 + break; + case DateTimeKind.Local: + dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Local); + break; + } + + resultAsString.ShouldBe($"{Kind.ToString().ToLower()}_{dateTime.ToString("O").ToLower()}"); + } + [Fact] public async Task DisableDateTimeNormalizationDateTimeKind_Test() { @@ -104,6 +149,41 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase resultAsString.ShouldBe($"local_{Kind.ToString().ToLower()}_{Kind.ToString().ToLower()}_local"); } + [Fact] + public async Task ComplexTypeDateTimeKind_JSON_WithTimezone_Test() + { + var time = DateTime.Parse("2010-01-01T00:00:00"); + var response = await Client.PostAsync("/api/model-Binding-test/ComplexTypeDateTimeKind_JSON_WithResult?__timezone=Europe/Istanbul", + new StringContent(JsonSerializer.Serialize( + new GetDateTimeKindModel + { + Time1 = time, + Time2 = time, + Time3 = time, + InnerModel = new GetDateTimeKindModel.GetDateTimeKindInnerModel + { + Time4 = time + } + } + ), Encoding.UTF8, MimeTypes.Application.Json)); + + response.StatusCode.ShouldBe(HttpStatusCode.OK); + var resultAsString = await response.Content.ReadAsStringAsync(); + + var dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + switch(Kind) + { + case DateTimeKind.Utc: + dateTime = new DateTime(2009, 12, 31, 21, 0, 0, DateTimeKind.Utc); //Turkey is UTC+3 + break; + case DateTimeKind.Local: + dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Local); + break; + } + + resultAsString.ShouldBe($"unspecified_{time.ToString("O").ToLower()}_{Kind.ToString().ToLower()}_{dateTime.ToString("O").ToLower()}_{Kind.ToString().ToLower()}_{dateTime.ToString("O").ToLower()}_unspecified_{time.ToString("O").ToLower()}"); + } + [Fact] public async Task Guid_Json_Test() { From 84cf25f17b1eb068b4071a1f4fe94ecf3d69a1c7 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 3 Jan 2026 18:02:38 +0800 Subject: [PATCH 16/17] Refactor DateTime handling to use GetUtcOffset for accurate timezone adjustments in model binding and JSON converters --- .../ModelBinding/AbpDateTimeModelBinder.cs | 2 +- .../Json/Newtonsoft/AbpDateTimeConverter.cs | 2 +- .../AbpDateTimeConverterBase.cs | 2 +- .../ModelBinding/ModelBindingController.cs | 1 - .../ModelBindingController_Tests.cs | 54 +++++++++---------- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs index c63c2c582f..109b85fe9e 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs @@ -37,7 +37,7 @@ public class AbpDateTimeModelBinder : IModelBinder try { var timezoneInfo = _timezoneProvider.GetTimeZoneInfo(_currentTimezoneProvider.TimeZone); - dateTime = new DateTimeOffset(dateTime, timezoneInfo.BaseUtcOffset).UtcDateTime; + dateTime = new DateTimeOffset(dateTime, timezoneInfo.GetUtcOffset(dateTime)).UtcDateTime; } catch { diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs index 257a90e84c..7b9a2cd4d3 100644 --- a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs @@ -130,7 +130,7 @@ public class AbpDateTimeConverter : DateTimeConverterBase, ITransientDependency try { var timezoneInfo = _timezoneProvider.GetTimeZoneInfo(_currentTimezoneProvider.TimeZone); - dateTime = new DateTimeOffset(dateTime, timezoneInfo.BaseUtcOffset).UtcDateTime; + dateTime = new DateTimeOffset(dateTime, timezoneInfo.GetUtcOffset(dateTime)).UtcDateTime; } catch { diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs index 2f3557fcbd..b603353d0c 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs @@ -98,7 +98,7 @@ public abstract class AbpDateTimeConverterBase : JsonConverter try { var timezoneInfo = TimezoneProvider.GetTimeZoneInfo(CurrentTimezoneProvider.TimeZone); - dateTime = new DateTimeOffset(dateTime, timezoneInfo.BaseUtcOffset).UtcDateTime; + dateTime = new DateTimeOffset(dateTime, timezoneInfo.GetUtcOffset(dateTime)).UtcDateTime; } catch { diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController.cs index 4093e47011..ceddc0083e 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController.cs @@ -89,7 +89,6 @@ public class ModelBindingController : AbpController input.InnerModel.Time4.ToString("O").ToLower(); } - [HttpPost("Guid_Json_Test")] public GuidJsonModel Guid_Json_Test([FromBody] GuidJsonModel input) { diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs index 0d2820c59c..2fd1130474 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs @@ -19,7 +19,7 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase [Fact] public async Task DateTimeKind_Test() { - var response = await Client.GetAsync("/api/model-Binding-test/DateTimeKind?input=2010-01-01T00:00:00Z"); + var response = await Client.GetAsync("/api/model-Binding-test/DateTimeKind?input=2020-01-01T00:00:00Z"); response.StatusCode.ShouldBe(HttpStatusCode.OK); var resultAsString = await response.Content.ReadAsStringAsync(); @@ -29,19 +29,19 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase [Fact] public async Task DateTimeKind_WithTimezone_Test() { - var response = await Client.GetAsync("/api/model-Binding-test/DateTimeKind_WithResult?input=2010-01-01T00:00:00&__timezone=Europe/Istanbul"); + var response = await Client.GetAsync("/api/model-Binding-test/DateTimeKind_WithResult?input=2020-01-01T00:00:00&__timezone=Europe/Istanbul"); response.StatusCode.ShouldBe(HttpStatusCode.OK); var resultAsString = await response.Content.ReadAsStringAsync(); - var dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); - switch(Kind) + var dateTime = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + switch (Kind) { case DateTimeKind.Utc: - dateTime = new DateTime(2009, 12, 31, 21, 0, 0, DateTimeKind.Utc); //Turkey is UTC+3 + dateTime = new DateTime(2019, 12, 31, 21, 0, 0, DateTimeKind.Utc); //Turkey is UTC+3 break; case DateTimeKind.Local: - dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Local); + dateTime = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Local); break; } @@ -52,7 +52,7 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase public async Task NullableDateTimeKind_Test() { var response = - await Client.GetAsync("/api/model-Binding-test/NullableDateTimeKind?input=2010-01-01T00:00:00Z"); + await Client.GetAsync("/api/model-Binding-test/NullableDateTimeKind?input=2020-01-01T00:00:00Z"); response.StatusCode.ShouldBe(HttpStatusCode.OK); var resultAsString = await response.Content.ReadAsStringAsync(); @@ -63,19 +63,19 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase public async Task NullableDateTimeKind_WithTimezone_Test() { var response = - await Client.GetAsync("/api/model-Binding-test/NullableDateTimeKind_WithResult?input=2010-01-01T00:00:00&__timezone=Europe/Istanbul"); + await Client.GetAsync("/api/model-Binding-test/NullableDateTimeKind_WithResult?input=2020-01-01T00:00:00&__timezone=Europe/Istanbul"); response.StatusCode.ShouldBe(HttpStatusCode.OK); var resultAsString = await response.Content.ReadAsStringAsync(); - var dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); - switch(Kind) + var dateTime = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + switch (Kind) { case DateTimeKind.Utc: - dateTime = new DateTime(2009, 12, 31, 21, 0, 0, DateTimeKind.Utc); //Turkey is UTC+3 + dateTime = new DateTime(2019, 12, 31, 21, 0, 0, DateTimeKind.Utc); //Turkey is UTC+3 break; case DateTimeKind.Local: - dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Local); + dateTime = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Local); break; } @@ -87,11 +87,11 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase { var response = await Client.GetAsync( - "/api/model-Binding-test/DisableDateTimeNormalizationDateTimeKind?input=2010-01-01T00:00:00Z"); + "/api/model-Binding-test/DisableDateTimeNormalizationDateTimeKind?input=2020-01-01T00:00:00Z"); response.StatusCode.ShouldBe(HttpStatusCode.OK); var resultAsString = await response.Content.ReadAsStringAsync(); - //Time parameter(2010-01-01T00:00:00Z) with time zone information, so the default Kind is UTC + //Time parameter(2020-01-01T00:00:00Z) with time zone information, so the default Kind is UTC //https://docs.microsoft.com/en-us/aspnet/core/migration/31-to-50?view=aspnetcore-3.1&tabs=visual-studio#datetime-values-are-model-bound-as-utc-times resultAsString.ShouldBe(DateTimeKind.Utc.ToString().ToLower()); } @@ -101,11 +101,11 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase { var response = await Client.GetAsync( - "/api/model-Binding-test/DisableDateTimeNormalizationNullableDateTimeKind?input=2010-01-01T00:00:00Z"); + "/api/model-Binding-test/DisableDateTimeNormalizationNullableDateTimeKind?input=2020-01-01T00:00:00Z"); response.StatusCode.ShouldBe(HttpStatusCode.OK); var resultAsString = await response.Content.ReadAsStringAsync(); - //Time parameter(2010-01-01T00:00:00Z) with time zone information, so the default Kind is UTC + //Time parameter(2020-01-01T00:00:00Z) with time zone information, so the default Kind is UTC //https://docs.microsoft.com/en-us/aspnet/core/migration/31-to-50?view=aspnetcore-3.1&tabs=visual-studio#datetime-values-are-model-bound-as-utc-times resultAsString.ShouldBe(DateTimeKind.Utc.ToString().ToLower()); } @@ -114,14 +114,14 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase public async Task ComplexTypeDateTimeKind_Test() { var response = await Client.GetAsync("/api/model-Binding-test/ComplexTypeDateTimeKind?" + - "Time1=2010-01-01T00:00:00Z&" + - "Time2=2010-01-01T00:00:00Z&" + - "Time3=2010-01-01T00:00:00Z&" + - "InnerModel.Time4=2010-01-01T00:00:00Z"); + "Time1=2020-01-01T00:00:00Z&" + + "Time2=2020-01-01T00:00:00Z&" + + "Time3=2020-01-01T00:00:00Z&" + + "InnerModel.Time4=2020-01-01T00:00:00Z"); response.StatusCode.ShouldBe(HttpStatusCode.OK); var resultAsString = await response.Content.ReadAsStringAsync(); - //Time parameter(2010-01-01T00:00:00Z) with time zone information, so the default Kind is UTC + //Time parameter(2020-01-01T00:00:00Z) with time zone information, so the default Kind is UTC //https://docs.microsoft.com/en-us/aspnet/core/migration/31-to-50?view=aspnetcore-3.1&tabs=visual-studio#datetime-values-are-model-bound-as-utc-times resultAsString.ShouldBe($"utc_{Kind.ToString().ToLower()}_{Kind.ToString().ToLower()}_utc"); } @@ -129,7 +129,7 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase [Fact] public async Task ComplexTypeDateTimeKind_JSON_Test() { - var time = DateTime.Parse("2010-01-01T00:00:00Z"); + var time = DateTime.Parse("2020-01-01T00:00:00Z"); var response = await Client.PostAsync("/api/model-Binding-test/ComplexTypeDateTimeKind_JSON", new StringContent(JsonSerializer.Serialize( new GetDateTimeKindModel @@ -152,7 +152,7 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase [Fact] public async Task ComplexTypeDateTimeKind_JSON_WithTimezone_Test() { - var time = DateTime.Parse("2010-01-01T00:00:00"); + var time = DateTime.Parse("2020-01-01T00:00:00"); var response = await Client.PostAsync("/api/model-Binding-test/ComplexTypeDateTimeKind_JSON_WithResult?__timezone=Europe/Istanbul", new StringContent(JsonSerializer.Serialize( new GetDateTimeKindModel @@ -170,14 +170,14 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase response.StatusCode.ShouldBe(HttpStatusCode.OK); var resultAsString = await response.Content.ReadAsStringAsync(); - var dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); - switch(Kind) + var dateTime = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + switch (Kind) { case DateTimeKind.Utc: - dateTime = new DateTime(2009, 12, 31, 21, 0, 0, DateTimeKind.Utc); //Turkey is UTC+3 + dateTime = new DateTime(2019, 12, 31, 21, 0, 0, DateTimeKind.Utc); //Turkey is UTC+3 break; case DateTimeKind.Local: - dateTime = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Local); + dateTime = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Local); break; } From 028362c6728f47331aa1211b4f122225d93ac318 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 3 Jan 2026 18:35:17 +0800 Subject: [PATCH 17/17] Add logging for DateTime conversion failures --- .../Mvc/ModelBinding/AbpDateTimeModelBinder.cs | 12 ++++++++++-- .../ModelBinding/AbpDateTimeModelBinderProvider.cs | 5 +---- .../Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs | 9 ++++++++- .../JsonConverters/AbpDateTimeConverterBase.cs | 8 +++++++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs index 109b85fe9e..ac6e44ed6c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs @@ -2,19 +2,27 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; +using Microsoft.Extensions.Logging; using Volo.Abp.Timing; namespace Volo.Abp.AspNetCore.Mvc.ModelBinding; public class AbpDateTimeModelBinder : IModelBinder { + private readonly ILogger _logger; private readonly DateTimeModelBinder _dateTimeModelBinder; private readonly IClock _clock; private readonly ICurrentTimezoneProvider _currentTimezoneProvider; private readonly ITimezoneProvider _timezoneProvider; - public AbpDateTimeModelBinder(DateTimeModelBinder dateTimeModelBinder, IClock clock, ICurrentTimezoneProvider currentTimezoneProvider, ITimezoneProvider timezoneProvider) + public AbpDateTimeModelBinder( + ILogger logger, + DateTimeModelBinder dateTimeModelBinder, + IClock clock, + ICurrentTimezoneProvider currentTimezoneProvider, + ITimezoneProvider timezoneProvider) { + _logger = logger; _dateTimeModelBinder = dateTimeModelBinder; _clock = clock; _currentTimezoneProvider = currentTimezoneProvider; @@ -41,7 +49,7 @@ public class AbpDateTimeModelBinder : IModelBinder } catch { - // ignored + _logger.LogWarning("Could not convert DateTime with unspecified Kind using timezone '{TimeZone}'.", _currentTimezoneProvider.TimeZone); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinderProvider.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinderProvider.cs index 3be76251c3..a836838cd1 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinderProvider.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinderProvider.cs @@ -50,9 +50,6 @@ public class AbpDateTimeModelBinderProvider : IModelBinderProvider { const DateTimeStyles supportedStyles = DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AdjustToUniversal; var dateTimeModelBinder = new DateTimeModelBinder(supportedStyles, context.Services.GetRequiredService()); - return new AbpDateTimeModelBinder(dateTimeModelBinder, - context.Services.GetRequiredService(), - context.Services.GetRequiredService(), - context.Services.GetRequiredService()); + return ActivatorUtilities.CreateInstance(context.Services, dateTimeModelBinder); } } diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs index 7b9a2cd4d3..55c270d14e 100644 --- a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpDateTimeConverter.cs @@ -2,6 +2,8 @@ using System.Globalization; using System.Linq; using System.Reflection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Newtonsoft.Json; using Newtonsoft.Json.Converters; @@ -15,6 +17,9 @@ namespace Volo.Abp.Json.Newtonsoft; public class AbpDateTimeConverter : DateTimeConverterBase, ITransientDependency { private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK"; + + public ILogger Logger { get; set; } + private readonly DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind; private readonly CultureInfo _culture = CultureInfo.InvariantCulture; private readonly IClock _clock; @@ -25,6 +30,8 @@ public class AbpDateTimeConverter : DateTimeConverterBase, ITransientDependency public AbpDateTimeConverter(IClock clock, IOptions options, ICurrentTimezoneProvider currentTimezoneProvider, ITimezoneProvider timezoneProvider) { + Logger = NullLogger.Instance; + _clock = clock; _currentTimezoneProvider = currentTimezoneProvider; _timezoneProvider = timezoneProvider; @@ -134,7 +141,7 @@ public class AbpDateTimeConverter : DateTimeConverterBase, ITransientDependency } catch { - // ignored + Logger.LogWarning("Could not convert DateTime with unspecified Kind using timezone '{TimeZone}'.", _currentTimezoneProvider.TimeZone); } return _skipDateTimeNormalization diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs index b603353d0c..ee39a66678 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/JsonConverters/AbpDateTimeConverterBase.cs @@ -3,6 +3,8 @@ using System.Globalization; using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Volo.Abp.Timing; @@ -10,6 +12,8 @@ namespace Volo.Abp.Json.SystemTextJson.JsonConverters; public abstract class AbpDateTimeConverterBase : JsonConverter { + public ILogger> Logger { get; set; } + protected IClock Clock { get; } protected AbpJsonOptions Options { get; } protected ICurrentTimezoneProvider CurrentTimezoneProvider { get; } @@ -22,6 +26,8 @@ public abstract class AbpDateTimeConverterBase : JsonConverter ICurrentTimezoneProvider currentTimezoneProvider, ITimezoneProvider timezoneProvider) { + Logger = NullLogger>.Instance; + Clock = clock; CurrentTimezoneProvider = currentTimezoneProvider; TimezoneProvider = timezoneProvider; @@ -102,7 +108,7 @@ public abstract class AbpDateTimeConverterBase : JsonConverter } catch { - // ignored + Logger.LogWarning("Could not convert DateTime with unspecified Kind using timezone '{TimeZone}'.", CurrentTimezoneProvider.TimeZone); } return IsSkipDateTimeNormalization ? dateTime : Clock.Normalize(dateTime);