Skip to content

Commit d7ad03c

Browse files
Copilotcaptainsafia
andcommitted
Update tests to use existing types with FromServices attributes
Co-authored-by: captainsafia <[email protected]>
1 parent 2ebdeba commit d7ad03c

File tree

3 files changed

+315
-173
lines changed

3 files changed

+315
-173
lines changed

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

Lines changed: 12 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ public async Task CanValidateComplexTypes()
2121
using Microsoft.Extensions.Validation;
2222
using Microsoft.AspNetCore.Routing;
2323
using Microsoft.Extensions.DependencyInjection;
24+
using Microsoft.AspNetCore.Mvc;
2425
2526
var builder = WebApplication.CreateBuilder();
2627
2728
builder.Services.AddValidation();
29+
builder.Services.AddSingleton<TestService>();
2830
2931
var app = builder.Build();
3032
@@ -58,6 +60,10 @@ public class ComplexType
5860
5961
[DerivedValidation, Range(10, 100)]
6062
public int PropertyWithMultipleAttributes { get; set; } = 10;
63+
64+
[FromServices]
65+
[Required] // This should be ignored because of [FromServices]
66+
public TestService ServiceProperty { get; set; } = null!;
6167
}
6268
6369
public class DerivedValidationAttribute : ValidationAttribute
@@ -96,6 +102,12 @@ public static ValidationResult Validate(int number, ValidationContext validation
96102
return ValidationResult.Success;
97103
}
98104
}
105+
106+
public class TestService
107+
{
108+
[Range(10, 100)]
109+
public int Value { get; set; } = 4;
110+
}
99111
""";
100112
await Verify(source, out var compilation);
101113
await VerifyEndpoint(compilation, "/complex-type", async (endpoint, serviceProvider) =>
@@ -373,94 +385,4 @@ async Task ValidInputProducesNoWarnings(Endpoint endpoint)
373385
}
374386
});
375387
}
376-
377-
[Fact]
378-
public async Task DoesNotValidatePropertiesWithFromServicesAttribute()
379-
{
380-
// Arrange
381-
var source = """
382-
using System;
383-
using System.ComponentModel.DataAnnotations;
384-
using System.Collections.Generic;
385-
using System.Threading.Tasks;
386-
using Microsoft.AspNetCore.Builder;
387-
using Microsoft.AspNetCore.Http;
388-
using Microsoft.Extensions.Validation;
389-
using Microsoft.AspNetCore.Routing;
390-
using Microsoft.Extensions.DependencyInjection;
391-
using Microsoft.AspNetCore.Mvc;
392-
393-
var builder = WebApplication.CreateBuilder();
394-
395-
builder.Services.AddValidation();
396-
builder.Services.AddSingleton<TestService>();
397-
398-
var app = builder.Build();
399-
400-
app.MapPost("/with-from-services", ([AsParameters] ComplexTypeWithFromServices complexType) => Results.Ok("Passed"!));
401-
402-
app.Run();
403-
404-
public class ComplexTypeWithFromServices
405-
{
406-
[Range(10, 100)]
407-
public int ValidatableProperty { get; set; } = 10;
408-
409-
[FromServices]
410-
[Required] // This should be ignored because of [FromServices]
411-
public TestService ServiceProperty { get; set; } = null!;
412-
413-
[FromKeyedServices("serviceKey")]
414-
[Range(10, 100)] // This should be ignored because of [FromKeyedServices]
415-
public int KeyedServiceProperty { get; set; } = 5;
416-
}
417-
418-
public class TestService
419-
{
420-
[Range(10, 100)]
421-
public int Value { get; set; } = 4;
422-
}
423-
""";
424-
await Verify(source, out var compilation);
425-
await VerifyEndpoint(compilation, "/with-from-services", async (endpoint, serviceProvider) =>
426-
{
427-
await ValidInputWithFromServicesProducesNoWarnings(endpoint);
428-
await InvalidValidatablePropertyProducesError(endpoint);
429-
430-
async Task ValidInputWithFromServicesProducesNoWarnings(Endpoint endpoint)
431-
{
432-
var payload = """
433-
{
434-
"ValidatableProperty": 50,
435-
"ServiceProperty": null,
436-
"KeyedServiceProperty": 5
437-
}
438-
""";
439-
var context = CreateHttpContextWithPayload(payload, serviceProvider);
440-
await endpoint.RequestDelegate(context);
441-
442-
Assert.Equal(200, context.Response.StatusCode);
443-
}
444-
445-
async Task InvalidValidatablePropertyProducesError(Endpoint endpoint)
446-
{
447-
var payload = """
448-
{
449-
"ValidatableProperty": 5,
450-
"ServiceProperty": null,
451-
"KeyedServiceProperty": 5
452-
}
453-
""";
454-
var context = CreateHttpContextWithPayload(payload, serviceProvider);
455-
await endpoint.RequestDelegate(context);
456-
457-
var problemDetails = await AssertBadRequest(context);
458-
Assert.Collection(problemDetails.Errors, kvp =>
459-
{
460-
Assert.Equal("ValidatableProperty", kvp.Key);
461-
Assert.Equal("The field ValidatableProperty must be between 10 and 100.", kvp.Value.Single());
462-
});
463-
}
464-
});
465-
}
466388
}

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

Lines changed: 11 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ public async Task CanValidateRecordTypes()
2121
using Microsoft.Extensions.Validation;
2222
using Microsoft.AspNetCore.Routing;
2323
using Microsoft.Extensions.DependencyInjection;
24+
using Microsoft.AspNetCore.Mvc;
2425
2526
var builder = WebApplication.CreateBuilder();
2627
2728
builder.Services.AddValidation();
29+
builder.Services.AddSingleton<TestService>();
2830
2931
var 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+
6977
public 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

Comments
 (0)