Skip to content

Commit a0b6080

Browse files
committed
this one is tricky
1 parent daa61ab commit a0b6080

File tree

1 file changed

+155
-14
lines changed

1 file changed

+155
-14
lines changed
Lines changed: 155 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
using JsonApiDotNetCore.Builders;
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Text.RegularExpressions;
4+
using JsonApiDotNetCore.Builders;
25
using JsonApiDotNetCore.Configuration;
36
using JsonApiDotNetCore.Internal;
47
using JsonApiDotNetCore.Models;
8+
using JsonApiDotNetCore.Request;
59
using JsonApiDotNetCore.Serialization;
610
using JsonApiDotNetCore.Services;
711
using Moq;
@@ -17,19 +21,9 @@ public void Can_Serialize_Complex_Types()
1721
// arrange
1822
var contextGraphBuilder = new ContextGraphBuilder();
1923
contextGraphBuilder.AddResource<TestResource>("test-resource");
20-
var contextGraph = contextGraphBuilder.Build();
21-
22-
var jsonApiContextMock = new Mock<IJsonApiContext>();
23-
jsonApiContextMock.SetupAllProperties();
24-
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
25-
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions());
26-
jsonApiContextMock.Setup(m => m.RequestEntity)
27-
.Returns(contextGraph.GetContextEntity("test-resource"));
28-
jsonApiContextMock.Setup(m => m.MetaBuilder).Returns(new MetaBuilder());
29-
jsonApiContextMock.Setup(m => m.PageManager).Returns(new PageManager());
24+
25+
var serializer = GetSerializer(contextGraphBuilder);
3026

31-
var documentBuilder = new DocumentBuilder(jsonApiContextMock.Object);
32-
var serializer = new JsonApiSerializer(jsonApiContextMock.Object, documentBuilder);
3327
var resource = new TestResource
3428
{
3529
ComplexMember = new ComplexType
@@ -43,18 +37,165 @@ public void Can_Serialize_Complex_Types()
4337

4438
// assert
4539
Assert.NotNull(result);
46-
Assert.Equal("{\"data\":{\"attributes\":{\"complex-member\":{\"compound-name\":\"testname\"}},\"type\":\"test-resource\",\"id\":\"\"}}", result);
40+
41+
var expectedFormatted =
42+
@"{
43+
""data"": {
44+
""attributes"": {
45+
""complex-member"": {
46+
""compound-name"": ""testname""
47+
}
48+
},
49+
""relationships"": {
50+
""children"": {
51+
""links"": {
52+
""self"": ""/test-resource//relationships/children"",
53+
""related"": ""/test-resource//children""
54+
}
55+
}
56+
},
57+
""type"": ""test-resource"",
58+
""id"": """"
59+
}
60+
}";
61+
var expected = Regex.Replace(expectedFormatted, @"\s+", "");
62+
63+
Assert.Equal(expected, result);
64+
}
65+
66+
[Fact]
67+
public void Can_Serialize_Deeply_Nested_Relationships()
68+
{
69+
// arrange
70+
var contextGraphBuilder = new ContextGraphBuilder();
71+
contextGraphBuilder.AddResource<TestResource>("test-resources");
72+
contextGraphBuilder.AddResource<ChildResource>("children");
73+
contextGraphBuilder.AddResource<InfectionResource>("infections");
74+
75+
var serializer = GetSerializer(
76+
contextGraphBuilder,
77+
included: new List<string> { "children.infections" }
78+
);
79+
80+
var resource = new TestResource
81+
{
82+
Children = new List<ChildResource> {
83+
new ChildResource {
84+
Infections = new List<InfectionResource> {
85+
new InfectionResource(),
86+
new InfectionResource(),
87+
}
88+
},
89+
new ChildResource()
90+
}
91+
};
92+
93+
// act
94+
var result = serializer.Serialize(resource);
95+
96+
// assert
97+
Assert.NotNull(result);
98+
99+
var expectedFormatted =
100+
@"{
101+
""data"": {
102+
""attributes"": {
103+
""complex-member"": {
104+
""compound-name"": ""testname""
105+
}
106+
},
107+
""relationships"": {
108+
""children"": {
109+
""links"": {
110+
""self"": ""/test-resource//relationships/children"",
111+
""related"": ""/test-resource//children""
112+
}
113+
}
114+
},
115+
""type"": ""test-resource"",
116+
""id"": """"
117+
},
118+
""included"": {
119+
{
120+
""attributes"": {},
121+
""relationships"": {},
122+
""type"": ""children""
123+
},
124+
{
125+
""attributes"": {},
126+
""relationships"": {},
127+
""type"": ""children""
128+
},
129+
{
130+
""attributes"": {},
131+
""relationships"": {},
132+
""type"": ""infections""
133+
},
134+
{
135+
""attributes"": {},
136+
""relationships"": {},
137+
""type"": ""infections""
138+
}
139+
}
140+
}";
141+
var expected = Regex.Replace(expectedFormatted, @"\s+", "");
142+
143+
Assert.Equal(expected, result);
144+
}
145+
146+
private JsonApiSerializer GetSerializer(
147+
ContextGraphBuilder contextGraphBuilder,
148+
List<string> included = null)
149+
{
150+
var contextGraph = contextGraphBuilder.Build();
151+
152+
var jsonApiContextMock = new Mock<IJsonApiContext>();
153+
jsonApiContextMock.SetupAllProperties();
154+
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
155+
jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions());
156+
jsonApiContextMock.Setup(m => m.RequestEntity).Returns(contextGraph.GetContextEntity("test-resource"));
157+
// jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>());
158+
// jsonApiContextMock.Setup(m => m.RelationshipsToUpdate).Returns(new Dictionary<RelationshipAttribute, object>());
159+
// jsonApiContextMock.Setup(m => m.HasManyRelationshipPointers).Returns(new HasManyRelationshipPointers());
160+
// jsonApiContextMock.Setup(m => m.HasOneRelationshipPointers).Returns(new HasOneRelationshipPointers());
161+
jsonApiContextMock.Setup(m => m.MetaBuilder).Returns(new MetaBuilder());
162+
jsonApiContextMock.Setup(m => m.PageManager).Returns(new PageManager());
163+
164+
if (included != null)
165+
jsonApiContextMock.Setup(m => m.IncludedRelationships).Returns(included);
166+
167+
var jsonApiOptions = new JsonApiOptions();
168+
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);
169+
170+
var documentBuilder = new DocumentBuilder(jsonApiContextMock.Object);
171+
var serializer = new JsonApiSerializer(jsonApiContextMock.Object, documentBuilder);
172+
173+
return serializer;
47174
}
48175

49176
private class TestResource : Identifiable
50177
{
51178
[Attr("complex-member")]
52179
public ComplexType ComplexMember { get; set; }
180+
181+
[HasMany("children")] public List<ChildResource> Children { get; set; }
53182
}
54183

55184
private class ComplexType
56185
{
57186
public string CompoundName { get; set; }
58187
}
188+
189+
private class ChildResource : Identifiable
190+
{
191+
[HasMany("infections")] public List<InfectionResource> Infections { get; set;}
192+
193+
[HasOne("parent")] public TestResource Parent { get; set; }
194+
}
195+
196+
private class InfectionResource : Identifiable
197+
{
198+
[HasOne("infected")] public ChildResource Infected { get; set; }
199+
}
59200
}
60201
}

0 commit comments

Comments
 (0)