diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogEntityTypeFullNameConverter.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogEntityTypeFullNameConverter.cs new file mode 100644 index 0000000000..f50aec3d92 --- /dev/null +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogEntityTypeFullNameConverter.cs @@ -0,0 +1,39 @@ +using System; +using System.Text.RegularExpressions; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AuditLogging; + +public class AuditLogEntityTypeFullNameConverter : ITransientDependency +{ + public virtual string Convert(string typeFullName) + { + var genericType = Regex.Match(typeFullName, @"(.+?)`1\[\["); + if (!genericType.Success) + { + return ReplaceGenericSymbol(typeFullName); + } + + var type = Regex.Match(typeFullName, @"`1\[\[(.+?), "); + if (!type.Success) + { + return typeFullName; + } + + if (type.Groups[1].Value.Contains("System.Nullable`1[[")) + { + return genericType.Groups[1].Value + "<" + type.Groups[1].Value.Replace("System.Nullable`1[[", "") + "?>"; + } + + return genericType.Groups[1].Value.Contains("System.Nullable") + ? type.Groups[1].Value + "?" + : genericType.Groups[1].Value + "<" + ReplaceGenericSymbol(type.Groups[1].Value) + ">"; + } + + protected virtual string ReplaceGenericSymbol(string typeFullName) + { + return typeFullName.Contains("`1+") + ? typeFullName.Substring(0, typeFullName.IndexOf("[[", StringComparison.Ordinal)).Replace("`1+", ".") + : typeFullName; + } +} diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogInfoToAuditLogConverter.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogInfoToAuditLogConverter.cs index 94f0622714..30b0495658 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogInfoToAuditLogConverter.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogInfoToAuditLogConverter.cs @@ -19,12 +19,19 @@ public class AuditLogInfoToAuditLogConverter : IAuditLogInfoToAuditLogConverter, protected IExceptionToErrorInfoConverter ExceptionToErrorInfoConverter { get; } protected IJsonSerializer JsonSerializer { get; } protected AbpExceptionHandlingOptions ExceptionHandlingOptions { get; } + protected AuditLogEntityTypeFullNameConverter AuditLogEntityTypeFullNameConverter { get; } - public AuditLogInfoToAuditLogConverter(IGuidGenerator guidGenerator, IExceptionToErrorInfoConverter exceptionToErrorInfoConverter, IJsonSerializer jsonSerializer, IOptions exceptionHandlingOptions) + public AuditLogInfoToAuditLogConverter( + IGuidGenerator guidGenerator, + IExceptionToErrorInfoConverter exceptionToErrorInfoConverter, + IJsonSerializer jsonSerializer, + IOptions exceptionHandlingOptions, + AuditLogEntityTypeFullNameConverter auditLogEntityTypeFullNameConverter) { GuidGenerator = guidGenerator; ExceptionToErrorInfoConverter = exceptionToErrorInfoConverter; JsonSerializer = jsonSerializer; + AuditLogEntityTypeFullNameConverter = auditLogEntityTypeFullNameConverter; ExceptionHandlingOptions = exceptionHandlingOptions.Value; } @@ -41,6 +48,15 @@ public class AuditLogInfoToAuditLogConverter : IAuditLogInfoToAuditLogConverter, } } + foreach (var entityChange in auditLogInfo.EntityChanges ?? Enumerable.Empty()) + { + entityChange.EntityTypeFullName = AuditLogEntityTypeFullNameConverter.Convert(entityChange.EntityTypeFullName); + foreach (var propertyChange in entityChange.PropertyChanges ?? Enumerable.Empty()) + { + propertyChange.PropertyTypeFullName = AuditLogEntityTypeFullNameConverter.Convert(propertyChange.PropertyTypeFullName); + } + } + var entityChanges = auditLogInfo .EntityChanges? .Select(entityChangeInfo => new EntityChange(GuidGenerator, auditLogId, entityChangeInfo, tenantId: auditLogInfo.TenantId)) diff --git a/modules/audit-logging/test/Volo.Abp.AuditLogging.EntityFrameworkCore.Tests/Volo/Abp/AuditLogging/EntityFrameworkCore/AuditLogEntityTypeFullNameConverter_Tests.cs b/modules/audit-logging/test/Volo.Abp.AuditLogging.EntityFrameworkCore.Tests/Volo/Abp/AuditLogging/EntityFrameworkCore/AuditLogEntityTypeFullNameConverter_Tests.cs new file mode 100644 index 0000000000..db57347815 --- /dev/null +++ b/modules/audit-logging/test/Volo.Abp.AuditLogging.EntityFrameworkCore.Tests/Volo/Abp/AuditLogging/EntityFrameworkCore/AuditLogEntityTypeFullNameConverter_Tests.cs @@ -0,0 +1,6 @@ +namespace Volo.Abp.AuditLogging.EntityFrameworkCore; + +public class AuditLogEntityTypeFullNameConverter_Tests : AuditLogEntityTypeFullNameConverter_Tests +{ + +} diff --git a/modules/audit-logging/test/Volo.Abp.AuditLogging.MongoDB.Tests/Volo/Abp/AuditLogging/MongoDB/AuditLogEntityTypeFullNameConverter_Tests.cs b/modules/audit-logging/test/Volo.Abp.AuditLogging.MongoDB.Tests/Volo/Abp/AuditLogging/MongoDB/AuditLogEntityTypeFullNameConverter_Tests.cs new file mode 100644 index 0000000000..5c803a2a44 --- /dev/null +++ b/modules/audit-logging/test/Volo.Abp.AuditLogging.MongoDB.Tests/Volo/Abp/AuditLogging/MongoDB/AuditLogEntityTypeFullNameConverter_Tests.cs @@ -0,0 +1,9 @@ +using Xunit; + +namespace Volo.Abp.AuditLogging.MongoDB; + +[Collection(MongoTestCollection.Name)] +public class AuditLogEntityTypeFullNameConverter_Tests : AuditLogEntityTypeFullNameConverter_Tests +{ + +} diff --git a/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogEntityTypeFullNameConverter_Tests.cs b/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogEntityTypeFullNameConverter_Tests.cs new file mode 100644 index 0000000000..2aac19cd42 --- /dev/null +++ b/modules/audit-logging/test/Volo.Abp.AuditLogging.TestBase/Volo/Abp/AuditLogging/AuditLogEntityTypeFullNameConverter_Tests.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using Shouldly; +using Volo.Abp.Modularity; +using Xunit; + +namespace Volo.Abp.AuditLogging; + +public abstract class AuditLogEntityTypeFullNameConverter_Tests : AuditLoggingTestBase + where TStartupModule : IAbpModule +{ + private readonly AuditLogEntityTypeFullNameConverter _typeFullNameConverter; + + protected AuditLogEntityTypeFullNameConverter_Tests() + { + _typeFullNameConverter = GetRequiredService(); + } + + [Fact] + public void AuditLogEntityTypeFullNameConverter_Test() + { + _typeFullNameConverter.Convert("MyType").ShouldBe("MyType"); + + _typeFullNameConverter.Convert(typeof(string).FullName!).ShouldBe("System.String"); + _typeFullNameConverter.Convert(typeof(Guid).FullName!).ShouldBe("System.Guid"); + _typeFullNameConverter.Convert(typeof(Guid?).FullName!).ShouldBe("System.Guid?"); + _typeFullNameConverter.Convert(typeof(int).FullName!).ShouldBe("System.Int32"); + _typeFullNameConverter.Convert(typeof(long?).FullName!).ShouldBe("System.Int64?"); + _typeFullNameConverter.Convert(typeof(MyClass).FullName!).ShouldBe("Volo.Abp.AuditLogging.AuditLogEntityTypeFullNameConverter_Tests.MyClass"); + + _typeFullNameConverter.Convert(typeof(ICollection).FullName!).ShouldBe($"System.Collections.Generic.ICollection"); + _typeFullNameConverter.Convert(typeof(Collection).FullName!).ShouldBe($"System.Collections.ObjectModel.Collection"); + _typeFullNameConverter.Convert(typeof(List).FullName!).ShouldBe($"System.Collections.Generic.List"); + _typeFullNameConverter.Convert(typeof(List).FullName!).ShouldBe($"System.Collections.Generic.List"); + + _typeFullNameConverter.Convert(typeof(ICollection).FullName!).ShouldBe($"System.Collections.Generic.ICollection"); + _typeFullNameConverter.Convert(typeof(Collection).FullName!).ShouldBe($"System.Collections.ObjectModel.Collection"); + _typeFullNameConverter.Convert(typeof(List).FullName!).ShouldBe($"System.Collections.Generic.List"); + } + + public class MyClass + { + + } +}