runtime: DataAnnotations validation not supported for properties of nested values.
Description
I have a polymorphic class like:
public record Request
{
public required Base Prop { get; init; }
}
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(Derived), "derived")]
public abstract record Base
{
}
public record Derived : Base
{
[Required, StringLength(1)]
public string? Field { get; init; }
}
and 2 methods
[HttpPost("test-property")]
public void Test(Request request)
{
}
[HttpPost("test-request")]
public void Test(Base request)
{
}
This request to “test-property” results in 200 (which is incorrect since the field “field” is not specified):
{
"prop": {
"type": "derived"
}
}
while a request to “test-request” results in 400 (“The Field field is required.” which is correct):
{
"type": "derived"
}
Reproduction Steps
- Use the action methods described in the description
- Run the described payloads on both methods
Expected behavior
Both tests should result in a 400 since field “field” is not provided.
Actual behavior
Only one of them results in 400. If the property is polymorphic, then the validation does not work (it is not only the Required attribute that is affected)
Regression?
No response
Known Workarounds
No response
Configuration
.Net 8 Mac OS ARM
Other information
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 3
- Comments: 22 (14 by maintainers)
It’s because
dotnet/runtimedoesn’t contain aspnet components. That’s fine, I transferred it to the relevant repo.We don’t currently support Native AOT with our web UI frameworks like MVC, Razor Pages, and Blazor. Blazor does support AOT for WebAssembly, but we haven’t yet shipped support for full object validation for Blazor: https://github.com/dotnet/aspnetcore/issues/28640
Correct, ASP.NET Core does its own object traversal for validation.
No, ValidateObjectMembersAttribute is part of a separate component, the options validator, that uses the same validation attributes.