@@ -195,6 +195,62 @@ public async Task BindModel_PassesAllowEmptyInputOptionViaContext(bool treatEmpt
195
195
Times . Once ) ;
196
196
}
197
197
198
+ [ Fact ]
199
+ public async Task BindModel_SetsModelIfAllowEmpty ( )
200
+ {
201
+ // Arrange
202
+ var mockInputFormatter = new Mock < IInputFormatter > ( ) ;
203
+ mockInputFormatter . Setup ( f => f . CanRead ( It . IsAny < InputFormatterContext > ( ) ) )
204
+ . Returns ( false ) ;
205
+ var inputFormatter = mockInputFormatter . Object ;
206
+
207
+ var provider = new TestModelMetadataProvider ( ) ;
208
+ provider . ForType < Person > ( ) . BindingDetails ( d => d . BindingSource = BindingSource . Body ) ;
209
+
210
+ var bindingContext = GetBindingContext (
211
+ typeof ( Person ) ,
212
+ metadataProvider : provider ) ;
213
+ bindingContext . BinderModelName = "custom" ;
214
+
215
+ var binder = CreateBinder ( new [ ] { inputFormatter } , treatEmptyInputAsDefaultValueOption : true ) ;
216
+
217
+ // Act
218
+ await binder . BindModelAsync ( bindingContext ) ;
219
+
220
+ // Assert
221
+ Assert . True ( bindingContext . Result . IsModelSet ) ;
222
+ Assert . Null ( bindingContext . Result . Model ) ;
223
+ Assert . True ( bindingContext . ModelState . IsValid ) ;
224
+ }
225
+
226
+ [ Fact ]
227
+ public async Task BindModel_FailsIfNotAllowEmpty ( )
228
+ {
229
+ // Arrange
230
+ var mockInputFormatter = new Mock < IInputFormatter > ( ) ;
231
+ mockInputFormatter . Setup ( f => f . CanRead ( It . IsAny < InputFormatterContext > ( ) ) )
232
+ . Returns ( false ) ;
233
+ var inputFormatter = mockInputFormatter . Object ;
234
+
235
+ var provider = new TestModelMetadataProvider ( ) ;
236
+ provider . ForType < Person > ( ) . BindingDetails ( d => d . BindingSource = BindingSource . Body ) ;
237
+
238
+ var bindingContext = GetBindingContext (
239
+ typeof ( Person ) ,
240
+ metadataProvider : provider ) ;
241
+ bindingContext . BinderModelName = "custom" ;
242
+
243
+ var binder = CreateBinder ( new [ ] { inputFormatter } , treatEmptyInputAsDefaultValueOption : false ) ;
244
+
245
+ // Act
246
+ await binder . BindModelAsync ( bindingContext ) ;
247
+
248
+ // Assert
249
+ Assert . False ( bindingContext . ModelState . IsValid ) ;
250
+ Assert . Single ( bindingContext . ModelState [ bindingContext . BinderModelName ] . Errors ) ;
251
+ Assert . Equal ( "Unsupported content type ''." , bindingContext . ModelState [ bindingContext . BinderModelName ] . Errors [ 0 ] . Exception . Message ) ;
252
+ }
253
+
198
254
// Throwing InputFormatterException
199
255
[ Fact ]
200
256
public async Task BindModel_CustomFormatter_ThrowingInputFormatterException_AddsErrorToModelState ( )
0 commit comments