Skip to content

fix(PATCH): dont update relationships not included in request #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ public virtual async Task<TEntity> 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);
Expand Down
24 changes: 12 additions & 12 deletions src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public JsonApiDeSerializer(
public object Deserialize(string requestBody)
{
var document = JsonConvert.DeserializeObject<Document>(requestBody);
var entity = DataToObject(document.Data);
var entity = DocumentToObject(document.Data);
return entity;
}

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


public List<TEntity> DeserializeList<TEntity>(string requestBody)
{
var documents = JsonConvert.DeserializeObject<Documents>(requestBody);

var deserializedList = new List<TEntity>();
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;

Expand All @@ -79,7 +78,7 @@ private object DataToObject(DocumentData data)
return identifiableEntity;
}

private object _setEntityAttributes(
private object SetEntityAttributes(
object entity, ContextEntity contextEntity, Dictionary<string, object> attributeValues)
{
if (attributeValues == null || attributeValues.Count == 0)
Expand All @@ -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<string, RelationshipData> relationships)
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/JsonApiDotNetCore/Services/IJsonApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public interface IJsonApiContext
PageManager PageManager { get; set; }
IMetaBuilder MetaBuilder { get; set; }
IGenericProcessorFactory GenericProcessorFactory { get; set; }
Dictionary<AttrAttribute, object> AttributesToUpdate { get; set; }
Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; }
}
}
17 changes: 9 additions & 8 deletions src/JsonApiDotNetCore/Services/JsonApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public JsonApiContext(
Options = options;
MetaBuilder = metaBuilder;
GenericProcessorFactory = genericProcessorFactory;
RelationshipsToUpdate = new Dictionary<RelationshipAttribute, object>();
}

public JsonApiOptions Options { get; set; }
Expand All @@ -39,16 +38,17 @@ public JsonApiContext(
public PageManager PageManager { get; set; }
public IMetaBuilder MetaBuilder { get; set; }
public IGenericProcessorFactory GenericProcessorFactory { get; set; }
public Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; }
public Dictionary<AttrAttribute, object> AttributesToUpdate { get; set; } = new Dictionary<AttrAttribute, object>();
public Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; } = new Dictionary<RelationshipAttribute, object>();

public IJsonApiContext ApplyContext<T>()
{
var context = _httpContextAccessor.HttpContext;
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;
Expand All @@ -63,12 +63,13 @@ public IJsonApiContext ApplyContext<T>()

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
Expand Down