From 1bc2040800e3565de0b762553ab206f84c7f0ff3 Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Fri, 26 May 2017 18:53:09 -0500 Subject: [PATCH] fix(PATCH): dont update relationships not included in request --- .../Data/DefaultEntityRepository.cs | 6 ++--- .../Serialization/JsonApiDeSerializer.cs | 24 +++++++++---------- .../Services/IJsonApiContext.cs | 1 + .../Services/JsonApiContext.cs | 17 ++++++------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs b/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs index 4630cf3b49..56f55d406e 100644 --- a/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs +++ b/src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs @@ -111,10 +111,8 @@ public virtual async Task UpdateAsync(TId id, TEntity entity) if (oldEntity == null) return null; - _jsonApiContext.RequestEntity.Attributes.ForEach(attr => - { - attr.SetValue(oldEntity, attr.GetValue(entity)); - }); + foreach(var attr in _jsonApiContext.AttributesToUpdate) + attr.Key.SetValue(oldEntity, attr.Value); foreach(var relationship in _jsonApiContext.RelationshipsToUpdate) relationship.Key.SetValue(oldEntity, relationship.Value); diff --git a/src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs b/src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs index fbbd4f69e2..d64d18366f 100644 --- a/src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs +++ b/src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs @@ -27,7 +27,7 @@ public JsonApiDeSerializer( public object Deserialize(string requestBody) { var document = JsonConvert.DeserializeObject(requestBody); - var entity = DataToObject(document.Data); + var entity = DocumentToObject(document.Data); return entity; } @@ -46,7 +46,6 @@ public object DeserializeRelationship(string requestBody) return new List { data.ToObject() }; } - public List DeserializeList(string requestBody) { var documents = JsonConvert.DeserializeObject(requestBody); @@ -54,22 +53,22 @@ public List DeserializeList(string requestBody) var deserializedList = new List(); foreach (var data in documents.Data) { - var entity = DataToObject(data); + var entity = DocumentToObject(data); deserializedList.Add((TEntity)entity); } return deserializedList; } - private object DataToObject(DocumentData data) + private object DocumentToObject(DocumentData data) { var contextEntity = _jsonApiContext.ContextGraph.GetContextEntity(data.Type); _jsonApiContext.RequestEntity = contextEntity; var entity = Activator.CreateInstance(contextEntity.EntityType); - entity = _setEntityAttributes(entity, contextEntity, data.Attributes); - entity = _setRelationships(entity, contextEntity, data.Relationships); + entity = SetEntityAttributes(entity, contextEntity, data.Attributes); + entity = SetRelationships(entity, contextEntity, data.Relationships); var identifiableEntity = (IIdentifiable)entity; @@ -79,7 +78,7 @@ private object DataToObject(DocumentData data) return identifiableEntity; } - private object _setEntityAttributes( + private object SetEntityAttributes( object entity, ContextEntity contextEntity, Dictionary attributeValues) { if (attributeValues == null || attributeValues.Count == 0) @@ -99,13 +98,14 @@ private object _setEntityAttributes( { var convertedValue = TypeHelper.ConvertType(newValue, entityProperty.PropertyType); entityProperty.SetValue(entity, convertedValue); + _jsonApiContext.AttributesToUpdate[attr] = convertedValue; } } return entity; } - private object _setRelationships( + private object SetRelationships( object entity, ContextEntity contextEntity, Dictionary relationships) @@ -118,14 +118,14 @@ private object _setRelationships( foreach (var attr in contextEntity.Relationships) { entity = attr.IsHasOne - ? _setHasOneRelationship(entity, entityProperties, attr, contextEntity, relationships) - : _setHasManyRelationship(entity, entityProperties, attr, contextEntity, relationships); + ? SetHasOneRelationship(entity, entityProperties, attr, contextEntity, relationships) + : SetHasManyRelationship(entity, entityProperties, attr, contextEntity, relationships); } return entity; } - private object _setHasOneRelationship(object entity, + private object SetHasOneRelationship(object entity, PropertyInfo[] entityProperties, RelationshipAttribute attr, ContextEntity contextEntity, @@ -158,7 +158,7 @@ private object _setHasOneRelationship(object entity, return entity; } - private object _setHasManyRelationship(object entity, + private object SetHasManyRelationship(object entity, PropertyInfo[] entityProperties, RelationshipAttribute attr, ContextEntity contextEntity, diff --git a/src/JsonApiDotNetCore/Services/IJsonApiContext.cs b/src/JsonApiDotNetCore/Services/IJsonApiContext.cs index cfc58cec61..3529815f1f 100644 --- a/src/JsonApiDotNetCore/Services/IJsonApiContext.cs +++ b/src/JsonApiDotNetCore/Services/IJsonApiContext.cs @@ -22,6 +22,7 @@ public interface IJsonApiContext PageManager PageManager { get; set; } IMetaBuilder MetaBuilder { get; set; } IGenericProcessorFactory GenericProcessorFactory { get; set; } + Dictionary AttributesToUpdate { get; set; } Dictionary RelationshipsToUpdate { get; set; } } } diff --git a/src/JsonApiDotNetCore/Services/JsonApiContext.cs b/src/JsonApiDotNetCore/Services/JsonApiContext.cs index 908a5c9cf9..17793c3019 100644 --- a/src/JsonApiDotNetCore/Services/JsonApiContext.cs +++ b/src/JsonApiDotNetCore/Services/JsonApiContext.cs @@ -25,7 +25,6 @@ public JsonApiContext( Options = options; MetaBuilder = metaBuilder; GenericProcessorFactory = genericProcessorFactory; - RelationshipsToUpdate = new Dictionary(); } public JsonApiOptions Options { get; set; } @@ -39,7 +38,8 @@ public JsonApiContext( public PageManager PageManager { get; set; } public IMetaBuilder MetaBuilder { get; set; } public IGenericProcessorFactory GenericProcessorFactory { get; set; } - public Dictionary RelationshipsToUpdate { get; set; } + public Dictionary AttributesToUpdate { get; set; } = new Dictionary(); + public Dictionary RelationshipsToUpdate { get; set; } = new Dictionary(); public IJsonApiContext ApplyContext() { @@ -47,8 +47,8 @@ public IJsonApiContext ApplyContext() var path = context.Request.Path.Value.Split('/'); RequestEntity = ContextGraph.GetContextEntity(typeof(T)); - - if(context.Request.Query.Any()) + + if (context.Request.Query.Any()) { QuerySet = new QuerySet(this, context.Request.Query); IncludedRelationships = QuerySet.IncludedRelationships; @@ -63,12 +63,13 @@ public IJsonApiContext ApplyContext() private PageManager GetPageManager() { - if(Options.DefaultPageSize == 0 && (QuerySet == null || QuerySet.PageQuery.PageSize == 0)) + if (Options.DefaultPageSize == 0 && (QuerySet == null || QuerySet.PageQuery.PageSize == 0)) return new PageManager(); - - var query = QuerySet?.PageQuery ?? new PageQuery(); - return new PageManager { + var query = QuerySet?.PageQuery ?? new PageQuery(); + + return new PageManager + { DefaultPageSize = Options.DefaultPageSize, CurrentPage = query.PageOffset > 0 ? query.PageOffset : 1, PageSize = query.PageSize > 0 ? query.PageSize : Options.DefaultPageSize