Skip to content

Commit df27fe7

Browse files
committed
Fix handling of primitive types in RuntimeValidatableParameterInfoResolver
1 parent f0a43e8 commit df27fe7

File tree

3 files changed

+11
-25
lines changed

3 files changed

+11
-25
lines changed

src/Http/Http.Abstractions/src/Validation/RuntimeValidatableParameterInfoResolver.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace Microsoft.AspNetCore.Http.Validation;
1414

1515
internal sealed class RuntimeValidatableParameterInfoResolver : IValidatableInfoResolver
1616
{
17-
// TODO: the implementation currently relies on static discovery of types.
17+
// TODO: The implementation currently relies on static discovery of types.
18+
// See https://github.com/dotnet/aspnetcore/issues/61220
1819
public bool TryGetValidatableTypeInfo(Type type, [NotNullWhen(true)] out IValidatableInfo? validatableInfo)
1920
{
2021
validatableInfo = null;
@@ -76,17 +77,7 @@ private static bool IsClass(Type type)
7677
{
7778
// Skip primitives, enums, common built-in types, and types that are specially
7879
// handled by RDF/RDG that don't need validation if they don't have attributes
79-
if (type.IsPrimitive ||
80-
type.IsEnum ||
81-
type == typeof(string) ||
82-
type == typeof(decimal) ||
83-
type == typeof(DateTime) ||
84-
type == typeof(DateTimeOffset) ||
85-
type == typeof(TimeOnly) ||
86-
type == typeof(DateOnly) ||
87-
type == typeof(TimeSpan) ||
88-
type == typeof(Guid) ||
89-
type == typeof(IFormFile) ||
80+
if (type == typeof(IFormFile) ||
9081
type == typeof(IFormFileCollection) ||
9182
type == typeof(IFormCollection) ||
9283
type == typeof(HttpContext) ||

src/Http/Http.Abstractions/test/Validation/RuntimeValidatableParameterInfoResolverTests.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,8 @@ public void TryGetValidatableParameterInfo_WithNullName_ThrowsInvalidOperationEx
3333
}
3434

3535
[Theory]
36-
[InlineData(typeof(string))]
37-
[InlineData(typeof(int))]
38-
[InlineData(typeof(bool))]
39-
[InlineData(typeof(DateTime))]
40-
[InlineData(typeof(Guid))]
41-
[InlineData(typeof(decimal))]
42-
[InlineData(typeof(DayOfWeek))] // Enum
4336
[InlineData(typeof(ClaimsPrincipal))]
4437
[InlineData(typeof(PipeReader))]
45-
[InlineData(typeof(DateTimeOffset))]
46-
[InlineData(typeof(TimeOnly))]
47-
[InlineData(typeof(DateOnly))]
48-
[InlineData(typeof(TimeSpan))]
4938
[InlineData(typeof(IFormFile))]
5039
[InlineData(typeof(IFormFileCollection))]
5140
[InlineData(typeof(IFormCollection))]

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public async Task CanValidateParameters()
3939
[CustomValidation(ErrorMessage = "Value must be an even number")] int value4 = 4,
4040
[CustomValidation, Range(10, 100)] int value5 = 10,
4141
// Skipped from validation because it is marked as a [FromService] parameter
42-
[FromServices] [Range(10, 100)] int? value6 = 4) => "OK");
42+
[FromServices] [Range(10, 100)] int? value6 = 4,
43+
[MinLength(7)] string? value7 = "LongEnough") => "OK");
4344
4445
app.Run();
4546
@@ -58,7 +59,7 @@ public class TestService
5859
await VerifyEndpoint(compilation, "/params", async (endpoint, serviceProvider) =>
5960
{
6061
var context = CreateHttpContext(serviceProvider);
61-
context.Request.QueryString = new QueryString("?value1=5&value2=5&value3=&value4=3&value5=5");
62+
context.Request.QueryString = new QueryString("?value1=5&value2=5&value3=&value4=3&value5=5&value7=Short");
6263
await endpoint.RequestDelegate(context);
6364
var problemDetails = await AssertBadRequest(context);
6465
Assert.Collection(problemDetails.Errors,
@@ -93,6 +94,11 @@ await VerifyEndpoint(compilation, "/params", async (endpoint, serviceProvider) =
9394
{
9495
Assert.Equal("The field value5 must be between 10 and 100.", error);
9596
});
97+
},
98+
error =>
99+
{
100+
Assert.Equal("value7", error.Key);
101+
Assert.Equal("The field value7 must be a string or array type with a minimum length of '7'.", error.Value.Single());
96102
});
97103
});
98104
}

0 commit comments

Comments
 (0)