@@ -28,6 +28,7 @@ public DocumentBuilder_Tests()
2828 _options . BuildContextGraph ( builder =>
2929 {
3030 builder . AddResource < Model > ( "models" ) ;
31+ builder . AddResource < RelatedModel > ( "related-models" ) ;
3132 } ) ;
3233
3334 _jsonApiContextMock
@@ -59,7 +60,7 @@ public DocumentBuilder_Tests()
5960 [ Fact ]
6061 public void Includes_Paging_Links_By_Default ( )
6162 {
62- // arrange
63+ // arrange
6364 _pageManager . PageSize = 1 ;
6465 _pageManager . TotalRecords = 1 ;
6566 _pageManager . CurrentPage = 1 ;
@@ -121,6 +122,38 @@ public void Related_Links_Can_Be_Disabled()
121122 Assert . Null ( document . Data . Relationships [ "related-model" ] . Links ) ;
122123 }
123124
125+ [ Fact ]
126+ public void Related_Data_Included_In_Relationships_By_Default ( )
127+ {
128+ // arrange
129+ const string relatedTypeName = "related-models" ;
130+ const string relationshipName = "related-model" ;
131+ const int relatedId = 1 ;
132+ _jsonApiContextMock
133+ . Setup ( m => m . ContextGraph )
134+ . Returns ( _options . ContextGraph ) ;
135+
136+ var documentBuilder = new DocumentBuilder ( _jsonApiContextMock . Object ) ;
137+ var entity = new Model
138+ {
139+ RelatedModel = new RelatedModel
140+ {
141+ Id = relatedId
142+ }
143+ } ;
144+
145+ // act
146+ var document = documentBuilder . Build ( entity ) ;
147+
148+ // assert
149+ var relationshipData = document . Data . Relationships [ relationshipName ] ;
150+ Assert . NotNull ( relationshipData ) ;
151+ Assert . NotNull ( relationshipData . SingleData ) ;
152+ Assert . NotNull ( relationshipData . SingleData ) ;
153+ Assert . Equal ( relatedId . ToString ( ) , relationshipData . SingleData . Id ) ;
154+ Assert . Equal ( relatedTypeName , relationshipData . SingleData . Type ) ;
155+ }
156+
124157 [ Fact ]
125158 public void Build_Can_Build_Arrays ( )
126159 {
@@ -145,12 +178,12 @@ public void Build_Can_Build_CustomIEnumerables()
145178
146179
147180 [ Theory ]
148- [ InlineData ( null , null , true ) ]
149- [ InlineData ( false , null , true ) ]
150- [ InlineData ( true , null , false ) ]
151- [ InlineData ( null , "foo" , true ) ]
152- [ InlineData ( false , "foo" , true ) ]
153- [ InlineData ( true , "foo" , true ) ]
181+ [ InlineData ( null , null , true ) ]
182+ [ InlineData ( false , null , true ) ]
183+ [ InlineData ( true , null , false ) ]
184+ [ InlineData ( null , "foo" , true ) ]
185+ [ InlineData ( false , "foo" , true ) ]
186+ [ InlineData ( true , "foo" , true ) ]
154187 public void DocumentBuilderOptions ( bool ? omitNullValuedAttributes ,
155188 string attributeValue ,
156189 bool resultContainsAttribute )
@@ -162,7 +195,7 @@ public void DocumentBuilderOptions(bool? omitNullValuedAttributes,
162195 . Returns ( new DocumentBuilderOptions ( omitNullValuedAttributes . Value ) ) ;
163196 }
164197 var documentBuilder = new DocumentBuilder ( _jsonApiContextMock . Object , null , omitNullValuedAttributes . HasValue ? documentBuilderBehaviourMock . Object : null ) ;
165- var document = documentBuilder . Build ( new Model ( ) { StringProperty = attributeValue } ) ;
198+ var document = documentBuilder . Build ( new Model ( ) { StringProperty = attributeValue } ) ;
166199
167200 Assert . Equal ( resultContainsAttribute , document . Data . Attributes . ContainsKey ( "StringProperty" ) ) ;
168201 }
0 commit comments