diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ObjectExtending/CachedObjectExtensionsDtoService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ObjectExtending/CachedObjectExtensionsDtoService.cs index 10900f6ee4..097fd899a5 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ObjectExtending/CachedObjectExtensionsDtoService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ObjectExtending/CachedObjectExtensionsDtoService.cs @@ -242,13 +242,19 @@ public class CachedObjectExtensionsDtoService : ICachedObjectExtensionsDtoServic e => e.GetProperties() ) ) - .Where(p => p.Type.IsEnum) + .Where(p => p.Type.IsEnum || TypeHelper.IsNullableEnum(p.Type)) .ToList(); foreach (var enumProperty in enumProperties) { - // ReSharper disable once AssignNullToNotNullAttribute (enumProperty.Type.FullName can not be null for this case) - objectExtensionsDto.Enums[enumProperty.Type.FullName!] = CreateExtensionEnumDto(enumProperty); + if (TypeHelper.IsNullableEnum(enumProperty.Type)) + { + objectExtensionsDto.Enums[Nullable.GetUnderlyingType(enumProperty.Type)!.FullName + "?"] = CreateExtensionEnumDto(enumProperty); + } + else + { + objectExtensionsDto.Enums[enumProperty.Type.FullName!] = CreateExtensionEnumDto(enumProperty); + } } } @@ -260,12 +266,23 @@ public class CachedObjectExtensionsDtoService : ICachedObjectExtensionsDtoServic LocalizationResource = enumProperty.GetLocalizationResourceNameOrNull() }; - foreach (var enumValue in enumProperty.Type.GetEnumValues()) + var enumType = enumProperty.Type.IsEnum + ? enumProperty.Type + : TypeHelper.IsNullableEnum(enumProperty.Type) + ? Nullable.GetUnderlyingType(enumProperty.Type) + : null; + + if (enumType == null) + { + return extensionEnumDto; + } + + foreach (var enumValue in enumType.GetEnumValues()) { extensionEnumDto.Fields.Add( new ExtensionEnumFieldDto { - Name = enumProperty.Type.GetEnumName(enumValue)!, + Name = enumType.GetEnumName(enumValue)!, Value = enumValue } ); 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 1efa6c4f29..f9fdea3e50 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs @@ -84,6 +84,14 @@ public static class TypeHelper return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); } + public static bool IsNullableEnum(Type type) + { + return type.IsGenericType && + type.GetGenericTypeDefinition() == typeof(Nullable<>) && + type.GenericTypeArguments.Length == 1 && + type.GenericTypeArguments[0].IsEnum; + } + public static Type GetFirstGenericArgumentIfNullable(this Type t) { if (t.GetGenericArguments().Length > 0 && t.GetGenericTypeDefinition() == typeof(Nullable<>)) @@ -309,6 +317,10 @@ public static class TypeHelper { return "object"; } + else if (type.IsEnum) + { + return "enum"; + } return type.FullName ?? type.Name; }