Skip to content

Commit de037b2

Browse files
authored
Fix handling for Name property on DisplayAttribute (#61888)
* Fix handling for Name property on DisplayAttribute * Add test for negative case
1 parent dcc31e1 commit de037b2

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.ValidationsGenerator/Extensions/ISymbolExtensions.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Linq;
56
using Microsoft.CodeAnalysis;
67

@@ -14,17 +15,19 @@ public static string GetDisplayName(this ISymbol property, INamedTypeSymbol disp
1415
.FirstOrDefault(attribute =>
1516
attribute.AttributeClass is { } attributeClass &&
1617
SymbolEqualityComparer.Default.Equals(attributeClass, displayAttribute));
18+
1719
if (displayNameAttribute is not null)
1820
{
19-
if (displayNameAttribute.ConstructorArguments.Length > 0)
20-
{
21-
return displayNameAttribute.ConstructorArguments[0].Value?.ToString() ?? property.Name;
22-
}
23-
else if (displayNameAttribute.NamedArguments.Length > 0)
21+
if (!displayNameAttribute.NamedArguments.IsDefaultOrEmpty)
2422
{
25-
return displayNameAttribute.NamedArguments[0].Value.Value?.ToString() ?? property.Name;
23+
foreach (var namedArgument in displayNameAttribute.NamedArguments)
24+
{
25+
if (string.Equals(namedArgument.Key, "Name", StringComparison.Ordinal))
26+
{
27+
return namedArgument.Value.Value?.ToString() ?? property.Name;
28+
}
29+
}
2630
}
27-
return property.Name;
2831
}
2932

3033
return property.Name;

src/Http/Http.Extensions/test/ValidationsGenerator/ValidationsGenerator.IValidatableObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
5454
public class SubType
5555
{
5656
[Required]
57+
// This gets ignored since it has an unsupported constructor name
58+
[Display(ShortName = "SubType")]
5759
public string RequiredProperty { get; set; } = "some-value";
5860
5961
[StringLength(10)]

0 commit comments

Comments
 (0)