Skip to content

Commit 0304725

Browse files
committed
test: add implicit removal test
1 parent 24dd97c commit 0304725

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using JsonApiDotNetCore.Models;
2+
3+
namespace JsonApiDotNetCoreExample.Models
4+
{
5+
public class Passport : Identifiable
6+
{
7+
public virtual int? SocialSecurityNumber { get; set; }
8+
[HasOne("person")]
9+
public virtual Person Person { get; set; }
10+
}
11+
}

src/Examples/JsonApiDotNetCoreExample/Models/Person.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,9 @@ public Dictionary<string, object> GetMeta(IJsonApiContext context)
4545
{ "authors", new string[] { "Jared Nance" } }
4646
};
4747
}
48+
public int? PassportId { get; set; }
49+
[HasOne("passport")]
50+
public virtual Passport Passport { get; set; }
51+
4852
}
4953
}

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
242242
}
243243
else if (relationshipEntry.Key is HasOneAttribute hasOneAttribute)
244244
{
245-
//var foreignkeyValue = relationshipValue.GetType().GetProperty(hasOneAttribute.IdentifiablePropertyName).GetValue(oldEntity);
246245
hasOneAttribute.SetValue(oldEntity, relationshipValue);
247246
}
248247
}

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/UpdatingRelationshipsTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,5 +621,52 @@ public async Task Can_Delete_Relationship_By_Patching_Relationship()
621621
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
622622
Assert.Null(todoItemResult.Owner);
623623
}
624+
625+
[Fact]
626+
public async Task Updating_ToOne_Relationship_With_Implicit_Remove()
627+
{
628+
629+
// Arrange
630+
var context = _fixture.GetService<AppDbContext>();
631+
var passport = new Passport();
632+
var person1 = _personFaker.Generate();
633+
person1.Passport = passport;
634+
var person2 = _personFaker.Generate();
635+
context.People.AddRange(new List<Person>() { person1, person2 });
636+
await context.SaveChangesAsync();
637+
638+
var content = new
639+
{
640+
data = new
641+
{
642+
type = "people",
643+
id = person2.Id,
644+
relationships = new Dictionary<string, object>
645+
{
646+
{ "passport", new
647+
{
648+
data = new { type = "passports", id = $"{person1.PassportId}" }
649+
}
650+
}
651+
}
652+
}
653+
};
654+
655+
var httpMethod = new HttpMethod("PATCH");
656+
var route = $"/api/v1/people/{person2.Id}";
657+
var request = new HttpRequestMessage(httpMethod, route);
658+
659+
string serializedContent = JsonConvert.SerializeObject(content);
660+
request.Content = new StringContent(serializedContent);
661+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
662+
663+
// Act
664+
var response = await _fixture.Client.SendAsync(request);
665+
666+
// Assert
667+
var body = await response.Content.ReadAsStringAsync();
668+
Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with payload: {body}");
669+
670+
}
624671
}
625672
}

0 commit comments

Comments
 (0)