Closed
Description
DESCRIPTION
When I use an object implementing IValidatableObject on a relationship with linkage data only, then the validation is still executed.
This bug looks similar to #671 which had the same issue when using the [Required]
attribute.
STEPS TO REPRODUCE
Consider the following models
[Resource]
public class Library : Identifiable<int>, IValidatableObject
{
[Attr, Required] public string? Name { get; set; }
[HasMany] public ICollection<Book> Books { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
// Should never happen as we have the [Required] attribute
// However, this is still called when used on a relationship
if (Name == null)
{
yield return new ValidationResult("Name is mandatory");
}
}
}
[Resource]
public class Book : Identifiable<int>
{
[Attr] public string? Name { get; set; }
[HasOne] public Library Library { get; set; }
}
First, create a library
{
"data": {
"type": "libraries",
"attributes": {
"name": "Test library"
},
"relationships": {
"books": {
"data": []
}
}
}
}
Then, create a book and associate it with the created library.
{
"data": {
"type": "books",
"attributes": {
"name": "Test book"
},
"relationships": {
"library": {
"data": {
"type": "libraries",
"id": "1"
}
}
}
}
}
EXPECTED BEHAVIOR
The book is created.
ACTUAL BEHAVIOR
The custom validation code from Library
is executed, causing this error
{
"links": {
"self": "http://localhost:5000/v1/books"
},
"errors": [
{
"id": "5d84f0da-0432-49e4-9214-6e4476fd35d6",
"status": "422",
"title": "Input validation failed.",
"detail": "Name is mandatory",
"source": {
"pointer": "/data/relationships/library/data"
}
}
]
}
VERSIONS USED
- JsonApiDotNetCore version: 5.0.3
- ASP.NET Core version: 6.0.301
- Entity Framework Core version: 6.0.10
- Database provider: pgsql