From 1bbaefa34b1c3035acc9083d44e7be30e39ef02a Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 16 Jul 2018 15:50:45 +0300 Subject: [PATCH 1/3] Added GetFirstGenericArgumentIfNullable extension method --- .../Volo/Abp/Reflection/TypeHelper.cs | 15 +++++++++++++-- .../EntityHistory/EntityHistoryHelper.cs | 5 +++-- 2 files changed, 16 insertions(+), 4 deletions(-) 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 f79f0a60a2..a708ecc10d 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reflection; namespace Volo.Abp.Reflection @@ -33,8 +34,8 @@ namespace Volo.Abp.Reflection return true; } - if (includeNullables && - type.IsGenericType && + if (includeNullables && + type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) { return IsPrimitiveExtendedInternal(type.GenericTypeArguments[0], includeEnums); @@ -43,6 +44,16 @@ namespace Volo.Abp.Reflection return false; } + public static Type GetFirstGenericArgumentIfNullable(this Type t) + { + if (t.GetGenericArguments().Length > 0 && t.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + return t.GetGenericArguments().FirstOrDefault(); + } + + return t; + } + private static bool IsPrimitiveExtendedInternal(Type type, bool includeEnums) { if (type.IsPrimitive) 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 1c48fffc53..b50afd3b2a 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 @@ -13,6 +13,7 @@ using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities; using Volo.Abp.Json; using Volo.Abp.MultiTenancy; +using Volo.Abp.Reflection; using Volo.Abp.Timing; using Volo.Abp.Uow; @@ -170,7 +171,7 @@ namespace Volo.Abp.EntityFrameworkCore.EntityHistory 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.FullName, + PropertyTypeFullName = property.ClrType.GetFirstGenericArgumentIfNullable().FullName, TenantId = GetTenantId(entityEntry.Entity) }); } @@ -311,7 +312,7 @@ namespace Volo.Abp.EntityFrameworkCore.EntityHistory NewValue = JsonSerializer.Serialize(propertyEntry.CurrentValue), OriginalValue = JsonSerializer.Serialize(propertyEntry.OriginalValue), PropertyName = property.Name, - PropertyTypeFullName = property.ClrType.FullName + PropertyTypeFullName = property.ClrType.GetFirstGenericArgumentIfNullable().FullName }); } From b3ccf2c11f20fb3351b21bab6363f2f53e629045 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 16 Jul 2018 16:07:37 +0300 Subject: [PATCH 2/3] Added TypeHelper_Tests --- .../Volo/Abp/Reflection/TypeHelper_Tests.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs new file mode 100644 index 0000000000..0fc11addae --- /dev/null +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/Reflection/TypeHelper_Tests.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Shouldly; +using Xunit; + +namespace Volo.Abp.Reflection +{ + public class TypeHelper_Tests + { + [Fact] + public void Should_Generic_Type_From_Nullable() + { + var nullableType = typeof(Guid?); + var guidType = nullableType.GetFirstGenericArgumentIfNullable(); + + guidType.ShouldBe(typeof(Guid)); + } + } +} From 89f57cb73833f6e575fce9fc91db7be77bdbebc5 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 16 Jul 2018 17:18:26 +0300 Subject: [PATCH 3/3] Added TruncateFromBeginning extension and used in audit loggng module --- .../System/AbpStringExtensions.cs | 19 +++++++++++++++++++ .../EntityPropertyChangeConsts.cs | 6 +----- .../Volo/Abp/AuditLogging/AuditLog.cs | 4 ++-- .../Volo/Abp/AuditLogging/AuditLogAction.cs | 7 ++++--- .../Volo/Abp/AuditLogging/EntityChange.cs | 4 ++-- .../Abp/AuditLogging/EntityPropertyChange.cs | 8 ++++---- 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs b/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs index d97d8326a3..3fb7573858 100644 --- a/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs +++ b/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs @@ -364,6 +364,25 @@ namespace System return str.Left(maxLength); } + /// + /// Gets a substring of a string from Ending of the string if it exceeds maximum length. + /// + /// Thrown if is null + public static string TruncateFromBeginning(this string str, int maxLength) + { + if (str == null) + { + return null; + } + + if (str.Length <= maxLength) + { + return str; + } + + return str.Right(maxLength); + } + /// /// Gets a substring of a string from beginning of the string if it exceeds maximum length. /// It adds a "..." postfix to end of the string if it's truncated. diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/AuditLogging/EntityPropertyChangeConsts.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/AuditLogging/EntityPropertyChangeConsts.cs index 141ea04ed4..2ac9204395 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/AuditLogging/EntityPropertyChangeConsts.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/AuditLogging/EntityPropertyChangeConsts.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Volo.Abp.AuditLogging +namespace Volo.Abp.AuditLogging { public class EntityPropertyChangeConsts { diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLog.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLog.cs index 8859d515d7..1b2f17a87a 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLog.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLog.cs @@ -59,8 +59,8 @@ namespace Volo.Abp.AuditLogging ExecutionTime = auditInfo.ExecutionTime; ExecutionDuration = auditInfo.ExecutionDuration; ClientIpAddress = auditInfo.ClientIpAddress; - ClientName = auditInfo.ClientName; - BrowserInfo = auditInfo.BrowserInfo; + ClientName = auditInfo.ClientName.Truncate(AuditLogConsts.MaxClientNameLength); + BrowserInfo = auditInfo.BrowserInfo.Truncate(AuditLogConsts.MaxBrowserInfoLength); HttpMethod = auditInfo.HttpMethod; Url = auditInfo.Url; HttpStatusCode = auditInfo.HttpStatusCode; diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogAction.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogAction.cs index b9385d00a9..27291eca9b 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogAction.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogAction.cs @@ -34,15 +34,16 @@ namespace Volo.Abp.AuditLogging public AuditLogAction(Guid id, Guid auditLogId, AuditLogActionInfo actionInfo) { + Id = id; TenantId = actionInfo.TenantId; AuditLogId = auditLogId; - ServiceName = actionInfo.ServiceName; - MethodName = actionInfo.MethodName; - Parameters = actionInfo.Parameters; ExecutionTime = actionInfo.ExecutionTime; ExecutionDuration = actionInfo.ExecutionDuration; ExtraProperties = actionInfo.ExtraProperties.ToDictionary(pair => pair.Key, pair => pair.Value); + ServiceName = actionInfo.ServiceName.TruncateFromBeginning(AuditLogActionConsts.MaxServiceNameLength); + MethodName = actionInfo.MethodName.TruncateFromBeginning(AuditLogActionConsts.MaxMethodNameLength); + Parameters = actionInfo.Parameters.Length > AuditLogActionConsts.MaxParametersLength ? "" : actionInfo.Parameters; } } } diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityChange.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityChange.cs index d77e2566a8..5a9d263d30 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityChange.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityChange.cs @@ -40,8 +40,8 @@ namespace Volo.Abp.AuditLogging TenantId = entityChangeInfo.TenantId; ChangeTime = entityChangeInfo.ChangeTime; ChangeType = entityChangeInfo.ChangeType; - EntityId = entityChangeInfo.EntityId; - EntityTypeFullName = entityChangeInfo.EntityTypeFullName; + EntityId = entityChangeInfo.EntityId.Truncate(EntityChangeConsts.MaxEntityTypeFullNameLength); + EntityTypeFullName = entityChangeInfo.EntityTypeFullName.TruncateFromBeginning(EntityChangeConsts.MaxEntityTypeFullNameLength); PropertyChanges = entityChangeInfo.PropertyChanges.Select(p => new EntityPropertyChange(guidGenerator, Id, p)).ToList(); ExtraProperties = entityChangeInfo.ExtraProperties.ToDictionary(pair => pair.Key, pair => pair.Value); } diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityPropertyChange.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityPropertyChange.cs index c85f5c93b2..7cd11797a5 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityPropertyChange.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/EntityPropertyChange.cs @@ -31,10 +31,10 @@ namespace Volo.Abp.AuditLogging Id = guidGenerator.Create(); TenantId = entityChangeInfo.TenantId; EntityChangeId = entityChangeId; - NewValue = entityChangeInfo.NewValue; - OriginalValue = entityChangeInfo.OriginalValue; - PropertyName = entityChangeInfo.PropertyName; - PropertyTypeFullName = entityChangeInfo.PropertyTypeFullName; + NewValue = entityChangeInfo.NewValue.Truncate(EntityPropertyChangeConsts.MaxNewValueLength); + OriginalValue = entityChangeInfo.OriginalValue.Truncate(EntityPropertyChangeConsts.MaxOriginalValueLength); + PropertyName = entityChangeInfo.PropertyName.TruncateFromBeginning(EntityPropertyChangeConsts.MaxPropertyNameLength); + PropertyTypeFullName = entityChangeInfo.PropertyTypeFullName.TruncateFromBeginning(EntityPropertyChangeConsts.MaxPropertyTypeFullNameLength); } } } \ No newline at end of file