Skip to content

Commit 1bc2040

Browse files
committed
fix(PATCH): dont update relationships not included in request
1 parent f765d74 commit 1bc2040

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,8 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
111111
if (oldEntity == null)
112112
return null;
113113

114-
_jsonApiContext.RequestEntity.Attributes.ForEach(attr =>
115-
{
116-
attr.SetValue(oldEntity, attr.GetValue(entity));
117-
});
114+
foreach(var attr in _jsonApiContext.AttributesToUpdate)
115+
attr.Key.SetValue(oldEntity, attr.Value);
118116

119117
foreach(var relationship in _jsonApiContext.RelationshipsToUpdate)
120118
relationship.Key.SetValue(oldEntity, relationship.Value);

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public JsonApiDeSerializer(
2727
public object Deserialize(string requestBody)
2828
{
2929
var document = JsonConvert.DeserializeObject<Document>(requestBody);
30-
var entity = DataToObject(document.Data);
30+
var entity = DocumentToObject(document.Data);
3131
return entity;
3232
}
3333

@@ -46,30 +46,29 @@ public object DeserializeRelationship(string requestBody)
4646
return new List<DocumentData> { data.ToObject<DocumentData>() };
4747
}
4848

49-
5049
public List<TEntity> DeserializeList<TEntity>(string requestBody)
5150
{
5251
var documents = JsonConvert.DeserializeObject<Documents>(requestBody);
5352

5453
var deserializedList = new List<TEntity>();
5554
foreach (var data in documents.Data)
5655
{
57-
var entity = DataToObject(data);
56+
var entity = DocumentToObject(data);
5857
deserializedList.Add((TEntity)entity);
5958
}
6059

6160
return deserializedList;
6261
}
6362

64-
private object DataToObject(DocumentData data)
63+
private object DocumentToObject(DocumentData data)
6564
{
6665
var contextEntity = _jsonApiContext.ContextGraph.GetContextEntity(data.Type);
6766
_jsonApiContext.RequestEntity = contextEntity;
6867

6968
var entity = Activator.CreateInstance(contextEntity.EntityType);
7069

71-
entity = _setEntityAttributes(entity, contextEntity, data.Attributes);
72-
entity = _setRelationships(entity, contextEntity, data.Relationships);
70+
entity = SetEntityAttributes(entity, contextEntity, data.Attributes);
71+
entity = SetRelationships(entity, contextEntity, data.Relationships);
7372

7473
var identifiableEntity = (IIdentifiable)entity;
7574

@@ -79,7 +78,7 @@ private object DataToObject(DocumentData data)
7978
return identifiableEntity;
8079
}
8180

82-
private object _setEntityAttributes(
81+
private object SetEntityAttributes(
8382
object entity, ContextEntity contextEntity, Dictionary<string, object> attributeValues)
8483
{
8584
if (attributeValues == null || attributeValues.Count == 0)
@@ -99,13 +98,14 @@ private object _setEntityAttributes(
9998
{
10099
var convertedValue = TypeHelper.ConvertType(newValue, entityProperty.PropertyType);
101100
entityProperty.SetValue(entity, convertedValue);
101+
_jsonApiContext.AttributesToUpdate[attr] = convertedValue;
102102
}
103103
}
104104

105105
return entity;
106106
}
107107

108-
private object _setRelationships(
108+
private object SetRelationships(
109109
object entity,
110110
ContextEntity contextEntity,
111111
Dictionary<string, RelationshipData> relationships)
@@ -118,14 +118,14 @@ private object _setRelationships(
118118
foreach (var attr in contextEntity.Relationships)
119119
{
120120
entity = attr.IsHasOne
121-
? _setHasOneRelationship(entity, entityProperties, attr, contextEntity, relationships)
122-
: _setHasManyRelationship(entity, entityProperties, attr, contextEntity, relationships);
121+
? SetHasOneRelationship(entity, entityProperties, attr, contextEntity, relationships)
122+
: SetHasManyRelationship(entity, entityProperties, attr, contextEntity, relationships);
123123
}
124124

125125
return entity;
126126
}
127127

128-
private object _setHasOneRelationship(object entity,
128+
private object SetHasOneRelationship(object entity,
129129
PropertyInfo[] entityProperties,
130130
RelationshipAttribute attr,
131131
ContextEntity contextEntity,
@@ -158,7 +158,7 @@ private object _setHasOneRelationship(object entity,
158158
return entity;
159159
}
160160

161-
private object _setHasManyRelationship(object entity,
161+
private object SetHasManyRelationship(object entity,
162162
PropertyInfo[] entityProperties,
163163
RelationshipAttribute attr,
164164
ContextEntity contextEntity,

src/JsonApiDotNetCore/Services/IJsonApiContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public interface IJsonApiContext
2222
PageManager PageManager { get; set; }
2323
IMetaBuilder MetaBuilder { get; set; }
2424
IGenericProcessorFactory GenericProcessorFactory { get; set; }
25+
Dictionary<AttrAttribute, object> AttributesToUpdate { get; set; }
2526
Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; }
2627
}
2728
}

src/JsonApiDotNetCore/Services/JsonApiContext.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public JsonApiContext(
2525
Options = options;
2626
MetaBuilder = metaBuilder;
2727
GenericProcessorFactory = genericProcessorFactory;
28-
RelationshipsToUpdate = new Dictionary<RelationshipAttribute, object>();
2928
}
3029

3130
public JsonApiOptions Options { get; set; }
@@ -39,16 +38,17 @@ public JsonApiContext(
3938
public PageManager PageManager { get; set; }
4039
public IMetaBuilder MetaBuilder { get; set; }
4140
public IGenericProcessorFactory GenericProcessorFactory { get; set; }
42-
public Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; }
41+
public Dictionary<AttrAttribute, object> AttributesToUpdate { get; set; } = new Dictionary<AttrAttribute, object>();
42+
public Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; } = new Dictionary<RelationshipAttribute, object>();
4343

4444
public IJsonApiContext ApplyContext<T>()
4545
{
4646
var context = _httpContextAccessor.HttpContext;
4747
var path = context.Request.Path.Value.Split('/');
4848

4949
RequestEntity = ContextGraph.GetContextEntity(typeof(T));
50-
51-
if(context.Request.Query.Any())
50+
51+
if (context.Request.Query.Any())
5252
{
5353
QuerySet = new QuerySet(this, context.Request.Query);
5454
IncludedRelationships = QuerySet.IncludedRelationships;
@@ -63,12 +63,13 @@ public IJsonApiContext ApplyContext<T>()
6363

6464
private PageManager GetPageManager()
6565
{
66-
if(Options.DefaultPageSize == 0 && (QuerySet == null || QuerySet.PageQuery.PageSize == 0))
66+
if (Options.DefaultPageSize == 0 && (QuerySet == null || QuerySet.PageQuery.PageSize == 0))
6767
return new PageManager();
68-
69-
var query = QuerySet?.PageQuery ?? new PageQuery();
7068

71-
return new PageManager {
69+
var query = QuerySet?.PageQuery ?? new PageQuery();
70+
71+
return new PageManager
72+
{
7273
DefaultPageSize = Options.DefaultPageSize,
7374
CurrentPage = query.PageOffset > 0 ? query.PageOffset : 1,
7475
PageSize = query.PageSize > 0 ? query.PageSize : Options.DefaultPageSize

0 commit comments

Comments
 (0)