@@ -694,23 +694,6 @@ public void GetFieldValidity_ReturnsUnvalidated_IfAnyItemInSubtreeIsInvalid()
694
694
Assert . Equal ( ModelValidationState . Unvalidated , validationState ) ;
695
695
}
696
696
697
- [ Fact ]
698
- public void GetFieldValidity_ReturnsUnvalidated_IfTreeHeightIsGreaterThanLimit ( )
699
- {
700
- // Arrange
701
- var dictionary = new ModelStateDictionary ( ) ;
702
- var stackLimit = 32 ;
703
- var key = string . Join ( "." , Enumerable . Repeat ( "foo" , stackLimit + 1 ) ) ;
704
- dictionary . MaxValidationDepth = stackLimit ;
705
- dictionary . MarkFieldValid ( key ) ;
706
-
707
- // Act
708
- var validationState = dictionary . GetFieldValidationState ( "foo" ) ;
709
-
710
- // Assert
711
- Assert . Equal ( ModelValidationState . Unvalidated , validationState ) ;
712
- }
713
-
714
697
[ Theory ]
715
698
[ InlineData ( "" ) ]
716
699
[ InlineData ( "user" ) ]
@@ -1619,6 +1602,162 @@ public void GetModelStateForProperty_ReturnsModelStateForIndexedChildren()
1619
1602
Assert . Equal ( "value1" , property . RawValue ) ;
1620
1603
}
1621
1604
1605
+ [ Fact ]
1606
+ public void GetFieldValidationState_ReturnsUnvalidated_IfTreeHeightIsGreaterThanLimit ( )
1607
+ {
1608
+ // Arrange
1609
+ var stackLimit = 5 ;
1610
+ var dictionary = new ModelStateDictionary ( ) ;
1611
+ var key = string . Join ( "." , Enumerable . Repeat ( "foo" , stackLimit + 1 ) ) ;
1612
+ dictionary . MaxValidationDepth = stackLimit ;
1613
+ dictionary . MaxStateDepth = null ;
1614
+ dictionary . MarkFieldValid ( key ) ;
1615
+
1616
+ // Act
1617
+ var validationState = dictionary . GetFieldValidationState ( "foo" ) ;
1618
+
1619
+ // Assert
1620
+ Assert . Equal ( ModelValidationState . Unvalidated , validationState ) ;
1621
+ }
1622
+
1623
+ [ Fact ]
1624
+ public void IsValidProperty_ReturnsTrue_IfTreeHeightIsGreaterThanLimit ( )
1625
+ {
1626
+ // Arrange
1627
+ var stackLimit = 5 ;
1628
+ var dictionary = new ModelStateDictionary ( ) ;
1629
+ var key = string . Join ( "." , Enumerable . Repeat ( "foo" , stackLimit + 1 ) ) ;
1630
+ dictionary . MaxValidationDepth = stackLimit ;
1631
+ dictionary . MaxStateDepth = null ;
1632
+ dictionary . AddModelError ( key , "some error" ) ;
1633
+
1634
+ // Act
1635
+ var isValid = dictionary . IsValid ;
1636
+ var validationState = dictionary . ValidationState ;
1637
+
1638
+ // Assert
1639
+ Assert . True ( isValid ) ;
1640
+ Assert . Equal ( ModelValidationState . Valid , validationState ) ;
1641
+ }
1642
+
1643
+ [ Fact ]
1644
+ public void TryAddModelException_Throws_IfKeyHasTooManySegments ( )
1645
+ {
1646
+ // Arrange
1647
+ var exception = new TestException ( ) ;
1648
+
1649
+ var stateDepth = 5 ;
1650
+ var dictionary = new ModelStateDictionary ( ) ;
1651
+ var key = string . Join ( "." , Enumerable . Repeat ( "foo" , stateDepth + 1 ) ) ;
1652
+ dictionary . MaxStateDepth = stateDepth ;
1653
+
1654
+ // Act
1655
+ var invalidException = Assert . Throws < InvalidOperationException > ( ( ) => dictionary . TryAddModelException ( key , exception ) ) ;
1656
+
1657
+ // Assert
1658
+ Assert . Equal (
1659
+ $ "The specified key exceeded the maximum ModelState depth: { dictionary . MaxStateDepth } ",
1660
+ invalidException . Message ) ;
1661
+ }
1662
+
1663
+ [ Fact ]
1664
+ public void TryAddModelError_Throws_IfKeyHasTooManySegments ( )
1665
+ {
1666
+ // Arrange
1667
+ var stateDepth = 5 ;
1668
+ var dictionary = new ModelStateDictionary ( ) ;
1669
+ var key = string . Join ( "." , Enumerable . Repeat ( "foo" , stateDepth + 1 ) ) ;
1670
+ dictionary . MaxStateDepth = stateDepth ;
1671
+
1672
+ // Act
1673
+ var invalidException = Assert . Throws < InvalidOperationException > ( ( ) => dictionary . TryAddModelError ( key , "errorMessage" ) ) ;
1674
+
1675
+ // Assert
1676
+ Assert . Equal (
1677
+ $ "The specified key exceeded the maximum ModelState depth: { dictionary . MaxStateDepth } ",
1678
+ invalidException . Message ) ;
1679
+ }
1680
+
1681
+ [ Fact ]
1682
+ public void SetModelValue_Throws_IfKeyHasTooManySegments ( )
1683
+ {
1684
+ var stateDepth = 5 ;
1685
+ var dictionary = new ModelStateDictionary ( ) ;
1686
+ var key = string . Join ( "." , Enumerable . Repeat ( "foo" , stateDepth + 1 ) ) ;
1687
+ dictionary . MaxStateDepth = stateDepth ;
1688
+
1689
+ // Act
1690
+ var invalidException = Assert . Throws < InvalidOperationException > ( ( ) => dictionary . SetModelValue ( key , string . Empty , string . Empty ) ) ;
1691
+
1692
+ // Assert
1693
+ Assert . Equal (
1694
+ $ "The specified key exceeded the maximum ModelState depth: { dictionary . MaxStateDepth } ",
1695
+ invalidException . Message ) ;
1696
+ }
1697
+
1698
+ [ Fact ]
1699
+ public void MarkFieldValid_Throws_IfKeyHasTooManySegments ( )
1700
+ {
1701
+ // Arrange
1702
+ var stateDepth = 5 ;
1703
+ var source = new ModelStateDictionary ( ) ;
1704
+ var key = string . Join ( "." , Enumerable . Repeat ( "foo" , stateDepth + 1 ) ) ;
1705
+ source . MaxStateDepth = stateDepth ;
1706
+
1707
+ // Act
1708
+ var exception = Assert . Throws < InvalidOperationException > ( ( ) => source . MarkFieldValid ( key ) ) ;
1709
+
1710
+ // Assert
1711
+ Assert . Equal (
1712
+ $ "The specified key exceeded the maximum ModelState depth: { source . MaxStateDepth } ",
1713
+ exception . Message ) ;
1714
+ }
1715
+
1716
+ [ Fact ]
1717
+ public void MarkFieldSkipped_Throws_IfKeyHasTooManySegments ( )
1718
+ {
1719
+ // Arrange
1720
+ var stateDepth = 5 ;
1721
+ var source = new ModelStateDictionary ( ) ;
1722
+ var key = string . Join ( "." , Enumerable . Repeat ( "foo" , stateDepth + 1 ) ) ;
1723
+ source . MaxStateDepth = stateDepth ;
1724
+
1725
+ // Act
1726
+ var exception = Assert . Throws < InvalidOperationException > ( ( ) => source . MarkFieldSkipped ( key ) ) ;
1727
+
1728
+ // Assert
1729
+ Assert . Equal (
1730
+ $ "The specified key exceeded the maximum ModelState depth: { source . MaxStateDepth } ",
1731
+ exception . Message ) ;
1732
+ }
1733
+
1734
+ [ Fact ]
1735
+ public void Constructor_SetsDefaultRecursionDepth ( )
1736
+ {
1737
+ // Arrange && Act
1738
+ var dictionary = new ModelStateDictionary ( ) ;
1739
+
1740
+ // Assert
1741
+ Assert . Equal ( ModelStateDictionary . DefaultMaxRecursionDepth , dictionary . MaxValidationDepth ) ;
1742
+ Assert . Equal ( ModelStateDictionary . DefaultMaxRecursionDepth , dictionary . MaxStateDepth ) ;
1743
+ }
1744
+
1745
+ [ Fact ]
1746
+ public void CopyConstructor_PreservesRecursionDepth ( )
1747
+ {
1748
+ // Arrange
1749
+ var dictionary = new ModelStateDictionary ( ) ;
1750
+ dictionary . MaxValidationDepth = 5 ;
1751
+ dictionary . MaxStateDepth = 4 ;
1752
+
1753
+ // Act
1754
+ var newDictionary = new ModelStateDictionary ( dictionary ) ;
1755
+
1756
+ // Assert
1757
+ Assert . Equal ( dictionary . MaxValidationDepth , newDictionary . MaxValidationDepth ) ;
1758
+ Assert . Equal ( dictionary . MaxStateDepth , newDictionary . MaxStateDepth ) ;
1759
+ }
1760
+
1622
1761
private class OptionsAccessor : IOptions < MvcOptions >
1623
1762
{
1624
1763
public MvcOptions Value { get ; } = new MvcOptions ( ) ;
0 commit comments