From eefa58e6176e03910be9211ee7fdc787e4cc6288 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Sun, 27 Sep 2015 15:55:18 -0700 Subject: [PATCH] [Fixes #3043] Overflow exceptions converted to invalid value messages --- .../ModelBinding/ModelStateDictionary.cs | 4 +- .../ValidationIntegrationTests.cs | 88 +++++++++++++++++++ .../HtmlHelperValidationSummaryTest.cs | 2 + 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelStateDictionary.cs b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelStateDictionary.cs index d2c6f3891c..d4aeec8ed3 100644 --- a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelStateDictionary.cs +++ b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelStateDictionary.cs @@ -209,9 +209,9 @@ namespace Microsoft.AspNet.Mvc.ModelBinding return false; } - if (exception is FormatException) + if (exception is FormatException || exception is OverflowException) { - // Convert FormatExceptions to Invalid value messages. + // Convert FormatExceptions and OverflowExceptions to Invalid value messages. ModelState modelState; TryGetValue(key, out modelState); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/ValidationIntegrationTests.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/ValidationIntegrationTests.cs index c503eb33e5..61e14ac0fc 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/ValidationIntegrationTests.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/ValidationIntegrationTests.cs @@ -992,6 +992,94 @@ namespace Microsoft.AspNet.Mvc.IntegrationTests Assert.True(modelState.IsValid); } + private class User + { + public int Id { get; set; } + + public uint Zip { get; set; } + + } + + [Fact] + public async Task Validation_FormatException_ShowsInvalidValueMessage_OnSimpleTypeProperty() + { + // Arrange + var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); + var parameter = new ParameterDescriptor() + { + Name = "parameter", + ParameterType = typeof(User) + }; + + var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => + { + request.QueryString = new QueryString("?Id=bill"); + }); + + var modelState = new ModelStateDictionary(); + + // Act + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + + // Assert + Assert.True(modelBindingResult.IsModelSet); + + var model = Assert.IsType(modelBindingResult.Model); + Assert.Equal(0, model.Id); + Assert.Equal(1, modelState.ErrorCount); + Assert.False(modelState.IsValid); + + var state = Assert.Single(modelState); + Assert.Equal("Id", state.Key); + var entry = state.Value; + Assert.Equal("bill", entry.AttemptedValue); + Assert.Equal("bill", entry.RawValue); + Assert.Single(entry.Errors); + + var error = entry.Errors[0]; + Assert.Equal("The value 'bill' is not valid for Id.", error.ErrorMessage); + } + + [Fact] + public async Task Validation_OverflowException_ShowsInvalidValueMessage_OnSimpleTypeProperty() + { + // Arrange + var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); + var parameter = new ParameterDescriptor() + { + Name = "parameter", + ParameterType = typeof(User) + }; + + var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => + { + request.QueryString = new QueryString("?Zip=-123"); + }); + + var modelState = new ModelStateDictionary(); + + // Act + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + + // Assert + Assert.True(modelBindingResult.IsModelSet); + + var model = Assert.IsType(modelBindingResult.Model); + Assert.Equal(0, model.Zip); + Assert.Equal(1, modelState.ErrorCount); + Assert.False(modelState.IsValid); + + var state = Assert.Single(modelState); + Assert.Equal("Zip", state.Key); + var entry = state.Value; + Assert.Equal("-123", entry.AttemptedValue); + Assert.Equal("-123", entry.RawValue); + Assert.Single(entry.Errors); + + var error = entry.Errors[0]; + Assert.Equal("The value '-123' is not valid for Zip.", error.ErrorMessage); + } + private class Order11 { public IEnumerable
ShippingAddresses { get; set; } diff --git a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/Rendering/HtmlHelperValidationSummaryTest.cs b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/Rendering/HtmlHelperValidationSummaryTest.cs index bb67cf4404..386af6f225 100644 --- a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/Rendering/HtmlHelperValidationSummaryTest.cs +++ b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/Rendering/HtmlHelperValidationSummaryTest.cs @@ -115,6 +115,7 @@ namespace Microsoft.AspNet.Mvc.Rendering "
  • HtmlEncode[[This is an error for Property3.]]
  • " + Environment.NewLine + "
  • HtmlEncode[[This is an error for Property2.]]
  • " + Environment.NewLine + "
  • HtmlEncode[[This is another error for Property2.]]
  • " + Environment.NewLine + + "
  • HtmlEncode[[The value '' is not valid for Property2.]]
  • " + Environment.NewLine + "
  • HtmlEncode[[This is an error for the model root.]]
  • " + Environment.NewLine + "
  • HtmlEncode[[This is another error for the model root.]]
  • " + Environment.NewLine + ""; @@ -346,6 +347,7 @@ namespace Microsoft.AspNet.Mvc.Rendering modelState.AddModelError("Property2", "This is an error for Property2."); modelState.AddModelError("Property2", "This is another error for Property2."); + modelState.AddModelError("Property2", new OverflowException("Produces invalid value message")); modelState.AddModelError(string.Empty, "This is an error for the model root."); modelState.AddModelError(string.Empty, "This is another error for the model root.");