1
+ #pragma warning disable ASP0029 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
2
+
3
+ // Licensed to the .NET Foundation under one or more agreements.
4
+ // The .NET Foundation licenses this file to you under the MIT license.
5
+
6
+ using System . ComponentModel . DataAnnotations ;
7
+
8
+ namespace Microsoft . AspNetCore . Http . Validation . Tests ;
9
+
10
+ public class RuntimeValidatableTypeInfoResolverTests
11
+ {
12
+ private readonly RuntimeValidatableTypeInfoResolver _resolver = new ( ) ;
13
+
14
+ [ Fact ]
15
+ public void TryGetValidatableParameterInfo_AlwaysReturnsFalse ( )
16
+ {
17
+ var parameterInfo = typeof ( RuntimeValidatableTypeInfoResolverTests ) . GetMethod ( nameof ( TestMethod ) ) ! . GetParameters ( ) [ 0 ] ;
18
+
19
+ var result = _resolver . TryGetValidatableParameterInfo ( parameterInfo , out var validatableInfo ) ;
20
+
21
+ Assert . False ( result ) ;
22
+ Assert . Null ( validatableInfo ) ;
23
+ }
24
+
25
+ [ Theory ]
26
+ [ InlineData ( typeof ( string ) ) ]
27
+ [ InlineData ( typeof ( int ) ) ]
28
+ [ InlineData ( typeof ( bool ) ) ]
29
+ [ InlineData ( typeof ( DateTime ) ) ]
30
+ [ InlineData ( typeof ( Guid ) ) ]
31
+ [ InlineData ( typeof ( decimal ) ) ]
32
+ [ InlineData ( typeof ( DayOfWeek ) ) ] // Enum
33
+ public void TryGetValidatableTypeInfo_WithPrimitiveTypes_ReturnsFalse ( Type type )
34
+ {
35
+ var result = _resolver . TryGetValidatableTypeInfo ( type , out var validatableInfo ) ;
36
+
37
+ Assert . False ( result ) ;
38
+ Assert . Null ( validatableInfo ) ;
39
+ }
40
+
41
+ [ Fact ]
42
+ public void TryGetValidatableTypeInfo_WithComplexTypeWithValidationAttributes_ReturnsTrue ( )
43
+ {
44
+ var result = _resolver . TryGetValidatableTypeInfo ( typeof ( PersonWithValidation ) , out var validatableInfo ) ;
45
+
46
+ Assert . True ( result ) ;
47
+ Assert . NotNull ( validatableInfo ) ;
48
+ Assert . IsType < RuntimeValidatableTypeInfoResolver . RuntimeValidatableTypeInfo > ( validatableInfo ) ;
49
+ }
50
+
51
+ [ Fact ]
52
+ public void TryGetValidatableTypeInfo_WithComplexTypeWithoutValidationAttributes_ReturnsFalse ( )
53
+ {
54
+ var result = _resolver . TryGetValidatableTypeInfo ( typeof ( PersonWithoutValidation ) , out var validatableInfo ) ;
55
+
56
+ Assert . False ( result ) ;
57
+ Assert . Null ( validatableInfo ) ;
58
+ }
59
+
60
+ [ Fact ]
61
+ public void TryGetValidatableTypeInfo_WithNestedComplexType_ReturnsTrue ( )
62
+ {
63
+ var result = _resolver . TryGetValidatableTypeInfo ( typeof ( PersonWithNestedValidation ) , out var validatableInfo ) ;
64
+
65
+ Assert . True ( result ) ;
66
+ Assert . NotNull ( validatableInfo ) ;
67
+ }
68
+
69
+ [ Fact ]
70
+ public void TryGetValidatableTypeInfo_CachesResults ( )
71
+ {
72
+ // Call twice with the same type
73
+ var result1 = _resolver . TryGetValidatableTypeInfo ( typeof ( PersonWithValidation ) , out var validatableInfo1 ) ;
74
+ var result2 = _resolver . TryGetValidatableTypeInfo ( typeof ( PersonWithValidation ) , out var validatableInfo2 ) ;
75
+
76
+ Assert . True ( result1 ) ;
77
+ Assert . True ( result2 ) ;
78
+ Assert . Same ( validatableInfo1 , validatableInfo2 ) ; // Should be the same cached instance
79
+ }
80
+
81
+ [ Fact ]
82
+ public void TryGetValidatableTypeInfo_WithCyclicReference_HandlesGracefully ( )
83
+ {
84
+ var result = _resolver . TryGetValidatableTypeInfo ( typeof ( PersonWithCyclicReference ) , out var validatableInfo ) ;
85
+
86
+ Assert . True ( result ) ;
87
+ Assert . NotNull ( validatableInfo ) ;
88
+ // Should not throw StackOverflowException due to cycle detection
89
+ }
90
+
91
+ private void TestMethod ( string parameter ) { }
92
+
93
+ private class PersonWithValidation
94
+ {
95
+ [ Required ]
96
+ public string Name { get ; set ; } = "" ;
97
+
98
+ [ Range ( 0 , 120 ) ]
99
+ public int Age { get ; set ; }
100
+ }
101
+
102
+ private class PersonWithoutValidation
103
+ {
104
+ public string Name { get ; set ; } = "" ;
105
+ public int Age { get ; set ; }
106
+ }
107
+
108
+ private class PersonWithNestedValidation
109
+ {
110
+ [ Required ]
111
+ public string Name { get ; set ; } = "" ;
112
+
113
+ public AddressWithValidation Address { get ; set ; } = new ( ) ;
114
+ }
115
+
116
+ private class AddressWithValidation
117
+ {
118
+ [ Required ]
119
+ public string Street { get ; set ; } = "" ;
120
+
121
+ [ Required ]
122
+ public string City { get ; set ; } = "" ;
123
+ }
124
+
125
+ private class PersonWithCyclicReference
126
+ {
127
+ [ Required ]
128
+ public string Name { get ; set ; } = "" ;
129
+
130
+ public PersonWithCyclicReference ? Friend { get ; set ; }
131
+ }
132
+ }
0 commit comments