Example #1
0
        public void IncludeDeeplyNestedResourceTest()
        {
            // Setup
            var data         = TestModelRepository.GetIncludeDeeplyNestedResourceTestData();
            var jsonDocument = JsonApiDocumentExtensions.CreateDocumentFromApiResource <TestModelCompoundApiResource>(data, @"http://test.com");

            // add includes
            jsonDocument.IncludeRelation <TestModelCompoundApiResource>(data, "bigData.children.toOne", @"http://test.com");

            // Serialize
            var jsonString = JsonConvert.SerializeObject(jsonDocument, Formatting.Indented);

            // Deserialize
            var deserializedDocument = JsonConvert.DeserializeObject <JsonApiDocument>(jsonString);

            // Retrieve Data
            var retrievedData = deserializedDocument.ToObject <TestModelCompound, TestModelCompoundApiResource>();

            retrievedData.BigData          = deserializedDocument.GetIncludedResource <TestModelToN, TestModelToNApiResource>(retrievedData.BigDataId);
            retrievedData.BigData.Children = retrievedData.BigData.ChildrenIds.Select(item => deserializedDocument.GetIncludedResource <TestModelToOne, TestModelToOneApiResource>(item));
            retrievedData.SmallData        = deserializedDocument.GetIncludedResource <TestModelToOne, TestModelToOneApiResource>(retrievedData.SmallDataId);

            // Compare
            Assert.AreEqual(data.MyIdProperty, retrievedData.MyIdProperty);
        }
Example #2
0
        [TestMethod] public void floTest()
        {
            var data = new ModelOne
            {
                Id            = 1232385789,
                HomeAttribute = "myhome",
                Model2Id      = 55555555,
                Model2        = new Model2
                {
                    Id   = 55555555,
                    Name = "myName"
                },
                MoreModel2Id = new List <long?> {
                    1, 2, 3
                },
                MoreModel2 = new List <Model2>
                {
                    new Model2
                    {
                        Id   = 1,
                        Name = "arr"
                    },
                    new Model2
                    {
                        Id   = 2,
                        Name = "arrr"
                    },
                    new Model2
                    {
                        Id   = 3,
                        Name = "arrr"
                    },
                }
            };

            var doc = JsonApiDocumentExtensions.CreateDocumentFromApiResource <ModelOneResource>(data);

            doc.IncludeRelation <ModelOneResource>(data, $"{nameof(ModelOne.MoreModel2)},{nameof(ModelOne.Model2)}");

            var jsonString   = JsonConvert.SerializeObject(doc, Formatting.Indented);
            var deserialized = JsonConvert.DeserializeObject <JsonApiDocument>(jsonString);
            Func <string, bool> foundAttributes;
            var resultData = deserialized.ToObject <ModelOne, ModelOneResource>(out foundAttributes);

            resultData.Model2     = deserialized.Included.GetResource(resultData.Model2Id, typeof(Model2))?.ToObject <Model2, Model2Resource>();
            resultData.MoreModel2 = resultData.MoreModel2Id?.Select(x => deserialized.Included.GetResource(x, typeof(Model2))?.ToObject <Model2, Model2Resource>());
            Assert.IsTrue(foundAttributes(nameof(data.HomeAttribute)));
        }