1+ using System ;
12using System . Collections ;
23using System . Collections . Generic ;
34using System . Linq ;
@@ -14,22 +15,29 @@ public class DocumentBuilder : IDocumentBuilder
1415 private readonly IContextGraph _contextGraph ;
1516 private readonly IRequestMeta _requestMeta ;
1617 private readonly DocumentBuilderOptions _documentBuilderOptions ;
18+ private readonly IScopedServiceProvider _scopedServiceProvider ;
1719
18- public DocumentBuilder ( IJsonApiContext jsonApiContext , IRequestMeta requestMeta = null , IDocumentBuilderOptionsProvider documentBuilderOptionsProvider = null )
20+ public DocumentBuilder (
21+ IJsonApiContext jsonApiContext ,
22+ IRequestMeta requestMeta = null ,
23+ IDocumentBuilderOptionsProvider documentBuilderOptionsProvider = null ,
24+ IScopedServiceProvider scopedServiceProvider = null )
1925 {
2026 _jsonApiContext = jsonApiContext ;
2127 _contextGraph = jsonApiContext . ContextGraph ;
2228 _requestMeta = requestMeta ;
23- _documentBuilderOptions = documentBuilderOptionsProvider ? . GetDocumentBuilderOptions ( ) ?? new DocumentBuilderOptions ( ) ; ;
29+ _documentBuilderOptions = documentBuilderOptionsProvider ? . GetDocumentBuilderOptions ( ) ?? new DocumentBuilderOptions ( ) ;
30+ _scopedServiceProvider = scopedServiceProvider ;
2431 }
2532
2633 public Document Build ( IIdentifiable entity )
2734 {
2835 var contextEntity = _contextGraph . GetContextEntity ( entity . GetType ( ) ) ;
2936
37+ var resourceDefinition = _scopedServiceProvider ? . GetService ( contextEntity . ResourceType ) as IResourceDefinition ;
3038 var document = new Document
3139 {
32- Data = GetData ( contextEntity , entity ) ,
40+ Data = GetData ( contextEntity , entity , resourceDefinition ) ,
3341 Meta = GetMeta ( entity )
3442 } ;
3543
@@ -44,8 +52,8 @@ public Document Build(IIdentifiable entity)
4452 public Documents Build ( IEnumerable < IIdentifiable > entities )
4553 {
4654 var entityType = entities . GetElementType ( ) ;
47-
4855 var contextEntity = _contextGraph . GetContextEntity ( entityType ) ;
56+ var resourceDefinition = _scopedServiceProvider ? . GetService ( contextEntity . ResourceType ) as IResourceDefinition ;
4957
5058 var enumeratedEntities = entities as IList < IIdentifiable > ?? entities . ToList ( ) ;
5159 var documents = new Documents
@@ -59,7 +67,7 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
5967
6068 foreach ( var entity in enumeratedEntities )
6169 {
62- documents . Data . Add ( GetData ( contextEntity , entity ) ) ;
70+ documents . Data . Add ( GetData ( contextEntity , entity , resourceDefinition ) ) ;
6371 documents . Included = AppendIncludedObject ( documents . Included , contextEntity , entity ) ;
6472 }
6573
@@ -98,7 +106,11 @@ private List<DocumentData> AppendIncludedObject(List<DocumentData> includedObjec
98106 return includedObject ;
99107 }
100108
109+ [ Obsolete ( "You should specify an IResourceDefinition implementation using the GetData/3 overload." ) ]
101110 public DocumentData GetData ( ContextEntity contextEntity , IIdentifiable entity )
111+ => GetData ( contextEntity , entity , resourceDefinition : null ) ;
112+
113+ public DocumentData GetData ( ContextEntity contextEntity , IIdentifiable entity , IResourceDefinition resourceDefinition = null )
102114 {
103115 var data = new DocumentData
104116 {
@@ -111,7 +123,8 @@ public DocumentData GetData(ContextEntity contextEntity, IIdentifiable entity)
111123
112124 data . Attributes = new Dictionary < string , object > ( ) ;
113125
114- contextEntity . Attributes . ForEach ( attr =>
126+ var resourceAttributes = resourceDefinition ? . GetOutputAttrs ( entity ) ?? contextEntity . Attributes ;
127+ resourceAttributes . ForEach ( attr =>
115128 {
116129 var attributeValue = attr . GetValue ( entity ) ;
117130 if ( ShouldIncludeAttribute ( attr , attributeValue ) )
@@ -219,8 +232,9 @@ private DocumentData GetIncludedEntity(IIdentifiable entity)
219232 if ( entity == null ) return null ;
220233
221234 var contextEntity = _jsonApiContext . ContextGraph . GetContextEntity ( entity . GetType ( ) ) ;
235+ var resourceDefinition = _scopedServiceProvider . GetService ( contextEntity . ResourceType ) as IResourceDefinition ;
222236
223- var data = GetData ( contextEntity , entity ) ;
237+ var data = GetData ( contextEntity , entity , resourceDefinition ) ;
224238
225239 data . Attributes = new Dictionary < string , object > ( ) ;
226240
0 commit comments