|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
| 4 | +using System.Collections.Generic; |
4 | 5 | using System.Text.Json; |
5 | 6 | using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Converters; |
6 | 7 | using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Exceptions; |
@@ -69,6 +70,41 @@ public void NonGenericPatchDocToGenericMustSerialize() |
69 | 70 | Assert.Equal("A", targetObject.AnotherStringProperty); |
70 | 71 | } |
71 | 72 |
|
| 73 | + public class Employee |
| 74 | + { |
| 75 | + public int EmployeeId { get; set; } |
| 76 | + public string Name { get; set; } |
| 77 | + } |
| 78 | + |
| 79 | + public class SalariedEmployee : Employee |
| 80 | + { |
| 81 | + public decimal AnnualSalary { get; set; } |
| 82 | + } |
| 83 | + |
| 84 | + public class Organization |
| 85 | + { |
| 86 | + public List<Employee> Employees { get; } = new(); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void ListWithGenericTypeWorkForSpecificChildren() |
| 91 | + { |
| 92 | + //Arrange |
| 93 | + var org = new Organization(); |
| 94 | + // Populate Employees with two employees |
| 95 | + org.Employees.Add(new SalariedEmployee { EmployeeId = 2, Name = "Jane", AnnualSalary = 50000 }); |
| 96 | + org.Employees.Add(new Employee { EmployeeId = 1, Name = "John" }); |
| 97 | + |
| 98 | + var doc = new JsonPatchDocument<Organization>(); |
| 99 | + doc.Operations.Add(new Operations.Operation<Organization>("add", "/Employees/0/AnnualSalary", "", 100)); |
| 100 | + |
| 101 | + // Act |
| 102 | + doc.ApplyTo(org); |
| 103 | + |
| 104 | + // Assert |
| 105 | + Assert.Equal(100, (org.Employees[0] as SalariedEmployee).AnnualSalary); |
| 106 | + } |
| 107 | + |
72 | 108 | [Fact] |
73 | 109 | public void GenericPatchDocToNonGenericMustSerialize() |
74 | 110 | { |
|
0 commit comments