JsonPatchOperation on nested property #13415
-
How can I patch an object with a nested key that does not exist in the json representation of the document? This is somewhat what I try to do. [Fact]
public void ShouldPatchDocument()
{
using (var store = GetDocumentStore())
{
using (var session = store.OpenSession())
{
dynamic obj = new ExpandoObject();
obj.Id = "users/1";
session.Store(obj);
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var jpd = new JsonPatchDocument();
jpd.Add("/Address/Street", "Sesame Street");
JsonPatchResult op = store.Operations.Send(new JsonPatchOperation("users/1", jpd));
Assert.Equal(PatchStatus.Patched, op.Status);
}
}
} This fails with If I add |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi Marcus, The member you are trying to add to must exist first. "For example, an "add" with a target location of "/a/b" starting with this document: { "a": { "foo": 1 } } is not an error, because "a" exists, and "b" will be added to its { "q": { "bar": 2 } } because "a" does not exist." |
Beta Was this translation helpful? Give feedback.
Hi Marcus,
The member you are trying to add to must exist first.
This is according to the RFC (https://datatracker.ietf.org/doc/html/rfc6902#section-4.1):
"For example, an "add" with a target location of "/a/b" starting with this document:
{ "a": { "foo": 1 } }
is not an error, because "a" exists, and "b" will be added to its
value. It is an error in this document:
{ "q": { "bar": 2 } }
because "a" does not exist."