Skip to content

Commit 1a367fa

Browse files
committed
this is a big one
1 parent daa61ab commit 1a367fa

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

Diff for: test/UnitTests/Serialization/JsonApiDeSerializerTests.cs

+159
Original file line numberDiff line numberDiff line change
@@ -532,17 +532,176 @@ public void Sets_Attribute_Values_On_Included_HasOne_Relationships()
532532
Assert.Equal(expectedName, result.Independent.Name);
533533
}
534534

535+
536+
[Fact]
537+
public void Can_Deserialize_Nested_Included_HasMany_Relationships()
538+
{
539+
// arrange
540+
var contextGraphBuilder = new ContextGraphBuilder();
541+
contextGraphBuilder.AddResource<OneToManyIndependent>("independents");
542+
contextGraphBuilder.AddResource<OneToManyDependent>("dependents");
543+
contextGraphBuilder.AddResource<ManyToManyNested>("many-to-manys");
544+
545+
var deserializer = GetDeserializer(contextGraphBuilder);
546+
547+
var contentString =
548+
@"{
549+
""data"": {
550+
""type"": ""independents"",
551+
""id"": ""1"",
552+
""attributes"": { },
553+
""relationships"": {
554+
""many-to-manys"": {
555+
""data"": [{
556+
""type"": ""many-to-manys"",
557+
""id"": ""2""
558+
}, {
559+
""type"": ""many-to-manys"",
560+
""id"": ""3""
561+
}]
562+
}
563+
}
564+
},
565+
""included"": [
566+
{
567+
""type"": ""many-to-manys"",
568+
""id"": ""2"",
569+
""attributes"": {},
570+
""relationships"": {
571+
""dependent"": {
572+
""data"": {
573+
""type"": ""dependents"",
574+
""id"": ""4""
575+
}
576+
},
577+
""independent"": {
578+
""data"": {
579+
""type"": ""independents"",
580+
""id"": ""5""
581+
}
582+
}
583+
}
584+
},
585+
{
586+
""type"": ""many-to-manys"",
587+
""id"": ""3"",
588+
""attributes"": {},
589+
""relationships"": {
590+
""dependent"": {
591+
""data"": {
592+
""type"": ""dependents"",
593+
""id"": ""4""
594+
}
595+
},
596+
""independent"": {
597+
""data"": {
598+
""type"": ""independents"",
599+
""id"": ""6""
600+
}
601+
}
602+
}
603+
},
604+
{
605+
""type"": ""dependents"",
606+
""id"": ""4"",
607+
""attributes"": {},
608+
""relationships"": {
609+
""many-to-manys"": {
610+
""data"": [{
611+
""type"": ""many-to-manys"",
612+
""id"": ""2""
613+
}, {
614+
""type"": ""many-to-manys"",
615+
""id"": ""3""
616+
}]
617+
}
618+
}
619+
}
620+
,
621+
{
622+
""type"": ""independents"",
623+
""id"": ""5"",
624+
""attributes"": {},
625+
""relationships"": {
626+
""many-to-manys"": {
627+
""data"": [{
628+
""type"": ""many-to-manys"",
629+
""id"": ""2""
630+
}]
631+
}
632+
}
633+
}
634+
,
635+
{
636+
""type"": ""independents"",
637+
""id"": ""6"",
638+
""attributes"": {},
639+
""relationships"": {
640+
""many-to-manys"": {
641+
""data"": [{
642+
""type"": ""many-to-manys"",
643+
""id"": ""3""
644+
}]
645+
}
646+
}
647+
}
648+
]
649+
}";
650+
651+
// act
652+
var result = deserializer.Deserialize<OneToManyDependent>(contentString);
653+
654+
// assert
655+
Assert.NotNull(result);
656+
Assert.Equal(1, result.Id);
657+
Assert.NotNull(result.ManyToManys);
658+
Assert.Equal(2, result.ManyToManys.Count);
659+
}
660+
661+
private JsonApiDeSerializer GetDeserializer(ContextGraphBuilder contextGraphBuilder)
662+
{
663+
var contextGraph = contextGraphBuilder.Build();
664+
665+
var jsonApiContextMock = new Mock<IJsonApiContext>();
666+
jsonApiContextMock.SetupAllProperties();
667+
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
668+
jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>());
669+
jsonApiContextMock.Setup(m => m.RelationshipsToUpdate).Returns(new Dictionary<RelationshipAttribute, object>());
670+
jsonApiContextMock.Setup(m => m.HasManyRelationshipPointers).Returns(new HasManyRelationshipPointers());
671+
jsonApiContextMock.Setup(m => m.HasOneRelationshipPointers).Returns(new HasOneRelationshipPointers());
672+
673+
var jsonApiOptions = new JsonApiOptions();
674+
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);
675+
676+
var deserializer = new JsonApiDeSerializer(jsonApiContextMock.Object);
677+
678+
return deserializer;
679+
}
680+
681+
private class ManyToManyNested : Identifiable
682+
{
683+
[Attr("name")] public string Name { get; set; }
684+
[HasOne("dependent")] public OneToManyDependent Dependents { get; set; }
685+
public int DependentId { get; set; }
686+
[HasOne("independent")] public OneToManyIndependent Independents { get; set; }
687+
public int InependentId { get; set; }
688+
}
689+
535690
private class OneToManyDependent : Identifiable
536691
{
537692
[Attr("name")] public string Name { get; set; }
538693
[HasOne("independent")] public OneToManyIndependent Independent { get; set; }
539694
public int IndependentId { get; set; }
695+
696+
[HasMany("many-to-manys")] public List<ManyToManyNested> ManyToManys { get; set; }
540697
}
541698

542699
private class OneToManyIndependent : Identifiable
543700
{
544701
[Attr("name")] public string Name { get; set; }
545702
[HasMany("dependents")] public List<OneToManyDependent> Dependents { get; set; }
703+
704+
[HasMany("many-to-manys")] public List<ManyToManyNested> ManyToManys { get; set; }
546705
}
547706
}
548707
}

0 commit comments

Comments
 (0)