Skip to content

Commit 428141d

Browse files
committed
Added a polimorphic list validation scenario
1 parent 7da8d9f commit 428141d

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

src/Features/JsonPatch.SystemTextJson/JsonPatch.SystemTextJson.sln

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/Features/JsonPatch.SystemTextJson/test/JsonPatchDocumentTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
45
using System.Text.Json;
56
using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Converters;
67
using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Exceptions;
@@ -69,6 +70,41 @@ public void NonGenericPatchDocToGenericMustSerialize()
6970
Assert.Equal("A", targetObject.AnotherStringProperty);
7071
}
7172

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+
72108
[Fact]
73109
public void GenericPatchDocToNonGenericMustSerialize()
74110
{

0 commit comments

Comments
 (0)