Skip to content

Commit 8844f5e

Browse files
Add own method
Add own method for getting `[JsonPropertyName]` instead of mingling with `[Display]`.
1 parent 6bb9f11 commit 8844f5e

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Validation/gen/Extensions/ISymbolExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ attribute.AttributeClass is { } attributeClass &&
1919

2020
if (displayNameAttribute is not null)
2121
{
22-
// For the [Foo(Name = "bar")] case
2322
if (!displayNameAttribute.NamedArguments.IsDefaultOrEmpty)
2423
{
2524
foreach (var namedArgument in displayNameAttribute.NamedArguments)
@@ -33,8 +32,20 @@ attribute.AttributeClass is { } attributeClass &&
3332
}
3433
}
3534
}
35+
}
36+
37+
return null;
38+
}
3639

37-
// For the [Foo("bar")] case
40+
public static string? GetJsonPropertyName(this ISymbol property, INamedTypeSymbol jsonPropertyNameAttribute)
41+
{
42+
var displayNameAttribute = property.GetAttributes()
43+
.FirstOrDefault(attribute =>
44+
attribute.AttributeClass is { } attributeClass &&
45+
SymbolEqualityComparer.Default.Equals(attributeClass, jsonPropertyNameAttribute));
46+
47+
if (displayNameAttribute is not null)
48+
{
3849
if (displayNameAttribute.ConstructorArguments.Length is 1)
3950
{
4051
var arg = displayNameAttribute.ConstructorArguments[0];

src/Validation/gen/Parsers/ValidationsGenerator.TypesParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb
196196

197197
var displayName =
198198
parameter.GetDisplayName(wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_ComponentModel_DataAnnotations_DisplayAttribute)) ??
199-
parameter.GetDisplayName(wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_Text_Json_Serialization_JsonPropertyNameAttribute)) ??
199+
parameter.GetJsonPropertyName(wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_Text_Json_Serialization_JsonPropertyNameAttribute)) ??
200200
correspondingProperty.GetDisplayName(wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_ComponentModel_DataAnnotations_DisplayAttribute)) ??
201-
correspondingProperty.GetDisplayName(wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_Text_Json_Serialization_JsonPropertyNameAttribute)) ??
201+
correspondingProperty.GetJsonPropertyName(wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_Text_Json_Serialization_JsonPropertyNameAttribute)) ??
202202
parameter.Name ??
203203
correspondingProperty.Name;
204204

@@ -260,7 +260,7 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb
260260

261261
var displayName =
262262
member.GetDisplayName(wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_ComponentModel_DataAnnotations_DisplayAttribute)) ??
263-
member.GetDisplayName(wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_Text_Json_Serialization_JsonPropertyNameAttribute)) ??
263+
member.GetJsonPropertyName(wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_Text_Json_Serialization_JsonPropertyNameAttribute)) ??
264264
member.Name;
265265

266266
members.Add(new ValidatableProperty(

0 commit comments

Comments
 (0)