Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 3e0e78d

Browse files
committed
Add new polymorphic collection test
1 parent 935225f commit 3e0e78d

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

tests/ServiceStack.Text.Tests/JsonTests/PolymorphicListTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,5 +502,46 @@ public void polymorphic_serialization_of_class_implementing_generic_ienumerable_
502502
Assert.IsAssignableFrom<FooTerm>(terms2.First());
503503
}
504504

505+
[Test]
506+
public void Serialize_Polymorphic_collection()
507+
{
508+
var dto = new PolymorphicContainer
509+
{
510+
items = new List<PolymorphicBase>
511+
{
512+
new PolymorphicA { id = 1, fieldA = "testingA" },
513+
new PolymorphicB { id = 2, fieldB = "testingB" },
514+
}
515+
};
516+
517+
var json = dto.ToJson();
518+
519+
var fromJson = json.FromJson<PolymorphicContainer>();
520+
//fromJson.PrintDump();
521+
522+
Assert.That(fromJson.items.Count, Is.EqualTo(2));
523+
Assert.That(((PolymorphicB)fromJson.items[1]).fieldB, Is.EqualTo("testingB"));
524+
}
505525
}
526+
527+
public abstract class PolymorphicBase
528+
{
529+
public int id { get; set; }
530+
}
531+
532+
public class PolymorphicA : PolymorphicBase
533+
{
534+
public string fieldA { get; set; }
535+
}
536+
537+
public class PolymorphicB : PolymorphicBase
538+
{
539+
public string fieldB { get; set; }
540+
}
541+
542+
public class PolymorphicContainer
543+
{
544+
public List<PolymorphicBase> items { get; set; }
545+
}
546+
506547
}

0 commit comments

Comments
 (0)