public void AddNewPropertyToExpandoOjectInTypedObject()
        {
            var doc = new NestedObject()
            {
                DynamicProperty = new ExpandoObject()
            };

            // create patch
            var patchDoc = new JsonPatchDocument();

            patchDoc.Add("DynamicProperty/NewInt", 1);

            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            deserialized.ApplyTo(doc);

            Assert.Equal(1, doc.DynamicProperty.NewInt);
        }
        public void ShouldNotBeAbleToAddToNonExistingPropertyThatIsNotTheRoot()
        {
            //Adding to a Nonexistent Target
            //
            //   An example target JSON document:
            //   { "foo": "bar" }
            //   A JSON Patch document:
            //   [
            //        { "op": "add", "path": "/baz/bat", "value": "qux" }
            //      ]
            //   This JSON Patch document, applied to the target JSON document above,
            //   would result in an error (therefore, it would not be applied),
            //   because the "add" operation's target location that references neither
            //   the root of the document, nor a member of an existing object, nor a
            //   member of an existing array.

            var doc = new NestedObject()
            {
                DynamicProperty = new ExpandoObject()
            };

            // create patch
            var patchDoc = new JsonPatchDocument();

            patchDoc.Add("DynamicProperty/OtherProperty/IntProperty", 1);

            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            var exception = Assert.Throws <JsonPatchException>(() =>
            {
                deserialized.ApplyTo(doc);
            });

            Assert.Equal(
                string.Format(
                    "For operation '{0}', the target location specified by path '{1}' was not found.",
                    "add",
                    "/DynamicProperty/OtherProperty/IntProperty"),
                exception.Message);
        }
        public void ShouldNotBeAbleToAdd_ToNonExistingProperty_ThatIsNotTheRoot()
        {
            //Adding to a Nonexistent Target
            //
            //   An example target JSON document:
            //   { "foo": "bar" }
            //   A JSON Patch document:
            //   [
            //        { "op": "add", "path": "/baz/bat", "value": "qux" }
            //      ]
            //   This JSON Patch document, applied to the target JSON document above,
            //   would result in an error (therefore, it would not be applied),
            //   because the "add" operation's target location that references neither
            //   the root of the document, nor a member of an existing object, nor a
            //   member of an existing array.

            // Arrange
            var nestedObject = new NestedObject()
            {
                DynamicProperty = new DynamicTestObject()
            };

            var patchDoc = new JsonPatchDocument();

            patchDoc.Add("DynamicProperty/OtherProperty/IntProperty", 1);

            // Act
            var exception = Assert.Throws <JsonPatchException>(() =>
            {
                patchDoc.ApplyTo(nestedObject);
            });

            // Assert
            Assert.Equal(
                string.Format(
                    "The target location specified by path segment '{0}' was not found.",
                    "OtherProperty"),
                exception.Message);
        }
        public void AddNewPropertyToTypedObjectInExpandoObject()
        {
            dynamic dynamicProperty = new ExpandoObject();

            dynamicProperty.StringProperty = "A";

            var doc = new NestedObject()
            {
                DynamicProperty = dynamicProperty
            };

            // create patch
            var patchDoc = new JsonPatchDocument();

            patchDoc.Add("DynamicProperty/StringProperty", "B");

            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            deserialized.ApplyTo(doc);

            Assert.Equal("B", doc.DynamicProperty.StringProperty);
        }
 public SimpleObjectWithNestedObject()
 {
     NestedObject       = new NestedObject();
     SimpleObject       = new SimpleObject();
     ListOfSimpleObject = new List <SimpleObject>();
 }