3
3
using System . Text . RegularExpressions ;
4
4
using JsonApiDotNetCore . Builders ;
5
5
using JsonApiDotNetCore . Configuration ;
6
+ using JsonApiDotNetCore . Extensions ;
6
7
using JsonApiDotNetCore . Internal ;
7
8
using JsonApiDotNetCore . Models ;
8
9
using JsonApiDotNetCore . Request ;
9
10
using JsonApiDotNetCore . Serialization ;
10
11
using JsonApiDotNetCore . Services ;
12
+ using Microsoft . Extensions . DependencyInjection ;
11
13
using Moq ;
12
14
using Xunit ;
13
15
@@ -68,7 +70,7 @@ public void Can_Serialize_Deeply_Nested_Relationships()
68
70
{
69
71
// arrange
70
72
var contextGraphBuilder = new ContextGraphBuilder ( ) ;
71
- contextGraphBuilder . AddResource < TestResource > ( "test-resources " ) ;
73
+ contextGraphBuilder . AddResource < TestResource > ( "test-resource " ) ;
72
74
contextGraphBuilder . AddResource < ChildResource > ( "children" ) ;
73
75
contextGraphBuilder . AddResource < InfectionResource > ( "infections" ) ;
74
76
@@ -79,14 +81,18 @@ public void Can_Serialize_Deeply_Nested_Relationships()
79
81
80
82
var resource = new TestResource
81
83
{
84
+ Id = 1 ,
82
85
Children = new List < ChildResource > {
83
86
new ChildResource {
87
+ Id = 2 ,
84
88
Infections = new List < InfectionResource > {
85
- new InfectionResource ( ) ,
86
- new InfectionResource ( ) ,
89
+ new InfectionResource { Id = 4 } ,
90
+ new InfectionResource { Id = 5 } ,
87
91
}
88
92
} ,
89
- new ChildResource ( )
93
+ new ChildResource {
94
+ Id = 3
95
+ }
90
96
}
91
97
} ;
92
98
@@ -100,43 +106,99 @@ public void Can_Serialize_Deeply_Nested_Relationships()
100
106
@"{
101
107
""data"": {
102
108
""attributes"": {
103
- ""complex-member"": {
104
- ""compound-name"": ""testname""
105
- }
109
+ ""complex-member"": null
106
110
},
107
111
""relationships"": {
108
112
""children"": {
109
113
""links"": {
110
- ""self"": ""/test-resource//relationships/children"",
111
- ""related"": ""/test-resource//children""
112
- }
114
+ ""self"": ""/test-resource/1/relationships/children"",
115
+ ""related"": ""/test-resource/1/children""
116
+ },
117
+ ""data"": [{
118
+ ""type"": ""children"",
119
+ ""id"": ""2""
120
+ }, {
121
+ ""type"": ""children"",
122
+ ""id"": ""3""
123
+ }]
113
124
}
114
125
},
115
126
""type"": ""test-resource"",
116
- ""id"": """"
127
+ ""id"": ""1 ""
117
128
},
118
- ""included"": {
129
+ ""included"": [
119
130
{
120
131
""attributes"": {},
121
- ""relationships"": {},
122
- ""type"": ""children""
132
+ ""relationships"": {
133
+ ""infections"": {
134
+ ""links"": {
135
+ ""self"": ""/children/2/relationships/infections"",
136
+ ""related"": ""/children/2/infections""
137
+ },
138
+ ""data"": [{
139
+ ""type"": ""infections"",
140
+ ""id"": ""4""
141
+ }, {
142
+ ""type"": ""infections"",
143
+ ""id"": ""5""
144
+ }]
145
+ },
146
+ ""parent"": {
147
+ ""links"": {
148
+ ""self"": ""/children/2/relationships/parent"",
149
+ ""related"": ""/children/2/parent""
150
+ }
151
+ }
152
+ },
153
+ ""type"": ""children"",
154
+ ""id"": ""2""
123
155
},
124
156
{
125
157
""attributes"": {},
126
- ""relationships"": {},
127
- ""type"": ""children""
158
+ ""relationships"": {
159
+ ""infected"": {
160
+ ""links"": {
161
+ ""self"": ""/infections/4/relationships/infected"",
162
+ ""related"": ""/infections/4/infected""
163
+ }
164
+ }
165
+ },
166
+ ""type"": ""infections"",
167
+ ""id"": ""4""
128
168
},
129
169
{
130
170
""attributes"": {},
131
- ""relationships"": {},
132
- ""type"": ""infections""
171
+ ""relationships"": {
172
+ ""infected"": {
173
+ ""links"": {
174
+ ""self"": ""/infections/5/relationships/infected"",
175
+ ""related"": ""/infections/5/infected""
176
+ }
177
+ }
178
+ },
179
+ ""type"": ""infections"",
180
+ ""id"": ""5""
133
181
},
134
182
{
135
183
""attributes"": {},
136
- ""relationships"": {},
137
- ""type"": ""infections""
184
+ ""relationships"": {
185
+ ""infections"": {
186
+ ""links"": {
187
+ ""self"": ""/children/3/relationships/infections"",
188
+ ""related"": ""/children/3/infections""
189
+ }
190
+ },
191
+ ""parent"": {
192
+ ""links"": {
193
+ ""self"": ""/children/3/relationships/parent"",
194
+ ""related"": ""/children/3/parent""
195
+ }
196
+ }
197
+ },
198
+ ""type"": ""children"",
199
+ ""id"": ""3""
138
200
}
139
- }
201
+ ]
140
202
}" ;
141
203
var expected = Regex . Replace ( expectedFormatted , @"\s+" , "" ) ;
142
204
@@ -167,7 +229,17 @@ private JsonApiSerializer GetSerializer(
167
229
var jsonApiOptions = new JsonApiOptions ( ) ;
168
230
jsonApiContextMock . Setup ( m => m . Options ) . Returns ( jsonApiOptions ) ;
169
231
170
- var documentBuilder = new DocumentBuilder ( jsonApiContextMock . Object ) ;
232
+ var services = new ServiceCollection ( ) ;
233
+
234
+ var mvcBuilder = services . AddMvcCore ( ) ;
235
+
236
+ services
237
+ . AddJsonApiInternals ( jsonApiOptions ) ;
238
+
239
+ var provider = services . BuildServiceProvider ( ) ;
240
+ var scoped = new TestScopedServiceProvider ( provider ) ;
241
+
242
+ var documentBuilder = new DocumentBuilder ( jsonApiContextMock . Object , scopedServiceProvider : scoped ) ;
171
243
var serializer = new JsonApiSerializer ( jsonApiContextMock . Object , documentBuilder ) ;
172
244
173
245
return serializer ;
0 commit comments