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

Commit 27b8048

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

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

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

Lines changed: 50 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,29 @@ 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+
{ JTokenType.Bytes, typeof(Byte) },
49+
};
50+
2851
/// <inheritdoc />
2952
public virtual IDictionary<string, object> LoadTempData([NotNull] HttpContext context)
3053
{
@@ -45,6 +68,26 @@ public virtual IDictionary<string, object> LoadTempData([NotNull] HttpContext co
4568
{
4669
tempDataDictionary = _jsonSerializer.Deserialize<Dictionary<string, object>>(writer);
4770
}
71+
foreach (var item in tempDataDictionary.ToList())
72+
{
73+
var jArrayValue = item.Value as JArray;
74+
if (jArrayValue != null && jArrayValue.Count > 0)
75+
{
76+
Type returnType = null;
77+
_arrayTypeLookup.TryGetValue(jArrayValue[0].Type, out returnType);
78+
if (returnType != null)
79+
{
80+
var arrayConverter = _arrayConverters.GetOrAdd(returnType, type =>
81+
{
82+
return (Func<JArray, object>)Delegate.CreateDelegate(typeof(Func<JArray, object>),
83+
_convertArrayMethodInfo.MakeGenericMethod(type));
84+
});
85+
var result = arrayConverter(jArrayValue);
86+
87+
tempDataDictionary[item.Key] = result;
88+
}
89+
}
90+
}
4891

4992
// If we got it from Session, remove it so that no other request gets it
5093
session.Remove(TempDataSessionStateKey);
@@ -104,8 +147,7 @@ internal void EnsureObjectCanBeSerialized(object item)
104147
}
105148
else if (itemType.GetTypeInfo().IsGenericType)
106149
{
107-
if (itemType.ExtractGenericInterface(typeof(IList<>)) != null ||
108-
itemType.ExtractGenericInterface(typeof(IDictionary<,>)) != null)
150+
if (itemType.ExtractGenericInterface(typeof(IList<>)) != null)
109151
{
110152
actualTypes = itemType.GetGenericArguments();
111153
}
@@ -124,5 +166,10 @@ internal void EnsureObjectCanBeSerialized(object item)
124166
}
125167
}
126168
}
169+
170+
private static IList<TVal> ConvertArray<TVal>(JArray array)
171+
{
172+
return array.Values<TVal>().ToArray();
173+
}
127174
}
128175
}

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)