Skip to content

DisplayAttribute is not public #889

Closed

Description

Hi!

I'm writing an OpenApiOperation extension using enum and I've noticed that it has a method named GetDisplayName to use a display name instead of a enum name.

public static string GetDisplayName(this Enum enumValue)

        /// <summary>
        /// Gets the enum display name.
        /// </summary>
        /// <param name="enumValue">The enum value.</param>
        /// <returns>
        /// Use <see cref="DisplayAttribute"/> if exists.
        /// Otherwise, use the standard string representation.
        /// </returns>

But, DisplayAttribute is a private attribute and it's not possible to use them.

internal class DisplayAttribute : Attribute

Is it possible to change this attribute to be public instead internal class?

Example of use:

   //my enum, used on docOptions
    public enum ApiLevel
    {
        [DisplayAttribute(Name = "private")]
        Privada = 1,
        [Display(Name = "public")]
        Publica = 2,
        [Display(Name = "corporate")]        
        Corporativa = 3
    }

//my filter where i use the enum
    internal class OpenApiExampleFilter : IOperationFilter
    {
        private readonly DocOptions _docOptions;

        public OpenApiExampleFilter (DocOptions docOptions)
        {
            _docOptions = docOptions;
        }

        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {           
            operation.AddExtension("x-test", new OpenApiObject
            {
                ["apiLevel"] = new OpenApiString(_docOptions.ApiLevel.GetDisplayName()), //here i cant use GetDisplayName() :(
                ["apiDomain"] = new OpenApiString(_docOptions.ApiDomain)
            });
        }
    }

Tks!

Activity

  1. darrelmiller commented on Jun 17, 2022

    @darrelmiller
    Member

    That seems like a reasonable request to me.

  2. added this to the NET:1.4 milestone on Jul 4, 2022
  3. CodeCasterNL commented on Aug 4, 2022

    @CodeCasterNL

    Darrel (@darrelmiller) any reason not to use System.ComponentModel.DataAnnotations.DisplayAttribute but to roll your own?

    Perhaps remove it and use the BCL one? This library targets NETStandard 2.0, which covers the existing attribute (.NET Framework 4+, .NET Core 1+).

  4. fernandolamp commented on Aug 4, 2022

    @fernandolamp
    Author

    Darrel (@darrelmiller) any reason not to use System.ComponentModel.DataAnnotations.DisplayAttribufe?

    This make sense. I can change if Darrel (@darrelmiller) agrees.

  5. CodeCasterNL commented on Aug 5, 2022

    @CodeCasterNL

    On the other hand, one might want to differ between DataAnnotations.Display for MVC purposes, and OpenApi.Attributes.Display for OpenAPI descriptions, I think? Not sure what the intended usage for the attribute is, other than to annotate your classes exposed from your API?

  6. fernandolamp commented on Aug 10, 2022

    @fernandolamp
    Author

    On the other hand, one might want to differ between DataAnnotations.Display for MVC purposes, and OpenApi.Attributes.Display for OpenAPI descriptions, I think? Not sure what the intended usage for the attribute is, other than to annotate your classes exposed from your API?

    I was analyzing the code. Display attribute is used to standardize the display name of the enum.

    image

    And get enum from display name on StringExtensions

    public static T GetEnumFromDisplayName<T>(this string displayName)

    In my code i'm using to solve the same problem, i need to expose this enums using lower case and translated.

    Both attribute is used to the same thing, but to use from System.ComponentModel.DataAnnotations is necessary install the package System.ComponentModel.Annotations.

    I don't know if reference this package in this project is a problem. If not, this change make sense and is simple.

    I made this change in my local branch and all test have passed, i can push if there is no problem.

  7. CodeCasterNL commented on Aug 14, 2022

    @CodeCasterNL

    Maggie Kimani (@MaggieKimani1) or Darrel (@darrelmiller) can you chime in on the discussion? Does the attribute need to exist in the first place? Less code being more and all.

  8. darrelmiller commented on Aug 16, 2022

    @darrelmiller
    Member

    CodeCasterNL I agree less code is more, and the problem with System.ComponentModel.DataAnnotations is that it pulls in all kinds other code that we don't use and don't support. The DisplayAttribute class is only a few lines of code. I'd rather not take another dependency for just this small feature.

  9. CodeCasterNL commented on Aug 16, 2022

    @CodeCasterNL

    Darrel (@darrelmiller) sorry, I meant System.ComponentModel.DisplayNameAttribute all along, which is covered in netstandard2.0. You're right in that referencing the package System.ComponentModel.DataAnnotations would be overkill to remove like ten lines of code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:enhancementEnhancement request targeting an existing experience

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions