Skip to content

fix(relationship pointers): key pointers by RelationshipAttribute #333

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 3 commits into from
Jul 5, 2018
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
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>2.3.4</VersionPrefix>
<VersionPrefix>2.3.5</VersionPrefix>
<TargetFrameworks>$(NetStandardVersion)</TargetFrameworks>
<AssemblyName>JsonApiDotNetCore</AssemblyName>
<PackageId>JsonApiDotNetCore</PackageId>
Expand Down
10 changes: 5 additions & 5 deletions src/JsonApiDotNetCore/Request/HasManyRelationshipPointers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using JsonApiDotNetCore.Models;

namespace JsonApiDotNetCore.Request
{
Expand Down Expand Up @@ -32,18 +32,18 @@ namespace JsonApiDotNetCore.Request
/// </summary>
public class HasManyRelationshipPointers
{
private Dictionary<Type, IList> _hasManyRelationships = new Dictionary<Type, IList>();
private Dictionary<RelationshipAttribute, IList> _hasManyRelationships = new Dictionary<RelationshipAttribute, IList>();

/// <summary>
/// Add the relationship to the list of relationships that should be
/// set in the repository layer.
/// </summary>
public void Add(Type dependentType, IList entities)
=> _hasManyRelationships[dependentType] = entities;
public void Add(RelationshipAttribute relationship, IList entities)
=> _hasManyRelationships[relationship] = entities;

/// <summary>
/// Get all the models that should be associated
/// </summary>
public Dictionary<Type, IList> Get() => _hasManyRelationships;
public Dictionary<RelationshipAttribute, IList> Get() => _hasManyRelationships;
}
}
9 changes: 4 additions & 5 deletions src/JsonApiDotNetCore/Request/HasOneRelationshipPointers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using JsonApiDotNetCore.Models;
using System;
using System.Collections.Generic;

namespace JsonApiDotNetCore.Request
Expand Down Expand Up @@ -29,18 +28,18 @@ namespace JsonApiDotNetCore.Request
/// </summary>
public class HasOneRelationshipPointers
{
private Dictionary<Type, IIdentifiable> _hasOneRelationships = new Dictionary<Type, IIdentifiable>();
private Dictionary<RelationshipAttribute, IIdentifiable> _hasOneRelationships = new Dictionary<RelationshipAttribute, IIdentifiable>();

/// <summary>
/// Add the relationship to the list of relationships that should be
/// set in the repository layer.
/// </summary>
public void Add(Type dependentType, IIdentifiable entity)
=> _hasOneRelationships[dependentType] = entity;
public void Add(RelationshipAttribute relationship, IIdentifiable entity)
=> _hasOneRelationships[relationship] = entity;

/// <summary>
/// Get all the models that should be associated
/// </summary>
public Dictionary<Type, IIdentifiable> Get() => _hasOneRelationships;
public Dictionary<RelationshipAttribute, IIdentifiable> Get() => _hasOneRelationships;
}
}
16 changes: 8 additions & 8 deletions src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public object DocumentToObject(DocumentData data, List<DocumentData> included =
+ "If you have manually registered the resource, check that the call to AddResource correctly sets the public name."); ;

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

entity = SetEntityAttributes(entity, contextEntity, data.Attributes);
entity = SetRelationships(entity, contextEntity, data.Relationships, included);

Expand All @@ -141,7 +141,7 @@ private object SetEntityAttributes(
{
if (attributeValues == null || attributeValues.Count == 0)
return entity;

foreach (var attr in contextEntity.Attributes)
{
if (attributeValues.TryGetValue(attr.PublicAttributeName, out object newValue))
Expand Down Expand Up @@ -174,7 +174,7 @@ private object DeserializeComplexType(JContainer obj, Type targetType)
private object SetRelationships(
object entity,
ContextEntity contextEntity,
Dictionary<string, RelationshipData> relationships,
Dictionary<string, RelationshipData> relationships,
List<DocumentData> included = null)
{
if (relationships == null || relationships.Count == 0)
Expand Down Expand Up @@ -203,7 +203,7 @@ private object SetHasOneRelationship(object entity,

if (relationships.TryGetValue(relationshipName, out RelationshipData relationshipData) == false)
return entity;

var relationshipAttr = _jsonApiContext.RequestEntity.Relationships
.SingleOrDefault(r => r.PublicRelationshipName == relationshipName);

Expand Down Expand Up @@ -234,7 +234,7 @@ private object SetHasOneRelationship(object entity,
foreignKeyProperty.SetValue(entity, convertedValue);


if(rio != null
if (rio != null
// if the resource identifier is null, there should be no reason to instantiate an instance
&& rio.Id != null)
{
Expand All @@ -247,7 +247,7 @@ private object SetHasOneRelationship(object entity,
// we need to store the fact that this relationship was included in the payload
// for EF, the repository will use these pointers to make ensure we don't try to
// create resources if they already exist, we just need to create the relationship
_jsonApiContext.HasOneRelationshipPointers.Add(attr.Type, includedRelationshipObject);
_jsonApiContext.HasOneRelationshipPointers.Add(attr, includedRelationshipObject);
}

return entity;
Expand Down Expand Up @@ -278,7 +278,7 @@ private object SetHasManyRelationship(object entity,

attr.SetValue(entity, convertedCollection);

_jsonApiContext.HasManyRelationshipPointers.Add(attr.Type, convertedCollection);
_jsonApiContext.HasManyRelationshipPointers.Add(attr, convertedCollection);
}

return entity;
Expand All @@ -301,7 +301,7 @@ private IIdentifiable GetIncludedRelationship(ResourceIdentifierObject relatedRe
var contextEntity = _jsonApiContext.ContextGraph.GetContextEntity(relationshipAttr.Type);
if (contextEntity == null)
throw new JsonApiException(400, $"Included type '{relationshipAttr.Type}' is not a registered json:api resource.");

SetEntityAttributes(relatedInstance, contextEntity, includedResource.Attributes);

return relatedInstance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Bogus;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Data;
using JsonApiDotNetCoreExample.Models;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Xunit;
using Person = JsonApiDotNetCoreExample.Models.Person;
Expand Down Expand Up @@ -304,6 +306,74 @@ public async Task Can_Post_TodoItem()
Assert.Null(deserializedBody.AchievedDate);
}


[Fact]
public async Task Can_Post_TodoItem_With_Different_Owner_And_Assignee()
{
// Arrange
var person1 = new Person();
var person2 = new Person();
_context.People.Add(person1);
_context.People.Add(person2);
_context.SaveChanges();

var todoItem = _todoItemFaker.Generate();
var content = new
{
data = new
{
type = "todo-items",
attributes = new Dictionary<string, object>()
{
{ "description", todoItem.Description },
{ "ordinal", todoItem.Ordinal },
{ "created-date", todoItem.CreatedDate }
},
relationships = new
{
owner = new
{
data = new
{
type = "people",
id = person1.Id.ToString()
}
},
assignee = new
{
data = new
{
type = "people",
id = person2.Id.ToString()
}
}
}
}
};

var httpMethod = new HttpMethod("POST");
var route = $"/api/v1/todo-items";

var request = new HttpRequestMessage(httpMethod, route);
request.Content = new StringContent(JsonConvert.SerializeObject(content));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");

// Act
var response = await _fixture.Client.SendAsync(request);

// Assert -- response
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
var document = JsonConvert.DeserializeObject<Document>(body);
var resultId = int.Parse(document.Data.Id);

// Assert -- database
var todoItemResult = await _context.TodoItems.SingleAsync(t => t.Id == resultId);

Assert.Equal(person1.Id, todoItemResult.OwnerId);
Assert.Equal(person2.Id, todoItemResult.AssigneeId);
}

[Fact]
public async Task Can_Patch_TodoItem()
{
Expand Down