I'm developing a CRUD web application which exposes a REST endpoint to update an existing document in a MongoDB collection.
The json object sent by the frontend only contains the fields that have to change.
Since the json parser will initialize all fields that were non present in the json object to null, how can I know if a field is null because the user wanted to remove that value, or because it doesn't have to be updated ?
The answer is probably that there is no way to know, but then my question rather is: how do people usually handle that case ?
Sending the full object each time and replacing it entirely in the DB is not an option for me, for several reasons (good practices, performance and also the way the frontend works).
I thought of a solution that could work but I'm not really happy with it:
- add an extra boolean flag for each field that is not mandatory (for the mandatory ones, I know I can't delete the value)
- intercept the json in a MessageBodyReader and parse the json myself
- if a field is present with null in the json, set the extra flag to true to mark it as "to delete"
This is the "cleanest" solution I could think of so far, but it requires so much extra work that I hope I can find another way.
Thanks in advance for any advise.