@@ -96,9 +96,7 @@ protected IIdentifiable SetRelationships(IIdentifiable entity, Dictionary<string
96
96
var entityProperties = entity . GetType ( ) . GetProperties ( ) ;
97
97
foreach ( var attr in relationshipAttributes )
98
98
{
99
- relationshipsValues . TryGetValue ( attr . PublicRelationshipName , out RelationshipData relationshipData ) ;
100
-
101
- if ( relationshipData == null || ! relationshipData . HasData )
99
+ if ( ! relationshipsValues . TryGetValue ( attr . PublicRelationshipName , out RelationshipData relationshipData ) || ! relationshipData . IsPopulated )
102
100
continue ;
103
101
104
102
if ( attr is HasOneAttribute hasOne )
@@ -120,7 +118,6 @@ private JToken LoadJToken(string body)
120
118
return jToken ;
121
119
}
122
120
123
-
124
121
/// <summary>
125
122
/// Creates an instance of the referenced type in <paramref name="data"/>
126
123
/// and sets its attributes and relationships
@@ -227,15 +224,18 @@ private object SetHasManyRelationship(IIdentifiable entity,
227
224
HasManyAttribute attr ,
228
225
RelationshipData relationshipData )
229
226
{
230
- var relatedResources = relationshipData . ManyData . Select ( rio =>
231
- {
232
- var relatedInstance = attr . DependentType . New < IIdentifiable > ( ) ;
233
- relatedInstance . StringId = rio . Id ;
234
- return relatedInstance ;
235
- } ) ;
227
+ if ( relationshipData . Data != null )
228
+ { // if the relationship is set to null, no need to set the navigation property to null: this is the default value.
229
+ var relatedResources = relationshipData . ManyData . Select ( rio =>
230
+ {
231
+ var relatedInstance = attr . DependentType . New < IIdentifiable > ( ) ;
232
+ relatedInstance . StringId = rio . Id ;
233
+ return relatedInstance ;
234
+ } ) ;
235
+ var convertedCollection = TypeHelper . ConvertCollection ( relatedResources , attr . DependentType ) ;
236
+ attr . SetValue ( entity , convertedCollection ) ;
237
+ }
236
238
237
- var convertedCollection = TypeHelper . ConvertCollection ( relatedResources , attr . DependentType ) ;
238
- attr . SetValue ( entity , convertedCollection ) ;
239
239
AfterProcessField ( entity , attr , relationshipData ) ;
240
240
241
241
return entity ;
0 commit comments