From bbba9dcde67a7606442b304a4a045732c2d187b1 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Mon, 14 Dec 2015 12:16:51 -0800 Subject: [PATCH] [Fixes #3524] Handle negative long values in TempData correctly --- .../SessionStateTempDataProvider.cs | 2 +- .../SessionStateTempDataProviderTest.cs | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/SessionStateTempDataProvider.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/SessionStateTempDataProvider.cs index 01a2f4e524..fff5442953 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/SessionStateTempDataProvider.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/SessionStateTempDataProvider.cs @@ -132,7 +132,7 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures else if (item.Value is long) { var longValue = (long)item.Value; - if (longValue < int.MaxValue) + if (longValue >= int.MinValue && longValue <= int.MaxValue) { // BsonReader casts all ints to longs. We'll attempt to work around this by force converting // longs to ints when there's no loss of precision. diff --git a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewFeatures/SessionStateTempDataProviderTest.cs b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewFeatures/SessionStateTempDataProviderTest.cs index b384c90881..07ae31f176 100644 --- a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewFeatures/SessionStateTempDataProviderTest.cs +++ b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/ViewFeatures/SessionStateTempDataProviderTest.cs @@ -170,14 +170,17 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures Assert.Equal("value", stringVal); } - [Fact] - public void SaveAndLoad_IntCanBeStoredAndLoaded() + [Theory] + [InlineData(10)] + [InlineData(int.MaxValue)] + [InlineData(int.MinValue)] + public void SaveAndLoad_IntCanBeStoredAndLoaded(int expected) { // Arrange var testProvider = new SessionStateTempDataProvider(); var input = new Dictionary { - { "int", 10 } + { "int", expected } }; var context = GetHttpContext(); @@ -187,7 +190,7 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures // Assert var intVal = Assert.IsType(TempData["int"]); - Assert.Equal(10, intVal); + Assert.Equal(expected, intVal); } [Theory] @@ -277,13 +280,14 @@ namespace Microsoft.AspNet.Mvc.ViewFeatures Assert.Equal(expected, actual); } - [Fact] - public void SaveAndLoad_LongCanBeSavedAndLoaded() + [Theory] + [InlineData(3100000000L)] + [InlineData(-3100000000L)] + public void SaveAndLoad_LongCanBeSavedAndLoaded(long expected) { // Arrange var key = "LongValue"; var testProvider = new SessionStateTempDataProvider(); - var expected = 3100000000L; var input = new Dictionary { { key, expected }