Handling Nullable Property Updates in ASP.NET
I'm creating an endpoint to update an object, specifically a Salon. The SalonUpdateRequest class has three properties that can be updated: string Name, string PhoneNumber, and string? Description (nullable).
I'm facing a dilemma regarding the Description property. If the user doesn't update it (omits it in the JSON), it defaults to null, which makes it unclear whether the user intends to remove the Description or simply doesn't want to update it.
I have two ideas on how to solve this issue, but I'd like to seek advice from more experienced individuals to choose the most sensible approach.
4 Replies
The requester has to send the value
That's the difference between Patch and Update. I don't know if I remember which is which, but I think Patch is basically partial/optional, any specified values are written, anything omitted is left alone, while an Update is saying explicitly set these values exactly as I give them
so you'd usually have endpoints for both
I would make it non-nullable and default to an empty string
Empty string — no description, erase it if there was any
Null — do not change
Okay, I figured it out using JsonPatchDocument. Thanks for the help!