Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return EnumValues for nullable enum. #21675

Open
wants to merge 3 commits into
base: rel-9.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,19 @@ protected virtual void FillEnums(ObjectExtensionsDto objectExtensionsDto)
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);
}
}
}

Expand All @@ -260,12 +266,23 @@ protected virtual ExtensionEnumDto CreateExtensionEnumDto(ExtensionPropertyConfi
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
}
);
Expand Down
12 changes: 12 additions & 0 deletions framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public static bool IsNullable(Type type)
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<>))
Expand Down Expand Up @@ -309,6 +317,10 @@ public static string GetSimplifiedName([NotNull] Type type)
{
return "object";
}
else if (type.IsEnum)
{
return "enum";
}

return type.FullName ?? type.Name;
}
Expand Down
Loading