Add option for more tolerant "add" which generates missing parts of "path"
Closed this issue · 0 comments
Currently (and according to the JSON Patch standard) when performing "add" against a non-existent path, json-patch errors out.
That said, there are a number of use cases where it is significantly more convenient to have a single "add" automatically generate the missing parts of the path, instead of having different patches for different "missing-element" scenarios.
For example, when patching a Kubernetes object which has no annotations in its manifest with the following patch, it would be best if json-patch generated the missing annotations
object:
{
"op": "add",
"path": "/metadata/annotations/my-annotation",
"value": "my-value"
}
The suggested solution is to introduce a new ApplyOptions
option: EnsurePathExistsOnAdd
with default value of false
.
When set to true
, this option instructs add
operations to make sure every element of their path exists, and if it does not, it creates it.
This auto-generation algorithm would be smart enough to recognize the type of container element to create (object or array), based on the value of the subsequent path element.
Additionally, when parts of the path are array indexes that do not exist in their respective arrays, EnsurePathExistsOnAdd
pads the containing array with nulls to ensure the array contains enough elements to perform the add
operation.