From 2e3e43ae3c678e11a2ac49f09d220fa0f560e9ee Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Mon, 12 May 2025 12:56:19 -0700 Subject: [PATCH 1/2] Fix handling for Name property on DisplayAttribute --- .../Extensions/ISymbolExtensions.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.ValidationsGenerator/Extensions/ISymbolExtensions.cs b/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.ValidationsGenerator/Extensions/ISymbolExtensions.cs index 54efe204c1ec..86eb4e50a43a 100644 --- a/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.ValidationsGenerator/Extensions/ISymbolExtensions.cs +++ b/src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.ValidationsGenerator/Extensions/ISymbolExtensions.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; using System.Linq; using Microsoft.CodeAnalysis; @@ -14,17 +15,19 @@ public static string GetDisplayName(this ISymbol property, INamedTypeSymbol disp .FirstOrDefault(attribute => attribute.AttributeClass is { } attributeClass && SymbolEqualityComparer.Default.Equals(attributeClass, displayAttribute)); + if (displayNameAttribute is not null) { - if (displayNameAttribute.ConstructorArguments.Length > 0) - { - return displayNameAttribute.ConstructorArguments[0].Value?.ToString() ?? property.Name; - } - else if (displayNameAttribute.NamedArguments.Length > 0) + if (!displayNameAttribute.NamedArguments.IsDefaultOrEmpty) { - return displayNameAttribute.NamedArguments[0].Value.Value?.ToString() ?? property.Name; + foreach (var namedArgument in displayNameAttribute.NamedArguments) + { + if (string.Equals(namedArgument.Key, "Name", StringComparison.Ordinal)) + { + return namedArgument.Value.Value?.ToString() ?? property.Name; + } + } } - return property.Name; } return property.Name; From 565045103194462e5594273ce91e0144ecfde4df Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Mon, 12 May 2025 17:44:35 -0700 Subject: [PATCH 2/2] Add test for negative case --- .../ValidationsGenerator.IValidatableObject.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Http/Http.Extensions/test/ValidationsGenerator/ValidationsGenerator.IValidatableObject.cs b/src/Http/Http.Extensions/test/ValidationsGenerator/ValidationsGenerator.IValidatableObject.cs index 30de31e208b0..d50498d4a997 100644 --- a/src/Http/Http.Extensions/test/ValidationsGenerator/ValidationsGenerator.IValidatableObject.cs +++ b/src/Http/Http.Extensions/test/ValidationsGenerator/ValidationsGenerator.IValidatableObject.cs @@ -54,6 +54,8 @@ public IEnumerable Validate(ValidationContext validationContex public class SubType { [Required] + // This gets ignored since it has an unsupported constructor name + [Display(ShortName = "SubType")] public string RequiredProperty { get; set; } = "some-value"; [StringLength(10)]