Skip to content

Commit 926a945

Browse files
committed
test: expose bug
1 parent 0416df7 commit 926a945

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
5050
.WithOne(t => t.ParentTodoItem)
5151
.HasForeignKey(t => t.ParentTodoItemId);
5252

53+
modelBuilder.Entity<Person>()
54+
.HasOne(p => p.Passport)
55+
.WithOne(p => p.Person)
56+
.HasForeignKey<Person>(p => p.PassportId);
5357

5458
}
5559

@@ -61,13 +65,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
6165
public DbSet<Author> Authors { get; set; }
6266
public DbSet<NonJsonApiResource> NonJsonApiResources { get; set; }
6367
public DbSet<User> Users { get; set; }
64-
6568
public DbSet<CourseEntity> Courses { get; set; }
6669
public DbSet<DepartmentEntity> Departments { get; set; }
6770
public DbSet<CourseStudentEntity> Registrations { get; set; }
6871
public DbSet<StudentEntity> Students { get; set; }
6972
public DbSet<PersonRole> PersonRoles { get; set; }
7073
public DbSet<ArticleTag> ArticleTags { get; set; }
7174
public DbSet<Tag> Tags { get; set; }
75+
public DbSet<Passport> Passports { get; set; }
7276
}
7377
}
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
@@ -38,6 +38,10 @@ public class Person : Identifiable, IHasMeta
3838
[HasOne("unincludeable-item", documentLinks: Link.All, canInclude: false)]
3939
public virtual TodoItem UnIncludeableItem { get; set; }
4040

41+
public int? PassportId { get; set; }
42+
[HasOne("passport")]
43+
public virtual Passport Passport { get; set; }
44+
4145
public Dictionary<string, object> GetMeta(IJsonApiContext context)
4246
{
4347
return new Dictionary<string, object> {

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/UpdatingRelationshipsTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,5 +621,55 @@ 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+
630+
// Arrange
631+
var context = _fixture.GetService<AppDbContext>();
632+
var passport = new Passport();
633+
var person1 = _personFaker.Generate();
634+
person1.Passport = passport;
635+
var person2 = _personFaker.Generate();
636+
context.People.AddRange(new List<Person>() { person1, person2 });
637+
await context.SaveChangesAsync();
638+
639+
640+
641+
var content = new
642+
{
643+
data = new
644+
{
645+
type = "people",
646+
id = person2.Id,
647+
relationships = new Dictionary<string, object>
648+
{
649+
{ "passport", new
650+
{
651+
data = new { type = "passports", id = $"{person1.PassportId}" }
652+
}
653+
}
654+
}
655+
}
656+
};
657+
658+
var httpMethod = new HttpMethod("PATCH");
659+
var route = $"/api/v1/people/{person2.Id}";
660+
var request = new HttpRequestMessage(httpMethod, route);
661+
662+
string serializedContent = JsonConvert.SerializeObject(content);
663+
request.Content = new StringContent(serializedContent);
664+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
665+
666+
// Act
667+
var response = await _fixture.Client.SendAsync(request);
668+
669+
// Assert
670+
var body = await response.Content.ReadAsStringAsync();
671+
Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with payload: {body}");
672+
673+
}
624674
}
625675
}

0 commit comments

Comments
 (0)