public void NestedCopyFromNonListToList()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    IntegerValue = 5,
                    IntegerList  = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

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

            patchDoc.Copy("SimpleDTO/IntegerValue", "SimpleDTO/IntegerList/0");
            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            deserialized.ApplyTo(doc);

            Assert.Equal(new List <int>()
            {
                5, 1, 2, 3
            }, doc.SimpleDTO.IntegerList);
        }
Exemplo n.º 2
0
        public void SerializeAndReplaceNestedObjectTest()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    IntegerValue = 5,
                    IntegerList  = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

            var newDTO = new SimpleDTO()
            {
                DoubleValue = 1
            };

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

            patchDoc.Replace("SimpleDTO", newDTO);

            // serialize & deserialize
            var serialized   = JsonConvert.SerializeObject(patchDoc);
            var deserialized = JsonConvert.DeserializeObject <JsonPatchDocument>(serialized);

            deserialized.ApplyTo(doc);

            Assert.Equal(1, doc.SimpleDTO.DoubleValue);
            Assert.Equal(0, doc.SimpleDTO.IntegerValue);
            Assert.Equal(null, doc.SimpleDTO.IntegerList);
        }
Exemplo n.º 3
0
        public void NestedRemoveFromEndOfList()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    IntegerList = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

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

            patchDoc.Remove("SimpleDTO/IntegerList/-");

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

            deserialized.ApplyTo(doc);

            Assert.Equal(new List <int>()
            {
                1, 2
            }, doc.SimpleDTO.IntegerList);
        }
        public void AddToListInListInvalidPositionTooSmall()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                ListOfSimpleDTO = new List <SimpleDTO>()
                {
                    new SimpleDTO()
                    {
                        IntegerList = new List <int>()
                        {
                            1, 2, 3
                        }
                    }
                }
            };

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

            patchDoc.Add("ListOfSimpleDTO/-1/IntegerList/0", 4);

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

            Assert.Throws <JsonPatchException>(() => { deserialized.ApplyTo(doc); });
        }
        public void AddToListInList()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                ListOfSimpleDTO = new List <SimpleDTO>()
                {
                    new SimpleDTO()
                    {
                        IntegerList = new List <int>()
                        {
                            1, 2, 3
                        }
                    }
                }
            };

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

            patchDoc.Add("ListOfSimpleDTO/0/IntegerList/0", 4);

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

            deserialized.ApplyTo(doc);

            Assert.Equal(new List <int>()
            {
                4, 1, 2, 3
            }, doc.ListOfSimpleDTO[0].IntegerList);
        }
Exemplo n.º 6
0
        public void NestedRemove()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    StringProperty = "A"
                }
            };

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

            patchDoc.Remove("SimpleDTO/StringProperty");

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

            deserialized.ApplyTo(doc);

            Assert.Equal(null, doc.SimpleDTO.StringProperty);
        }
Exemplo n.º 7
0
        public void Replace()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    StringProperty = "A",
                    DecimalValue   = 10
                }
            };

            // create patch
            JsonPatchDocument <SimpleDTOWithNestedDTO> patchDoc =
                new JsonPatchDocument <SimpleDTOWithNestedDTO>();

            patchDoc.Replace <string>(o => o.SimpleDTO.StringProperty, "B");
            patchDoc.Replace(o => o.SimpleDTO.DecimalValue, 12);

            patchDoc.ApplyTo(doc);

            Assert.Equal("B", doc.SimpleDTO.StringProperty);
            Assert.Equal(12, doc.SimpleDTO.DecimalValue);
        }
Exemplo n.º 8
0
        public void NestedRemoveFromListInvalidPositionTooSmall()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    IntegerList = new List <int>()
                    {
                        1, 2, 3
                    }
                }
            };

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

            patchDoc.Remove("SimpleDTO/IntegerList/-1");

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

            Assert.Throws <JsonPatchException>(() => { deserialized.ApplyTo(doc); });
        }
        public void NestedCopy()
        {
            var doc = new SimpleDTOWithNestedDTO()
            {
                SimpleDTO = new SimpleDTO()
                {
                    StringProperty        = "A",
                    AnotherStringProperty = "B"
                }
            };

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

            patchDoc.Copy("SimpleDTO/StringProperty", "SimpleDTO/AnotherStringProperty");

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

            deserialized.ApplyTo(doc);

            Assert.Equal("A", doc.SimpleDTO.AnotherStringProperty);
        }