瀏覽代碼

[3.1.x] Backport HttpContext.Items fix (#17237)

Chris Ross 6 年之前
父節點
當前提交
6902b14471
共有 2 個文件被更改,包括 43 次插入2 次删除
  1. 2 2
      src/Http/Http/src/Internal/ItemsDictionary.cs
  2. 41 0
      src/Http/Http/test/Internal/ItemsDictionaryTests.cs

+ 2 - 2
src/Http/Http/src/Internal/ItemsDictionary.cs

@@ -101,7 +101,7 @@ namespace Microsoft.AspNetCore.Http
                 EmptyDictionary.Collection.CopyTo(array, arrayIndex);
                 EmptyDictionary.Collection.CopyTo(array, arrayIndex);
             }
             }
 
 
-            _items.CopyTo(array, arrayIndex);
+            _items?.CopyTo(array, arrayIndex);
         }
         }
 
 
         int ICollection<KeyValuePair<object, object>>.Count => _items?.Count ?? 0;
         int ICollection<KeyValuePair<object, object>>.Count => _items?.Count ?? 0;
@@ -133,7 +133,7 @@ namespace Microsoft.AspNetCore.Http
         IEnumerator<KeyValuePair<object, object>> IEnumerable<KeyValuePair<object, object>>.GetEnumerator()
         IEnumerator<KeyValuePair<object, object>> IEnumerable<KeyValuePair<object, object>>.GetEnumerator()
             => _items?.GetEnumerator() ?? EmptyEnumerator.Instance;
             => _items?.GetEnumerator() ?? EmptyEnumerator.Instance;
 
 
-        IEnumerator IEnumerable.GetEnumerator() => _items.GetEnumerator() ?? EmptyEnumerator.Instance;
+        IEnumerator IEnumerable.GetEnumerator() => _items?.GetEnumerator() ?? EmptyEnumerator.Instance;
 
 
         private class EmptyEnumerator : IEnumerator<KeyValuePair<object, object>>
         private class EmptyEnumerator : IEnumerator<KeyValuePair<object, object>>
         {
         {

+ 41 - 0
src/Http/Http/test/Internal/ItemsDictionaryTests.cs

@@ -0,0 +1,41 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Globalization;
+using System.IO;
+using System.IO.Pipelines;
+using Microsoft.AspNetCore.Http.Features;
+using Microsoft.AspNetCore.Routing;
+using Microsoft.Extensions.Primitives;
+using Xunit;
+
+namespace Microsoft.AspNetCore.Http
+{
+    public class ItemsDictionaryTests
+    {
+        [Fact]
+        public void GetEnumerator_ShouldResolveWithoutNullReferenceException()
+        {
+            // Arrange
+            var dict = new ItemsDictionary();
+
+            // Act and Assert
+            IEnumerable en = (IEnumerable) dict;
+            Assert.NotNull(en.GetEnumerator());
+        }
+
+        [Fact]
+        public void CopyTo_ShouldCopyItemsWithoutNullReferenceException() {
+            // Arrange
+            var dict = new ItemsDictionary();
+            var pairs = new KeyValuePair<object, object>[] { new KeyValuePair<object, object>("first", "value") };
+
+            // Act and Assert
+            ICollection<KeyValuePair<object, object>> cl = (ICollection<KeyValuePair<object, object>>) dict;
+            cl.CopyTo(pairs, 0);
+        }
+    }
+}