-
Notifications
You must be signed in to change notification settings - Fork 0
Description
Priority: Low
Problem:
If a new data annotation (ex: [StringLength(200)]) is added to a property and a PATCH updates a different property, then old property values not in accordance with this new annotation will make the entire PATCH fail, even if there is no operation present for that property. This can only be resolved by updating that property itself to a value in accordance with the data annotation rule.
Solution:
Only validate the properties that are being updated/removed/added/... by the JsonPatchDocument, instead of validating all properties of the model. The question is, how to implement this?
Apparently this is issue is not widely known in the .NET Core community, since our current strategy that contains this bug is widely used in the .NET Core community.
Scenario:
- Say you have a MyPatchRequest class with 2 properties:
public class MyPatchRequest
{
[Required]
public string Name { get; set; }
[Required]
public string Email { get; set; }
}- A new MyPatchRequest record gets added to the database:
{ "Name": "a", "Email": "bob@example.org" }. - The devs realize the property Name needs to have a minimum amount of chars and add a new data annotation to the MyPatchRequest class:
public class MyPatchRequest
{
[Required]
[StringLength(200, MinimumLength = 2)]
public string Name { get; set; }
[Required]
public string Email { get; set; }
}- Now when doing a PATCH with only updating the Email property for the old record, you will get an error saying "Name has to have at least 2 characters", even though you're not touching the Name property with your PATCH request. This will keep happening until the Name property itself is updated to a valid value.