Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Fix for Issue #1096 (Model binding errors for invalid dates) #2527

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public object Convert(string input, Type destinationType, BindingContext context
{
return true;
}
return null;
throw;
}
}
}
Expand Down
23 changes: 4 additions & 19 deletions test/Nancy.Tests/Unit/ModelBinding/DefaultBinderFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,25 +294,6 @@ public void Should_call_convert_on_type_converter_if_available()
.MustHaveHappened(Repeated.Exactly.Once);
}

[Fact]
public void Should_ignore_properties_that_cannot_be_converted()
{
// Given
var binder = this.GetBinder(typeConverters: new[] { new FallbackConverter() });
var context = new NancyContext { Request = new FakeRequest("GET", "/") };
context.Request.Form["StringProperty"] = "Test";
context.Request.Form["IntProperty"] = "12";
context.Request.Form["DateProperty"] = "Broken";

// When
var result = (TestModel)binder.Bind(context, typeof(TestModel), null, BindingConfig.Default);

// Then
result.StringProperty.ShouldEqual("Test");
result.IntProperty.ShouldEqual(12);
result.DateProperty.ShouldEqual(default(DateTime));
}

[Fact]
public void Should_throw_ModelBindingException_if_convertion_of_a_property_fails()
{
Expand All @@ -321,6 +302,7 @@ public void Should_throw_ModelBindingException_if_convertion_of_a_property_fails
var context = new NancyContext { Request = new FakeRequest("GET", "/") };
context.Request.Form["IntProperty"] = "badint";
context.Request.Form["AnotherIntProperty"] = "morebad";
context.Request.Form["DateProperty"] = "bad dates"; // Indiana Jones' monkey is dead :)

// Then
Type modelType = typeof(TestModel);
Expand All @@ -333,6 +315,9 @@ public void Should_throw_ModelBindingException_if_convertion_of_a_property_fails
&& exception.PropertyBindingExceptions.Any(pe =>
pe.PropertyName == "AnotherIntProperty"
&& pe.AttemptedValue == "morebad")
&& exception.PropertyBindingExceptions.Any(pe =>
pe.PropertyName == "DateProperty"
&& pe.AttemptedValue == "bad dates")
&& exception.PropertyBindingExceptions.All(pe =>
pe.InnerException.Message.Contains(pe.AttemptedValue)
&& pe.InnerException.Message.Contains(modelType.GetProperty(pe.PropertyName).PropertyType.Name)));
Expand Down