Merge, Replace, or Patch: How Astoria Handles Changing Data
Using REST, what should happen when you perform a PUT operation to update existing data? The Astoria Team asks that question and explains their answer.
When an Astoria-based web service receives a PUT operation, there are two possible ways to handle the update. Either it could replace the pre-existing data or it could merge the new values into the old ones. Due to their goal of maintaining compatibility with AtomPub, Microsoft decided that PUT should map to a replace.
This of course leaves the question of how to represent merging operations. Options include introducing a new verb, MERGE, or a new custom header. Pablo Castro writes,
While we’re not thrilled with the idea of introducing a new HTTP method, overloading PUT with an extra header seems to be very problematic. If anything else, a server that does not support “merge” through headers would see PUT as a regular “replace” request and perform an operation that’s not what the client expected. Also other things break. For example, if a server sees an actual MERGE request and cannot handle it then it can respond with 405 – method not supported.
Another option they considered was the PATCH operation. Unfortunately the specification wasn't finalized before Microsoft's code freeze date. This left Microsoft in the unenviable position of either using PATCH and risking the final specification was incompatible or use MERGE with the understanding that it may be deprecated. Since the first option would result in either breaking clients or being non-compliant, they decided to go with MERGE.
Since we are on the topic of clients, one should know that .NET clients default to merge semantics. This was chosen because a .NET client may not know about all the fields on the server, and the server has no way to know which fields were intentionally blank versus which were simply unknown.
AJAX clients also default to merge semantics. And like .NET clients they have an optional parameter to indicate that replace semantics are preferred.
Chris Mattmann Apr 15, 2014