1
- using JsonApiDotNetCore . Builders ;
1
+ using System . Collections . Generic ;
2
+ using System . Linq ;
3
+ using System . Text . RegularExpressions ;
4
+ using JsonApiDotNetCore . Builders ;
2
5
using JsonApiDotNetCore . Configuration ;
3
6
using JsonApiDotNetCore . Internal ;
4
7
using JsonApiDotNetCore . Models ;
8
+ using JsonApiDotNetCore . Request ;
5
9
using JsonApiDotNetCore . Serialization ;
6
10
using JsonApiDotNetCore . Services ;
7
11
using Moq ;
@@ -17,19 +21,9 @@ public void Can_Serialize_Complex_Types()
17
21
// arrange
18
22
var contextGraphBuilder = new ContextGraphBuilder ( ) ;
19
23
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 ) ;
30
26
31
- var documentBuilder = new DocumentBuilder ( jsonApiContextMock . Object ) ;
32
- var serializer = new JsonApiSerializer ( jsonApiContextMock . Object , documentBuilder ) ;
33
27
var resource = new TestResource
34
28
{
35
29
ComplexMember = new ComplexType
@@ -43,18 +37,165 @@ public void Can_Serialize_Complex_Types()
43
37
44
38
// assert
45
39
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 ;
47
174
}
48
175
49
176
private class TestResource : Identifiable
50
177
{
51
178
[ Attr ( "complex-member" ) ]
52
179
public ComplexType ComplexMember { get ; set ; }
180
+
181
+ [ HasMany ( "children" ) ] public List < ChildResource > Children { get ; set ; }
53
182
}
54
183
55
184
private class ComplexType
56
185
{
57
186
public string CompoundName { get ; set ; }
58
187
}
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
+ }
59
200
}
60
201
}
0 commit comments