1
- using System ;
1
+ using Microsoft . AspNetCore . Http ;
2
+ using System ;
2
3
using System . Collections ;
3
4
using System . Collections . Generic ;
4
5
using System . Reflection ;
@@ -14,6 +15,37 @@ public DefaultModelContentResolver(ProxyMetadataProvider metadataProvider)
14
15
MetadataProvider = metadataProvider ;
15
16
}
16
17
18
+ private void TrySetFormFile ( string prefix , ProxyModelMetadata modelMetadata , ModelDictionaryResult result , object value )
19
+ {
20
+ if ( modelMetadata . IsNullableValueType )
21
+ {
22
+ // recall with prefix
23
+ TrySetFormFile ( prefix , modelMetadata . ElementType , result , value ) ;
24
+ return ;
25
+ }
26
+
27
+ if ( modelMetadata . IsEnumerableType )
28
+ {
29
+ var enumerable = value as IEnumerable < IFormFile > ;
30
+ int index = 0 ;
31
+ foreach ( var item in enumerable )
32
+ {
33
+ if ( item != null )
34
+ {
35
+ result . Files . Add ( $ "{ prefix } [{ index } ]", item ) ;
36
+ }
37
+ index ++ ;
38
+ }
39
+
40
+ return ;
41
+ }
42
+
43
+ if ( modelMetadata . IsFormFile )
44
+ {
45
+ result . Files . Add ( prefix , ( IFormFile ) value ) ;
46
+ }
47
+ }
48
+
17
49
private void SetSimpleEnumerable ( string key , Dictionary < string , string > dictionary , object value )
18
50
{
19
51
var enumerable = value as IEnumerable ;
@@ -31,8 +63,8 @@ private void SetSimpleEnumerable(string key, Dictionary<string, string> dictiona
31
63
private void TrySetSystemObjectValue ( string key ,
32
64
PropertyInfo propertyInfo ,
33
65
string propertyName ,
34
- Type containerType ,
35
- Dictionary < string , string > dictionary ,
66
+ Type containerType ,
67
+ ModelDictionaryResult result ,
36
68
object value )
37
69
{
38
70
var objModelMetadata = new ProxyModelMetadata ( propertyInfo ,
@@ -43,29 +75,37 @@ private void TrySetSystemObjectValue(string key,
43
75
44
76
if ( objModelMetadata . IsSimpleType )
45
77
{
46
- dictionary . Add ( key , Convert . ToString ( value ) ) ;
78
+ result . Dictionary . Add ( key , Convert . ToString ( value ) ) ;
47
79
return ;
48
80
}
49
81
50
82
if ( objModelMetadata . IsEnumerableType )
51
83
{
52
84
if ( objModelMetadata . ElementType . IsSimpleType )
53
85
{
54
- SetSimpleEnumerable ( key , dictionary , value ) ;
86
+ SetSimpleEnumerable ( key , result . Dictionary , value ) ;
55
87
}
56
88
else
57
89
{
58
- TrySetValueInner ( key , objModelMetadata , dictionary , value ) ;
90
+ TrySetValueInner ( key , objModelMetadata , result , value ) ;
59
91
}
60
92
61
93
return ;
62
94
}
63
95
64
96
// Anonymous object resolver
65
- ResolveInternal ( objModelMetadata , dictionary , value , key ) ;
97
+ for ( int i = 0 ; i < objModelMetadata . Properties . Count ; i ++ )
98
+ {
99
+ var modelMetadata = objModelMetadata . Properties [ i ] ;
100
+ var v = modelMetadata . PropertyInfo . GetValue ( value ) ;
101
+ if ( v != null )
102
+ {
103
+ ResolveInternal ( modelMetadata , result , v , isTopLevelObject : false , prefix : key ) ;
104
+ }
105
+ }
66
106
}
67
107
68
- private void TrySetValueInner ( string key , ProxyModelMetadata modelMetadata , Dictionary < string , string > dictionary , object value )
108
+ private void TrySetValueInner ( string key , ProxyModelMetadata modelMetadata , ModelDictionaryResult result , object value )
69
109
{
70
110
var count = modelMetadata . ElementType . PropertiesCount ;
71
111
var elementProperties = modelMetadata . ElementType . Properties ;
@@ -80,72 +120,75 @@ private void TrySetValueInner(string key, ProxyModelMetadata modelMetadata, Dict
80
120
{
81
121
var propKey = $ "{ key } [{ index } ]";
82
122
var propValue = propertyInfo ? . GetValue ( v ) ;
83
- ResolveInternal ( elementModelMetadata , dictionary , propValue , propKey ) ;
123
+ ResolveInternal ( elementModelMetadata , result , propValue , isTopLevelObject : false , prefix : propKey ) ;
84
124
index ++ ;
85
125
}
86
126
}
87
127
}
88
128
}
89
129
90
- private void TrySetValue ( string prefix , ProxyModelMetadata modelMetadata , Dictionary < string , string > dictionary , object value )
130
+ private void TrySetValue ( string prefix , ProxyModelMetadata modelMetadata , ModelDictionaryResult result , object value )
91
131
{
92
- var key = modelMetadata . PropertyName ;
93
- if ( ! string . IsNullOrEmpty ( prefix ) )
94
- key = $ "{ prefix } .{ key } ";
132
+ var key = prefix ;
95
133
96
134
if ( modelMetadata . IsNullableValueType )
97
135
{
98
136
// recall with prefix
99
- TrySetValue ( prefix , modelMetadata . ElementType , dictionary , value ) ;
137
+ TrySetValue ( prefix , modelMetadata . ElementType , result , value ) ;
100
138
return ;
101
139
}
102
140
103
141
if ( modelMetadata . IsSimpleType )
104
142
{
105
- dictionary . Add ( key , Convert . ToString ( value ) ) ;
143
+ result . Dictionary . Add ( key , Convert . ToString ( value ) ) ;
106
144
return ;
107
145
}
108
146
109
- if ( modelMetadata . IsFormFile )
110
- {
111
- // TODO Gencebay
112
- }
113
-
114
147
if ( modelMetadata . IsEnumerableType )
115
148
{
116
149
if ( modelMetadata . ElementType . IsSimpleType )
117
150
{
118
- SetSimpleEnumerable ( key , dictionary , value ) ;
151
+ SetSimpleEnumerable ( key , result . Dictionary , value ) ;
119
152
}
120
153
else if ( modelMetadata . ElementType . IsSystemObject )
121
154
{
122
155
TrySetSystemObjectValue ( key ,
123
156
modelMetadata . ElementType . PropertyInfo ,
124
157
modelMetadata . ElementType . PropertyName ,
125
158
modelMetadata . ContainerType ,
126
- dictionary , value ) ;
159
+ result , value ) ;
127
160
}
128
161
else
129
162
{
130
- TrySetValueInner ( key , modelMetadata , dictionary , value ) ;
163
+ TrySetValueInner ( key , modelMetadata , result , value ) ;
131
164
}
132
165
133
166
return ;
134
167
}
135
-
136
- // If typeof(object)
168
+
137
169
if ( modelMetadata . IsSystemObject )
138
170
{
139
- TrySetSystemObjectValue ( key , modelMetadata . PropertyInfo , modelMetadata . PropertyName , modelMetadata . ContainerType , dictionary , value ) ;
171
+ TrySetSystemObjectValue ( key , modelMetadata . PropertyInfo , modelMetadata . PropertyName , modelMetadata . ContainerType , result , value ) ;
140
172
}
141
173
}
142
174
143
- private void ResolveInternal ( ProxyModelMetadata modelMetadata , Dictionary < string , string > dictionary , object value , string prefix = "" )
175
+ private void ResolveInternal ( ProxyModelMetadata modelMetadata , ModelDictionaryResult result , object value , bool isTopLevelObject = false , string prefix = "" )
144
176
{
177
+ var key = isTopLevelObject ? string . Empty : modelMetadata . PropertyName ;
178
+ if ( ! string . IsNullOrEmpty ( prefix ) )
179
+ key = $ "{ prefix } .{ key } ";
180
+
181
+ if ( modelMetadata . IsFormFile || ( modelMetadata . IsEnumerableType && modelMetadata . ElementType . IsFormFile ) )
182
+ {
183
+ TrySetFormFile ( key , modelMetadata , result , value ) ;
184
+ return ;
185
+ }
186
+
187
+ var dictionary = result . Dictionary ;
145
188
var count = modelMetadata . PropertiesCount ;
146
189
if ( count == 0 )
147
190
{
148
- TrySetValue ( prefix , modelMetadata , dictionary , value ) ;
191
+ TrySetValue ( key , modelMetadata , result , value ) ;
149
192
return ;
150
193
}
151
194
@@ -154,44 +197,41 @@ private void ResolveInternal(ProxyModelMetadata modelMetadata, Dictionary<string
154
197
var metadata = modelMetadata . Properties [ i ] ;
155
198
if ( metadata . ContainerType != null )
156
199
{
157
- var parent = prefix ;
158
- if ( ! metadata . IsSimpleType &&
159
- ! metadata . IsEnumerableType &&
160
- ! metadata . IsNullableValueType &&
161
- ! metadata . IsSystemObject )
162
- {
163
- parent = ! string . IsNullOrEmpty ( prefix ) ? $ "{ prefix } .{ metadata . PropertyName } " : metadata . PropertyName ;
164
- }
165
-
166
200
if ( value != null )
167
201
{
168
202
var v = metadata . PropertyInfo . GetValue ( value ) ;
169
203
if ( v != null )
170
204
{
171
- ResolveInternal ( metadata , dictionary , v , parent ) ;
205
+ ResolveInternal ( metadata , result , v , isTopLevelObject : false , prefix : key ) ;
172
206
}
173
207
}
174
208
}
175
209
else
176
210
{
177
- ResolveInternal ( metadata , dictionary , value ) ;
211
+ ResolveInternal ( metadata , result , value ) ;
178
212
}
179
213
}
180
214
}
181
215
182
216
// Per request parameter context resolver
183
- public ResolvedContentResult Resolve ( List < ProxyModelMetadata > parameters , object [ ] args )
217
+ public ModelDictionaryResult Resolve ( List < ProxyModelMetadata > parameters , object [ ] args )
184
218
{
185
- var dictionary = new Dictionary < string , string > ( StringComparer . Ordinal ) ;
186
- var count = parameters . Count ;
219
+ var result = new ModelDictionaryResult ( new Dictionary < string , string > ( StringComparer . Ordinal ) ,
220
+ new Dictionary < string , IFormFile > ( StringComparer . Ordinal ) ) ;
187
221
188
- for ( int i = 0 ; i < count ; i ++ )
222
+ for ( int i = 0 ; i < parameters . Count ; i ++ )
189
223
{
190
224
var modelMetadata = parameters [ i ] ;
191
- ResolveInternal ( modelMetadata , dictionary , args [ i ] ) ;
225
+ string prefix = string . Empty ;
226
+ if ( modelMetadata . ContainerType == null && ! string . IsNullOrEmpty ( modelMetadata . PropertyName ) )
227
+ {
228
+ prefix = modelMetadata . PropertyName ;
229
+ }
230
+
231
+ ResolveInternal ( modelMetadata , result , args [ i ] , isTopLevelObject : true , prefix : prefix ) ;
192
232
}
193
233
194
- return new ResolvedContentResult ( dictionary ) ;
234
+ return result ;
195
235
}
196
236
}
197
- }
237
+ }
0 commit comments