|
| 1 | +using System.Collections.Generic; |
| 2 | +using JsonApiDotNetCore.Builders; |
| 3 | +using JsonApiDotNetCore.Configuration; |
| 4 | +using JsonApiDotNetCore.Internal.Generics; |
| 5 | +using JsonApiDotNetCore.Models; |
| 6 | +using JsonApiDotNetCore.Serialization; |
| 7 | +using JsonApiDotNetCore.Services; |
| 8 | +using Moq; |
| 9 | +using Newtonsoft.Json; |
| 10 | +using Newtonsoft.Json.Serialization; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace UnitTests.Serialization |
| 14 | +{ |
| 15 | + public class JsonApiDeSerializerTests |
| 16 | + { |
| 17 | + [Fact] |
| 18 | + public void Can_Deserialize_Complex_Types() |
| 19 | + { |
| 20 | + // arrange |
| 21 | + var contextGraphBuilder = new ContextGraphBuilder(); |
| 22 | + contextGraphBuilder.AddResource<TestResource>("test-resource"); |
| 23 | + var contextGraph = contextGraphBuilder.Build(); |
| 24 | + |
| 25 | + var jsonApiContextMock = new Mock<IJsonApiContext>(); |
| 26 | + jsonApiContextMock.SetupAllProperties(); |
| 27 | + jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph); |
| 28 | + jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>()); |
| 29 | + jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions { |
| 30 | + JsonContractResolver = new CamelCasePropertyNamesContractResolver() |
| 31 | + }); |
| 32 | + |
| 33 | + var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>(); |
| 34 | + |
| 35 | + var deserializer = new JsonApiDeSerializer(jsonApiContextMock.Object, genericProcessorFactoryMock.Object); |
| 36 | + |
| 37 | + var content = new Document { |
| 38 | + Data = new DocumentData { |
| 39 | + Type = "test-resource", |
| 40 | + Id = "1", |
| 41 | + Attributes = new Dictionary<string, object> { |
| 42 | + { |
| 43 | + "complex-member", new { compoundName = "testName" } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + // act |
| 50 | + var result = deserializer.Deserialize<TestResource>(JsonConvert.SerializeObject(content)); |
| 51 | + |
| 52 | + // assert |
| 53 | + Assert.NotNull(result.ComplexMember); |
| 54 | + Assert.Equal("testName", result.ComplexMember.CompoundName); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void Can_Deserialize_Complex_List_Types() |
| 59 | + { |
| 60 | + // arrange |
| 61 | + var contextGraphBuilder = new ContextGraphBuilder(); |
| 62 | + contextGraphBuilder.AddResource<TestResourceWithList>("test-resource"); |
| 63 | + var contextGraph = contextGraphBuilder.Build(); |
| 64 | + |
| 65 | + var jsonApiContextMock = new Mock<IJsonApiContext>(); |
| 66 | + jsonApiContextMock.SetupAllProperties(); |
| 67 | + jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph); |
| 68 | + jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>()); |
| 69 | + jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions { |
| 70 | + JsonContractResolver = new CamelCasePropertyNamesContractResolver() |
| 71 | + }); |
| 72 | + |
| 73 | + var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>(); |
| 74 | + |
| 75 | + var deserializer = new JsonApiDeSerializer(jsonApiContextMock.Object, genericProcessorFactoryMock.Object); |
| 76 | + |
| 77 | + var content = new Document { |
| 78 | + Data = new DocumentData { |
| 79 | + Type = "test-resource", |
| 80 | + Id = "1", |
| 81 | + Attributes = new Dictionary<string, object> { |
| 82 | + { |
| 83 | + "complex-members", new [] { |
| 84 | + new { compoundName = "testName" } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + // act |
| 92 | + var result = deserializer.Deserialize<TestResourceWithList>(JsonConvert.SerializeObject(content)); |
| 93 | + |
| 94 | + // assert |
| 95 | + Assert.NotNull(result.ComplexMembers); |
| 96 | + Assert.NotEmpty(result.ComplexMembers); |
| 97 | + Assert.Equal("testName", result.ComplexMembers[0].CompoundName); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public void Can_Deserialize_Complex_Types_With_Dasherized_Attrs() |
| 102 | + { |
| 103 | + // arrange |
| 104 | + var contextGraphBuilder = new ContextGraphBuilder(); |
| 105 | + contextGraphBuilder.AddResource<TestResource>("test-resource"); |
| 106 | + var contextGraph = contextGraphBuilder.Build(); |
| 107 | + |
| 108 | + var jsonApiContextMock = new Mock<IJsonApiContext>(); |
| 109 | + jsonApiContextMock.SetupAllProperties(); |
| 110 | + jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph); |
| 111 | + jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>()); |
| 112 | + |
| 113 | + jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions { |
| 114 | + JsonContractResolver = new DasherizedResolver() // <--- |
| 115 | + }); |
| 116 | + |
| 117 | + var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>(); |
| 118 | + |
| 119 | + var deserializer = new JsonApiDeSerializer(jsonApiContextMock.Object, genericProcessorFactoryMock.Object); |
| 120 | + |
| 121 | + var content = new Document { |
| 122 | + Data = new DocumentData { |
| 123 | + Type = "test-resource", |
| 124 | + Id = "1", |
| 125 | + Attributes = new Dictionary<string, object> { |
| 126 | + { |
| 127 | + "complex-member", new Dictionary<string, string> { { "compound-name", "testName" } } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + }; |
| 132 | + |
| 133 | + // act |
| 134 | + var result = deserializer.Deserialize<TestResource>(JsonConvert.SerializeObject(content)); |
| 135 | + |
| 136 | + // assert |
| 137 | + Assert.NotNull(result.ComplexMember); |
| 138 | + Assert.Equal("testName", result.ComplexMember.CompoundName); |
| 139 | + } |
| 140 | + |
| 141 | + private class TestResource : Identifiable |
| 142 | + { |
| 143 | + [Attr("complex-member")] |
| 144 | + public ComplexType ComplexMember { get; set; } |
| 145 | + } |
| 146 | + |
| 147 | + private class TestResourceWithList : Identifiable |
| 148 | + { |
| 149 | + [Attr("complex-members")] |
| 150 | + public List<ComplexType> ComplexMembers { get; set; } |
| 151 | + } |
| 152 | + |
| 153 | + private class ComplexType |
| 154 | + { |
| 155 | + public string CompoundName { get; set; } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments