I am running into a problem when using the DataAnnotationsValidatorRecursive
I have a class Information where the Product property is required. However, I have inherited the class and I want to change the property to NOT required.
[DataContract]
[KnownType(typeof(InheritedPaperwork))]
public class Paperwork
{
[JsonProperty]
public string Id { get; set; }
[JsonProperty]
public Information Information { get; set; }
}
[DataContract]
public class Information
{
[DataMember, Required]
[JsonProperty]
public Product product { get; set; }
}
[DataContract]
public class Product
{
[JsonProperty]
public string Name { get; set; }
}
[DataContract]
public class InheritedPaperwork : Paperwork
{
}
[DataContract]
public class InheritedInformation : Information
{
[DataMember]
[JsonProperty]
public new Product product { get; set; }
}
However, when I deserialize a JSON object into a InheritedPaperwork
object, the result I get is that a Product
is required
var inheritedPaperwork =
JsonConvert.DeserializeObject<InheritedPaperwork>(
"{\"Id\": \"1\",\"Information\": {\"product\": null } }");
List<ValidationResult> results = new List<ValidationResult>();
Assert.IsTrue(
new DataAnnotationsValidator.DataAnnotationsValidator()
.TryValidateObjectRecursive(
inheritedInformation,
results),
string.Join(
Environment.NewLine,
results));
Assert.IsTrue failed. The Product field is required.
How and is it possible for the Validator to "see" the derived class ?