@@ -21,10 +21,12 @@ public async Task CanValidateRecordTypes()
2121using Microsoft.Extensions.Validation;
2222using Microsoft.AspNetCore.Routing;
2323using Microsoft.Extensions.DependencyInjection;
24+ using Microsoft.AspNetCore.Mvc;
2425
2526var builder = WebApplication.CreateBuilder();
2627
2728builder.Services.AddValidation();
29+ builder.Services.AddSingleton<TestService>();
2830
2931var app = builder.Build();
3032
@@ -66,6 +68,12 @@ public static ValidationResult Validate(int number, ValidationContext validation
6668 }
6769}
6870
71+ public class TestService
72+ {
73+ [Range(10, 100)]
74+ public int Value { get; set; } = 4;
75+ }
76+
6977public record ValidatableRecord(
7078 [Range(10, 100)]
7179 int IntegerWithRange = 10,
@@ -81,7 +89,9 @@ public record ValidatableRecord(
8189 [CustomValidation(typeof(CustomValidators), nameof(CustomValidators.Validate))]
8290 int IntegerWithCustomValidation = 0,
8391 [DerivedValidation, Range(10, 100)]
84- int PropertyWithMultipleAttributes = 10
92+ int PropertyWithMultipleAttributes = 10,
93+ [FromServices] [Required] TestService ServiceProperty = null!, // This should be ignored because of [FromServices]
94+ [FromKeyedServices("serviceKey")] [Range(10, 100)] int KeyedServiceProperty = 5 // This should be ignored because of [FromKeyedServices]
8595);
8696""" ;
8797 await Verify ( source , out var compilation ) ;
@@ -370,86 +380,4 @@ async Task ValidInputProducesNoWarnings(Endpoint endpoint)
370380 } ) ;
371381
372382 }
373-
374- [ Fact ]
375- public async Task DoesNotValidateRecordPropertiesWithFromServicesAttribute ( )
376- {
377- // Arrange
378- var source = """
379- using System;
380- using System.ComponentModel.DataAnnotations;
381- using System.Collections.Generic;
382- using System.Threading.Tasks;
383- using Microsoft.AspNetCore.Builder;
384- using Microsoft.AspNetCore.Http;
385- using Microsoft.Extensions.Validation;
386- using Microsoft.AspNetCore.Routing;
387- using Microsoft.Extensions.DependencyInjection;
388- using Microsoft.AspNetCore.Mvc;
389-
390- var builder = WebApplication.CreateBuilder();
391-
392- builder.Services.AddValidation();
393- builder.Services.AddSingleton<TestService>();
394-
395- var app = builder.Build();
396-
397- app.MapPost("/with-from-services-record", ([AsParameters] ComplexRecordWithFromServices complexType) => Results.Ok("Passed"!));
398-
399- app.Run();
400-
401- public record ComplexRecordWithFromServices(
402- [Range(10, 100)] int ValidatableProperty,
403- [FromServices] [Required] TestService ServiceProperty, // This should be ignored because of [FromServices]
404- [FromKeyedServices("serviceKey")] [Range(10, 100)] int KeyedServiceProperty // This should be ignored because of [FromKeyedServices]
405- );
406-
407- public class TestService
408- {
409- [Range(10, 100)]
410- public int Value { get; set; } = 4;
411- }
412- """ ;
413- await Verify ( source , out var compilation ) ;
414- await VerifyEndpoint ( compilation , "/with-from-services-record" , async ( endpoint , serviceProvider ) =>
415- {
416- await ValidInputWithFromServicesProducesNoWarnings ( endpoint ) ;
417- await InvalidValidatablePropertyProducesError ( endpoint ) ;
418-
419- async Task ValidInputWithFromServicesProducesNoWarnings ( Endpoint endpoint )
420- {
421- var payload = """
422- {
423- "ValidatableProperty": 50,
424- "ServiceProperty": null,
425- "KeyedServiceProperty": 5
426- }
427- """ ;
428- var context = CreateHttpContextWithPayload ( payload , serviceProvider ) ;
429- await endpoint . RequestDelegate ( context ) ;
430-
431- Assert . Equal ( 200 , context . Response . StatusCode ) ;
432- }
433-
434- async Task InvalidValidatablePropertyProducesError ( Endpoint endpoint )
435- {
436- var payload = """
437- {
438- "ValidatableProperty": 5,
439- "ServiceProperty": null,
440- "KeyedServiceProperty": 5
441- }
442- """ ;
443- var context = CreateHttpContextWithPayload ( payload , serviceProvider ) ;
444- await endpoint . RequestDelegate ( context ) ;
445-
446- var problemDetails = await AssertBadRequest ( context ) ;
447- Assert . Collection ( problemDetails . Errors , kvp =>
448- {
449- Assert . Equal ( "ValidatableProperty" , kvp . Key ) ;
450- Assert . Equal ( "The field ValidatableProperty must be between 10 and 100." , kvp . Value . Single ( ) ) ;
451- } ) ;
452- }
453- } ) ;
454- }
455383}
0 commit comments