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 1 commit into
base: rel-9.0
Choose a base branch
from

Conversation

maliming
Copy link
Member

@maliming maliming commented Dec 19, 2024

image

The temporary solution is to replace the CachedObjectExtensionsDtoService:

using System;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ObjectExtending;
using Volo.Abp.DependencyInjection;
using Volo.Abp.ObjectExtending;
using Volo.Abp.ObjectExtending.Modularity;

namespace MyCompanyName.MyProjectName.Web;

[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(MyCachedObjectExtensionsDtoService), typeof(CachedObjectExtensionsDtoService), typeof(ICachedObjectExtensionsDtoService))]
public class MyCachedObjectExtensionsDtoService : CachedObjectExtensionsDtoService
{
    public MyCachedObjectExtensionsDtoService(IExtensionPropertyAttributeDtoFactory extensionPropertyAttributeDtoFactory)
        : base(extensionPropertyAttributeDtoFactory)
    {

    }

    protected override void FillEnums(ObjectExtensionsDto objectExtensionsDto)
    {
        var enumProperties = ObjectExtensionManager.Instance.Modules().Values
            .SelectMany(
                m => m.Entities.Values.SelectMany(
                    e => e.GetProperties()
                )
            )
            .Where(p => p.Type.IsEnum || 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);
        }
    }

    protected override ExtensionEnumDto CreateExtensionEnumDto(ExtensionPropertyConfiguration enumProperty)
    {
        var extensionEnumDto = new ExtensionEnumDto
        {
            Fields = new List<ExtensionEnumFieldDto>(),
            LocalizationResource = enumProperty.GetLocalizationResourceNameOrNull()
        };

        var enumType = enumProperty.Type.IsEnum
            ? enumProperty.Type
            : 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 = enumType.GetEnumName(enumValue)!,
                    Value = enumValue
                }
            );
        }

        return extensionEnumDto;
    }

    public bool IsNullableEnum(Type type)
    {
        return type.IsGenericType &&
               type.GetGenericTypeDefinition() == typeof(Nullable<>) &&
               type.GenericTypeArguments.Length == 1 &&
               type.GenericTypeArguments[0].IsEnum;
    }
}

@maliming maliming added this to the 9.0-patch milestone Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants