@@ -13,7 +13,7 @@ public class DocumentBuilder : IDocumentBuilder
13
13
private readonly IJsonApiContext _jsonApiContext ;
14
14
private readonly IContextGraph _contextGraph ;
15
15
private readonly IRequestMeta _requestMeta ;
16
- private readonly DocumentBuilderOptions _documentBuilderOptions ;
16
+ private readonly DocumentBuilderOptions _documentBuilderOptions = new DocumentBuilderOptions ( ) ;
17
17
18
18
public DocumentBuilder ( IJsonApiContext jsonApiContext )
19
19
{
@@ -33,7 +33,7 @@ public DocumentBuilder(IJsonApiContext jsonApiContext, IRequestMeta requestMeta,
33
33
_jsonApiContext = jsonApiContext ;
34
34
_contextGraph = jsonApiContext . ContextGraph ;
35
35
_requestMeta = requestMeta ;
36
- _documentBuilderOptions = documentBuilderBehavior ? . GetDocumentBuilderOptions ( ) ?? new DocumentBuilderOptions ( ) ;
36
+ _documentBuilderOptions = documentBuilderBehavior ? . GetDocumentBuilderOptions ( ) ?? _documentBuilderOptions ;
37
37
}
38
38
39
39
public Document Build ( IIdentifiable entity )
@@ -127,13 +127,10 @@ private DocumentData GetData(ContextEntity contextEntity, IIdentifiable entity)
127
127
128
128
contextEntity . Attributes . ForEach ( attr =>
129
129
{
130
- if ( ShouldIncludeAttribute ( attr ) )
130
+ var attributeValue = attr . GetValue ( entity ) ;
131
+ if ( ShouldIncludeAttribute ( attr , attributeValue ) )
131
132
{
132
- var value = attr . GetValue ( entity ) ;
133
- if ( value != null || _documentBuilderOptions . IncludeNullAttributes )
134
- {
135
- data . Attributes . Add ( attr . PublicAttributeName , value ) ;
136
- }
133
+ data . Attributes . Add ( attr . PublicAttributeName , attributeValue ) ;
137
134
}
138
135
} ) ;
139
136
@@ -143,11 +140,17 @@ private DocumentData GetData(ContextEntity contextEntity, IIdentifiable entity)
143
140
return data ;
144
141
}
145
142
146
- private bool ShouldIncludeAttribute ( AttrAttribute attr )
143
+ private bool ShouldIncludeAttribute ( AttrAttribute attr , object attributeValue )
147
144
{
148
- return ( _jsonApiContext . QuerySet == null
149
- || _jsonApiContext . QuerySet . Fields . Count == 0
150
- || _jsonApiContext . QuerySet . Fields . Contains ( attr . InternalAttributeName ) ) ;
145
+ return ! ExcludeNullValuedAttribute ( attr , attributeValue )
146
+ && ( ( _jsonApiContext . QuerySet == null
147
+ || _jsonApiContext . QuerySet . Fields . Count == 0 )
148
+ || _jsonApiContext . QuerySet . Fields . Contains ( attr . InternalAttributeName ) ) ;
149
+ }
150
+
151
+ private bool ExcludeNullValuedAttribute ( AttrAttribute attr , object attributeValue )
152
+ {
153
+ return attributeValue == null && _documentBuilderOptions . ExcludeNullValuedAttributes ;
151
154
}
152
155
153
156
private void AddRelationships ( DocumentData data , ContextEntity contextEntity , IIdentifiable entity )
0 commit comments