Browse Source

Merge pull request #21675 from abpframework/CachedObjectExtensionsDtoService

Return `EnumValues` for `nullable enum`.
pull/21712/head
sumeyye 2 years ago
committed by GitHub
parent
commit
e29e4a1463
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 27
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApplicationConfigurations/ObjectExtending/CachedObjectExtensionsDtoService.cs
  2. 12
      framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs

27
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
}
);

12
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;
}

Loading…
Cancel
Save