Skip to content

Commit e55796c

Browse files
authored
Fix TempData lazy loading bugs (#65722)
1 parent cb2bd84 commit e55796c

3 files changed

Lines changed: 85 additions & 11 deletions

File tree

src/Components/Endpoints/src/TempData/TempData.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public object? this[string key]
4949

5050
public object? Get(string key)
5151
{
52+
var value = Data.GetValueOrDefault(key);
5253
_retainedKeys.Remove(key);
53-
return Data.GetValueOrDefault(key);
54+
return value;
5455
}
5556

5657
public object? Peek(string key)
@@ -60,7 +61,7 @@ public object? this[string key]
6061

6162
public void Keep()
6263
{
63-
_retainedKeys.UnionWith(_data.Keys);
64+
_retainedKeys.UnionWith(Data.Keys);
6465
}
6566

6667
public void Keep(string key)
@@ -78,8 +79,9 @@ public bool ContainsKey(string key)
7879

7980
public bool Remove(string key)
8081
{
82+
var removed = Data.Remove(key);
8183
_retainedKeys.Remove(key);
82-
return Data.Remove(key);
84+
return removed;
8385
}
8486

8587
public IDictionary<string, object?> Save()
@@ -175,7 +177,7 @@ sealed class TempDataEnumerator : IEnumerator<KeyValuePair<string, object?>>
175177
public TempDataEnumerator(TempData tempData)
176178
{
177179
_tempData = tempData;
178-
_innerEnumerator = tempData._data.GetEnumerator();
180+
_innerEnumerator = tempData.Data.GetEnumerator();
179181
}
180182

181183
public KeyValuePair<string, object?> Current

src/Components/Endpoints/test/TempData/TempDataTest.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,80 @@ public void TempData_Loads_WhenLoaded()
250250
Assert.True(tempData.WasLoaded);
251251
Assert.Equal("Value", value);
252252
}
253+
254+
[Fact]
255+
public void Get_ConsumesValue_WhenFirstAccessTriggersLazyLoad()
256+
{
257+
var tempData = new TempData(() => new Dictionary<string, object>
258+
{
259+
["Message"] = "hello"
260+
});
261+
262+
var value = tempData.Get("Message");
263+
var saved = tempData.Save();
264+
265+
Assert.Equal("hello", value);
266+
Assert.Empty(saved);
267+
}
268+
269+
[Fact]
270+
public void Indexer_ConsumesValue_WhenFirstAccessTriggersLazyLoad()
271+
{
272+
var tempData = new TempData(() => new Dictionary<string, object>
273+
{
274+
["Message"] = "hello"
275+
});
276+
277+
var value = tempData["Message"];
278+
var saved = tempData.Save();
279+
280+
Assert.Equal("hello", value);
281+
Assert.Empty(saved);
282+
}
283+
284+
[Fact]
285+
public void Remove_RemovesValue_WhenFirstAccessTriggersLazyLoad()
286+
{
287+
var tempData = new TempData(() => new Dictionary<string, object>
288+
{
289+
["Message"] = "hello"
290+
});
291+
292+
var result = tempData.Remove("Message");
293+
var saved = tempData.Save();
294+
295+
Assert.True(result);
296+
Assert.Empty(saved);
297+
}
298+
299+
[Fact]
300+
public void Keep_RetainsAllKeys_WhenFirstAccessTriggersLazyLoad()
301+
{
302+
var tempData = new TempData(() => new Dictionary<string, object>
303+
{
304+
["Key1"] = "Value1",
305+
["Key2"] = "Value2"
306+
});
307+
308+
tempData.Keep();
309+
var saved = tempData.Save();
310+
311+
Assert.Equal(2, saved.Count);
312+
Assert.Equal("Value1", saved["Key1"]);
313+
Assert.Equal("Value2", saved["Key2"]);
314+
}
315+
316+
[Fact]
317+
public void Enumerator_IteratesAllKeys_WhenFirstAccessTriggersLazyLoad()
318+
{
319+
var tempData = new TempData(() => new Dictionary<string, object>
320+
{
321+
["Key1"] = "Value1",
322+
["Key2"] = "Value2"
323+
});
324+
325+
var items = ((IEnumerable<KeyValuePair<string, object>>)tempData).ToList();
326+
327+
Assert.Equal(2, items.Count);
328+
}
253329
}

src/Components/test/testassets/Components.TestServer/RazorComponents/Pages/TempData/TempDataComponent.razor

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@
6565

6666
protected override void OnInitialized()
6767
{
68-
_containsMessageKey = TempData?.ContainsKey("Message") ?? false;
69-
_containsPeekedValueKey = TempData?.ContainsKey("PeekedValue") ?? false;
70-
71-
if (Handler is not null)
72-
{
73-
return;
74-
}
7568
_message = TempData!.Get("Message") as string ?? "No message";
7669
_peekedValue = TempData!.Peek("PeekedValue") as string ?? "No peeked value";
7770
_keptValue = TempData!.Get("KeptValue") as string ?? "No kept value";
@@ -82,6 +75,9 @@
8275
_stringArrayResult = stringArray is string[] arr ? string.Join(",", arr) : $"Wrong type: {stringArray?.GetType().Name ?? "null"}";
8376
_intArrayResult = intArray is int[] nums ? string.Join(",", nums) : $"Wrong type: {intArray?.GetType().Name ?? "null"}";
8477

78+
_containsMessageKey = TempData?.ContainsKey("Message") ?? false;
79+
_containsPeekedValueKey = TempData?.ContainsKey("PeekedValue") ?? false;
80+
8581
if (ValueToKeep == "all")
8682
{
8783
TempData!.Keep();

0 commit comments

Comments
 (0)