Skip to content

Commit 51614a0

Browse files
Fix ordering
- Check for `[Display]` before any `[JsonPropertyName]`. - Add test case for records.
1 parent 35dde07 commit 51614a0

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ internal ImmutableArray<ValidatableProperty> ExtractValidatableMembers(ITypeSymb
196196

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

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.ComplexType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public async Task CanValidateComplexTypesWithJsonIgnore()
1515
using System;
1616
using System.ComponentModel.DataAnnotations;
1717
using System.Collections.Generic;
18+
using System.Text.Json.Serialization;
1819
using System.Threading.Tasks;
1920
using Microsoft.AspNetCore.Builder;
2021
using Microsoft.AspNetCore.Http;
2122
using Microsoft.Extensions.Validation;
2223
using Microsoft.AspNetCore.Routing;
2324
using Microsoft.Extensions.DependencyInjection;
2425
using Microsoft.AspNetCore.Mvc;
25-
using System.Text.Json.Serialization;
2626
2727
var builder = WebApplication.CreateBuilder();
2828

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.RecordType.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public async Task CanValidateRecordTypes()
1515
using System;
1616
using System.ComponentModel.DataAnnotations;
1717
using System.Collections.Generic;
18+
using System.Text.Json.Serialization;
1819
using System.Threading.Tasks;
1920
using Microsoft.AspNetCore.Builder;
2021
using Microsoft.AspNetCore.Http;
@@ -31,6 +32,7 @@ public async Task CanValidateRecordTypes()
3132
var app = builder.Build();
3233
3334
app.MapPost("/validatable-record", (ValidatableRecord validatableRecord) => Results.Ok("Passed"!));
35+
app.MapPost("/validatable-record-with-json-property-name", (ValidatableRecordWithJsonPropertyNames validatableRecord) => Results.Ok("Passed"!));
3436
3537
app.Run();
3638
@@ -95,7 +97,20 @@ public record ValidatableRecord(
9597
[DerivedValidation, Range(10, 100)]
9698
int PropertyWithMultipleAttributes = 10,
9799
[FromServices] [Required] TestService ServiceProperty = null!, // This should be ignored because of [FromServices]
98-
[FromKeyedServices("serviceKey")] [Range(10, 100)] int KeyedServiceProperty = 5 // This should be ignored because of [FromKeyedServices]
100+
[FromKeyedServices("serviceKey")] [Range(10, 100)] int KeyedServiceProperty = 5, // This should be ignored because of [FromKeyedServices]
101+
[Display(Name = "display-name")] [Range(10, 100)] int IntegerWithRangeAndDisplay = 10
102+
);
103+
104+
public record ValidatableRecordWithJsonPropertyNames(
105+
[Range(10, 100)]
106+
int DefaultPropertyName = 10,
107+
[Range(10, 100)]
108+
[property: JsonPropertyName("custom-property-name")]
109+
int CustomJsonPropertyName = 20,
110+
[Display(Name = "display-name")]
111+
[Range(10, 100)]
112+
[property: JsonPropertyName("custom-property-name-with-display-name")]
113+
int CustomJsonPropertyNameWithDisplayName = 30
99114
);
100115
""";
101116
await Verify(source, out var compilation);

src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/snapshots/ValidationsGeneratorTests.CanValidateRecordTypes#ValidatableInfoResolver.g.verified.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,39 @@ public bool TryGetValidatableTypeInfo(global::System.Type type, [global::System.
193193
name: "PropertyWithMultipleAttributes",
194194
displayName: "PropertyWithMultipleAttributes"
195195
),
196+
new GeneratedValidatablePropertyInfo(
197+
containingType: typeof(global::ValidatableRecord),
198+
propertyType: typeof(int),
199+
name: "IntegerWithRangeAndDisplay",
200+
displayName: "display-name"
201+
),
202+
]
203+
);
204+
return true;
205+
}
206+
if (type == typeof(global::ValidatableRecordWithJsonPropertyNames))
207+
{
208+
validatableInfo = new GeneratedValidatableTypeInfo(
209+
type: typeof(global::ValidatableRecordWithJsonPropertyNames),
210+
members: [
211+
new GeneratedValidatablePropertyInfo(
212+
containingType: typeof(global::ValidatableRecordWithJsonPropertyNames),
213+
propertyType: typeof(int),
214+
name: "DefaultPropertyName",
215+
displayName: "DefaultPropertyName"
216+
),
217+
new GeneratedValidatablePropertyInfo(
218+
containingType: typeof(global::ValidatableRecordWithJsonPropertyNames),
219+
propertyType: typeof(int),
220+
name: "CustomJsonPropertyName",
221+
displayName: "custom-property-name"
222+
),
223+
new GeneratedValidatablePropertyInfo(
224+
containingType: typeof(global::ValidatableRecordWithJsonPropertyNames),
225+
propertyType: typeof(int),
226+
name: "CustomJsonPropertyNameWithDisplayName",
227+
displayName: "display-name"
228+
),
196229
]
197230
);
198231
return true;

0 commit comments

Comments
 (0)