Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit b207805

Browse files
committed
added support for list and removed support for dict
1 parent 467877b commit b207805

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

src/Microsoft.AspNet.Mvc.Core/SessionStateTempDataProvider.cs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Concurrent;
56
using System.Collections.Generic;
67
using System.IO;
8+
using System.Linq;
79
using System.Reflection;
810
using Microsoft.AspNet.Http;
911
using Microsoft.AspNet.Mvc.Core;
1012
using Microsoft.Framework.Internal;
1113
using Newtonsoft.Json;
1214
using Newtonsoft.Json.Bson;
15+
using Newtonsoft.Json.Linq;
1316

1417
namespace Microsoft.AspNet.Mvc
1518
{
@@ -22,9 +25,28 @@ public class SessionStateTempDataProvider : ITempDataProvider
2225
private readonly JsonSerializer _jsonSerializer = JsonSerializer.Create(
2326
new JsonSerializerSettings()
2427
{
25-
TypeNameHandling = TypeNameHandling.Auto
28+
TypeNameHandling = TypeNameHandling.None
2629
});
2730

31+
private static readonly MethodInfo _convertArrayMethodInfo = typeof(SessionStateTempDataProvider).GetMethod(
32+
nameof(ConvertArray), BindingFlags.NonPublic | BindingFlags.Static, null, new[] { typeof(JArray) }, null);
33+
34+
private readonly ConcurrentDictionary<Type, Func<JArray, object>> _arrayConverters =
35+
new ConcurrentDictionary<Type, Func<JArray, object>>();
36+
37+
private static Dictionary<JTokenType, Type> _arrayTypeLookup = new Dictionary<JTokenType, Type>
38+
{
39+
{ JTokenType.String, typeof(string) },
40+
{ JTokenType.Integer, typeof(int) },
41+
{ JTokenType.Boolean, typeof(bool) },
42+
{ JTokenType.Float, typeof(float) },
43+
{ JTokenType.Guid, typeof(Guid) },
44+
{ JTokenType.Object, typeof(object) },
45+
{ JTokenType.Date, typeof(DateTime) },
46+
{ JTokenType.TimeSpan, typeof(TimeSpan) },
47+
{ JTokenType.Uri, typeof(Uri) },
48+
};
49+
2850
/// <inheritdoc />
2951
public virtual IDictionary<string, object> LoadTempData([NotNull] HttpContext context)
3052
{
@@ -45,6 +67,26 @@ public virtual IDictionary<string, object> LoadTempData([NotNull] HttpContext co
4567
{
4668
tempDataDictionary = _jsonSerializer.Deserialize<Dictionary<string, object>>(writer);
4769
}
70+
foreach (var item in tempDataDictionary.ToList())
71+
{
72+
var jArrayValue = item.Value as JArray;
73+
if (jArrayValue != null && jArrayValue.Count > 0)
74+
{
75+
Type returnType = null;
76+
_arrayTypeLookup.TryGetValue(jArrayValue[0].Type, out returnType);
77+
if (returnType != null)
78+
{
79+
var arrayConverter = _arrayConverters.GetOrAdd(returnType, type =>
80+
{
81+
return (Func<JArray, object>)Delegate.CreateDelegate(typeof(Func<JArray, object>),
82+
_convertArrayMethodInfo.MakeGenericMethod(type));
83+
});
84+
var result = arrayConverter(jArrayValue);
85+
86+
tempDataDictionary[item.Key] = result;
87+
}
88+
}
89+
}
4890

4991
// If we got it from Session, remove it so that no other request gets it
5092
session.Remove(TempDataSessionStateKey);
@@ -104,8 +146,7 @@ internal void EnsureObjectCanBeSerialized(object item)
104146
}
105147
else if (itemType.GetTypeInfo().IsGenericType)
106148
{
107-
if (itemType.ExtractGenericInterface(typeof(IList<>)) != null ||
108-
itemType.ExtractGenericInterface(typeof(IDictionary<,>)) != null)
149+
if (itemType.ExtractGenericInterface(typeof(IList<>)) != null)
109150
{
110151
actualTypes = itemType.GetGenericArguments();
111152
}
@@ -124,5 +165,10 @@ internal void EnsureObjectCanBeSerialized(object item)
124165
}
125166
}
126167
}
168+
169+
private static IList<TVal> ConvertArray<TVal>(JArray array)
170+
{
171+
return array.Values<TVal>().ToArray();
172+
}
127173
}
128174
}

test/Microsoft.AspNet.Mvc.Core.Test/SessionStateTempDataProviderTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ public static TheoryData<object, Type> InvalidTypes
8686
{ new object[3], typeof(object) },
8787
{ new TestItem(), typeof(TestItem) },
8888
{ new List<TestItem>(), typeof(TestItem) },
89-
{ new Dictionary<string, TestItem>(), typeof(TestItem) },
90-
{ new Dictionary<object, string>(), typeof(object) },
91-
{ new Dictionary<TestItem, TestItem>(), typeof(TestItem) }
89+
{ new Dictionary<string, int>(), typeof(Dictionary<string, int>) },
90+
{ new Dictionary<Uri, Guid>(), typeof(Dictionary<Uri, Guid>) },
91+
{ new Dictionary<string, TestItem>(), typeof(Dictionary<string, TestItem>) },
92+
{ new Dictionary<object, string>(), typeof(Dictionary<object, string>) },
93+
{ new Dictionary<TestItem, TestItem>(), typeof(Dictionary<TestItem, TestItem>) }
9294
};
9395
}
9496
}
@@ -118,13 +120,11 @@ public static TheoryData<object> ValidTypes
118120
{ 10 },
119121
{ new int[]{ 10, 20 } },
120122
{ "FooValue" },
121-
{ new Dictionary<string, int>() },
122123
{ new Uri("http://Foo") },
123124
{ Guid.NewGuid() },
124125
{ new List<string> { "foo", "bar" } },
125126
{ new DateTimeOffset() },
126127
{ 100.1m },
127-
{ new Dictionary<Uri, Guid>() },
128128
{ new Uri[] { new Uri("http://Foo"), new Uri("http://Bar") } }
129129
};
130130
}

0 commit comments

Comments
 (0)