33using System . Text . RegularExpressions ;
44using JsonApiDotNetCore . Builders ;
55using JsonApiDotNetCore . Configuration ;
6+ using JsonApiDotNetCore . Extensions ;
67using JsonApiDotNetCore . Internal ;
78using JsonApiDotNetCore . Models ;
89using JsonApiDotNetCore . Request ;
910using JsonApiDotNetCore . Serialization ;
1011using JsonApiDotNetCore . Services ;
12+ using Microsoft . Extensions . DependencyInjection ;
1113using Moq ;
1214using Xunit ;
1315
@@ -68,7 +70,7 @@ public void Can_Serialize_Deeply_Nested_Relationships()
6870 {
6971 // arrange
7072 var contextGraphBuilder = new ContextGraphBuilder ( ) ;
71- contextGraphBuilder . AddResource < TestResource > ( "test-resources " ) ;
73+ contextGraphBuilder . AddResource < TestResource > ( "test-resource " ) ;
7274 contextGraphBuilder . AddResource < ChildResource > ( "children" ) ;
7375 contextGraphBuilder . AddResource < InfectionResource > ( "infections" ) ;
7476
@@ -79,14 +81,18 @@ public void Can_Serialize_Deeply_Nested_Relationships()
7981
8082 var resource = new TestResource
8183 {
84+ Id = 1 ,
8285 Children = new List < ChildResource > {
8386 new ChildResource {
87+ Id = 2 ,
8488 Infections = new List < InfectionResource > {
85- new InfectionResource ( ) ,
86- new InfectionResource ( ) ,
89+ new InfectionResource { Id = 4 } ,
90+ new InfectionResource { Id = 5 } ,
8791 }
8892 } ,
89- new ChildResource ( )
93+ new ChildResource {
94+ Id = 3
95+ }
9096 }
9197 } ;
9298
@@ -100,43 +106,99 @@ public void Can_Serialize_Deeply_Nested_Relationships()
100106 @"{
101107 ""data"": {
102108 ""attributes"": {
103- ""complex-member"": {
104- ""compound-name"": ""testname""
105- }
109+ ""complex-member"": null
106110 },
107111 ""relationships"": {
108112 ""children"": {
109113 ""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+ }]
113124 }
114125 },
115126 ""type"": ""test-resource"",
116- ""id"": """"
127+ ""id"": ""1 ""
117128 },
118- ""included"": {
129+ ""included"": [
119130 {
120131 ""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""
123155 },
124156 {
125157 ""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""
128168 },
129169 {
130170 ""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""
133181 },
134182 {
135183 ""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""
138200 }
139- }
201+ ]
140202 }" ;
141203 var expected = Regex . Replace ( expectedFormatted , @"\s+" , "" ) ;
142204
@@ -167,7 +229,17 @@ private JsonApiSerializer GetSerializer(
167229 var jsonApiOptions = new JsonApiOptions ( ) ;
168230 jsonApiContextMock . Setup ( m => m . Options ) . Returns ( jsonApiOptions ) ;
169231
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 ) ;
171243 var serializer = new JsonApiSerializer ( jsonApiContextMock . Object , documentBuilder ) ;
172244
173245 return serializer ;
0 commit comments