1- using System ;
1+ using Microsoft . AspNetCore . Http ;
2+ using System ;
23using System . Collections ;
34using System . Collections . Generic ;
45using System . Reflection ;
@@ -14,6 +15,37 @@ public DefaultModelContentResolver(ProxyMetadataProvider metadataProvider)
1415 MetadataProvider = metadataProvider ;
1516 }
1617
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+
1749 private void SetSimpleEnumerable ( string key , Dictionary < string , string > dictionary , object value )
1850 {
1951 var enumerable = value as IEnumerable ;
@@ -31,8 +63,8 @@ private void SetSimpleEnumerable(string key, Dictionary<string, string> dictiona
3163 private void TrySetSystemObjectValue ( string key ,
3264 PropertyInfo propertyInfo ,
3365 string propertyName ,
34- Type containerType ,
35- Dictionary < string , string > dictionary ,
66+ Type containerType ,
67+ ModelDictionaryResult result ,
3668 object value )
3769 {
3870 var objModelMetadata = new ProxyModelMetadata ( propertyInfo ,
@@ -43,29 +75,37 @@ private void TrySetSystemObjectValue(string key,
4375
4476 if ( objModelMetadata . IsSimpleType )
4577 {
46- dictionary . Add ( key , Convert . ToString ( value ) ) ;
78+ result . Dictionary . Add ( key , Convert . ToString ( value ) ) ;
4779 return ;
4880 }
4981
5082 if ( objModelMetadata . IsEnumerableType )
5183 {
5284 if ( objModelMetadata . ElementType . IsSimpleType )
5385 {
54- SetSimpleEnumerable ( key , dictionary , value ) ;
86+ SetSimpleEnumerable ( key , result . Dictionary , value ) ;
5587 }
5688 else
5789 {
58- TrySetValueInner ( key , objModelMetadata , dictionary , value ) ;
90+ TrySetValueInner ( key , objModelMetadata , result , value ) ;
5991 }
6092
6193 return ;
6294 }
6395
6496 // 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+ }
66106 }
67107
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 )
69109 {
70110 var count = modelMetadata . ElementType . PropertiesCount ;
71111 var elementProperties = modelMetadata . ElementType . Properties ;
@@ -80,72 +120,75 @@ private void TrySetValueInner(string key, ProxyModelMetadata modelMetadata, Dict
80120 {
81121 var propKey = $ "{ key } [{ index } ]";
82122 var propValue = propertyInfo ? . GetValue ( v ) ;
83- ResolveInternal ( elementModelMetadata , dictionary , propValue , propKey ) ;
123+ ResolveInternal ( elementModelMetadata , result , propValue , isTopLevelObject : false , prefix : propKey ) ;
84124 index ++ ;
85125 }
86126 }
87127 }
88128 }
89129
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 )
91131 {
92- var key = modelMetadata . PropertyName ;
93- if ( ! string . IsNullOrEmpty ( prefix ) )
94- key = $ "{ prefix } .{ key } ";
132+ var key = prefix ;
95133
96134 if ( modelMetadata . IsNullableValueType )
97135 {
98136 // recall with prefix
99- TrySetValue ( prefix , modelMetadata . ElementType , dictionary , value ) ;
137+ TrySetValue ( prefix , modelMetadata . ElementType , result , value ) ;
100138 return ;
101139 }
102140
103141 if ( modelMetadata . IsSimpleType )
104142 {
105- dictionary . Add ( key , Convert . ToString ( value ) ) ;
143+ result . Dictionary . Add ( key , Convert . ToString ( value ) ) ;
106144 return ;
107145 }
108146
109- if ( modelMetadata . IsFormFile )
110- {
111- // TODO Gencebay
112- }
113-
114147 if ( modelMetadata . IsEnumerableType )
115148 {
116149 if ( modelMetadata . ElementType . IsSimpleType )
117150 {
118- SetSimpleEnumerable ( key , dictionary , value ) ;
151+ SetSimpleEnumerable ( key , result . Dictionary , value ) ;
119152 }
120153 else if ( modelMetadata . ElementType . IsSystemObject )
121154 {
122155 TrySetSystemObjectValue ( key ,
123156 modelMetadata . ElementType . PropertyInfo ,
124157 modelMetadata . ElementType . PropertyName ,
125158 modelMetadata . ContainerType ,
126- dictionary , value ) ;
159+ result , value ) ;
127160 }
128161 else
129162 {
130- TrySetValueInner ( key , modelMetadata , dictionary , value ) ;
163+ TrySetValueInner ( key , modelMetadata , result , value ) ;
131164 }
132165
133166 return ;
134167 }
135-
136- // If typeof(object)
168+
137169 if ( modelMetadata . IsSystemObject )
138170 {
139- TrySetSystemObjectValue ( key , modelMetadata . PropertyInfo , modelMetadata . PropertyName , modelMetadata . ContainerType , dictionary , value ) ;
171+ TrySetSystemObjectValue ( key , modelMetadata . PropertyInfo , modelMetadata . PropertyName , modelMetadata . ContainerType , result , value ) ;
140172 }
141173 }
142174
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 = "" )
144176 {
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 ;
145188 var count = modelMetadata . PropertiesCount ;
146189 if ( count == 0 )
147190 {
148- TrySetValue ( prefix , modelMetadata , dictionary , value ) ;
191+ TrySetValue ( key , modelMetadata , result , value ) ;
149192 return ;
150193 }
151194
@@ -154,44 +197,41 @@ private void ResolveInternal(ProxyModelMetadata modelMetadata, Dictionary<string
154197 var metadata = modelMetadata . Properties [ i ] ;
155198 if ( metadata . ContainerType != null )
156199 {
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-
166200 if ( value != null )
167201 {
168202 var v = metadata . PropertyInfo . GetValue ( value ) ;
169203 if ( v != null )
170204 {
171- ResolveInternal ( metadata , dictionary , v , parent ) ;
205+ ResolveInternal ( metadata , result , v , isTopLevelObject : false , prefix : key ) ;
172206 }
173207 }
174208 }
175209 else
176210 {
177- ResolveInternal ( metadata , dictionary , value ) ;
211+ ResolveInternal ( metadata , result , value ) ;
178212 }
179213 }
180214 }
181215
182216 // Per request parameter context resolver
183- public ResolvedContentResult Resolve ( List < ProxyModelMetadata > parameters , object [ ] args )
217+ public ModelDictionaryResult Resolve ( List < ProxyModelMetadata > parameters , object [ ] args )
184218 {
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 ) ) ;
187221
188- for ( int i = 0 ; i < count ; i ++ )
222+ for ( int i = 0 ; i < parameters . Count ; i ++ )
189223 {
190224 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 ) ;
192232 }
193233
194- return new ResolvedContentResult ( dictionary ) ;
234+ return result ;
195235 }
196236 }
197- }
237+ }
0 commit comments