Skip to content

Commit 6574527

Browse files
authored
Add nullable annotations to Mvc.Core/Infrastructure (#28852)
1 parent 43f9b67 commit 6574527

File tree

78 files changed

+449
-333
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+449
-333
lines changed

src/Mvc/Mvc.Abstractions/src/Filters/ActionExecutedContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ public virtual ExceptionDispatchInfo? ExceptionDispatchInfo
9191
/// <summary>
9292
/// Gets or sets the <see cref="IActionResult"/>.
9393
/// </summary>
94-
public virtual IActionResult Result { get; set; } = default!;
94+
public virtual IActionResult? Result { get; set; }
9595
}
9696
}

src/Mvc/Mvc.Abstractions/src/Filters/ActionExecutingContext.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System;
57
using System.Collections.Generic;
68

@@ -24,7 +26,7 @@ public class ActionExecutingContext : FilterContext
2426
public ActionExecutingContext(
2527
ActionContext actionContext,
2628
IList<IFilterMetadata> filters,
27-
IDictionary<string, object> actionArguments,
29+
IDictionary<string, object?> actionArguments,
2830
object controller)
2931
: base(actionContext, filters)
3032
{
@@ -46,7 +48,7 @@ public ActionExecutingContext(
4648
/// <summary>
4749
/// Gets the arguments to pass when invoking the action. Keys are parameter names.
4850
/// </summary>
49-
public virtual IDictionary<string, object> ActionArguments { get; }
51+
public virtual IDictionary<string, object?> ActionArguments { get; }
5052

5153
/// <summary>
5254
/// Gets the controller instance containing the action.

src/Mvc/Mvc.Abstractions/src/Formatters/OutputFormatterWriteContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class OutputFormatterWriteContext : OutputFormatterCanWriteContext
2020
/// <param name="writerFactory">The delegate used to create a <see cref="TextWriter"/> for writing the response.</param>
2121
/// <param name="objectType">The <see cref="Type"/> of the object to write to the response.</param>
2222
/// <param name="object">The object to write to the response.</param>
23-
public OutputFormatterWriteContext(HttpContext httpContext, Func<Stream, Encoding, TextWriter> writerFactory, Type objectType, object @object)
23+
public OutputFormatterWriteContext(HttpContext httpContext, Func<Stream, Encoding, TextWriter> writerFactory, Type? objectType, object? @object)
2424
: base(httpContext)
2525
{
2626
if (writerFactory == null)

src/Mvc/Mvc.Abstractions/src/PublicAPI.Shipped.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Microsoft.AspNetCore.Mvc.Authorization.IAllowAnonymousFilter
170170
Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext
171171
Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext.ActionExecutedContext(Microsoft.AspNetCore.Mvc.ActionContext! actionContext, System.Collections.Generic.IList<Microsoft.AspNetCore.Mvc.Filters.IFilterMetadata!>! filters, object! controller) -> void
172172
Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext
173-
Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext.ActionExecutingContext(Microsoft.AspNetCore.Mvc.ActionContext! actionContext, System.Collections.Generic.IList<Microsoft.AspNetCore.Mvc.Filters.IFilterMetadata!>! filters, System.Collections.Generic.IDictionary<string!, object!>! actionArguments, object! controller) -> void
173+
Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext.ActionExecutingContext(Microsoft.AspNetCore.Mvc.ActionContext! actionContext, System.Collections.Generic.IList<Microsoft.AspNetCore.Mvc.Filters.IFilterMetadata!>! filters, System.Collections.Generic.IDictionary<string!, object?>! actionArguments, object! controller) -> void
174174
Microsoft.AspNetCore.Mvc.Filters.ActionExecutionDelegate
175175
Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext
176176
Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext.AuthorizationFilterContext(Microsoft.AspNetCore.Mvc.ActionContext! actionContext, System.Collections.Generic.IList<Microsoft.AspNetCore.Mvc.Filters.IFilterMetadata!>! filters) -> void
@@ -286,7 +286,7 @@ Microsoft.AspNetCore.Mvc.Formatters.InputFormatterResult.Model.get -> object?
286286
Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterCanWriteContext
287287
Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterCanWriteContext.OutputFormatterCanWriteContext(Microsoft.AspNetCore.Http.HttpContext! httpContext) -> void
288288
Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterWriteContext
289-
Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterWriteContext.OutputFormatterWriteContext(Microsoft.AspNetCore.Http.HttpContext! httpContext, System.Func<System.IO.Stream!, System.Text.Encoding!, System.IO.TextWriter!>! writerFactory, System.Type! objectType, object! object) -> void
289+
Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterWriteContext.OutputFormatterWriteContext(Microsoft.AspNetCore.Http.HttpContext! httpContext, System.Func<System.IO.Stream!, System.Text.Encoding!, System.IO.TextWriter!>! writerFactory, System.Type? objectType, object? object) -> void
290290
Microsoft.AspNetCore.Mvc.IActionResult
291291
Microsoft.AspNetCore.Mvc.IActionResult.ExecuteResultAsync(Microsoft.AspNetCore.Mvc.ActionContext! context) -> System.Threading.Tasks.Task!
292292
Microsoft.AspNetCore.Mvc.IUrlHelper
@@ -774,9 +774,9 @@ virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext.ExceptionDispatch
774774
virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext.ExceptionDispatchInfo.set -> void
775775
virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext.ExceptionHandled.get -> bool
776776
virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext.ExceptionHandled.set -> void
777-
virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext.Result.get -> Microsoft.AspNetCore.Mvc.IActionResult!
777+
virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext.Result.get -> Microsoft.AspNetCore.Mvc.IActionResult?
778778
virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutedContext.Result.set -> void
779-
virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext.ActionArguments.get -> System.Collections.Generic.IDictionary<string!, object!>!
779+
virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext.ActionArguments.get -> System.Collections.Generic.IDictionary<string!, object?>!
780780
virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext.Controller.get -> object!
781781
virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext.Result.get -> Microsoft.AspNetCore.Mvc.IActionResult?
782782
virtual Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext.Result.set -> void

src/Mvc/Mvc.Core/src/Infrastructure/ActionContextAccessor.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
6+
using System.Diagnostics.CodeAnalysis;
47
using System.Threading;
58

69
namespace Microsoft.AspNetCore.Mvc.Infrastructure
@@ -15,15 +18,16 @@ public class ActionContextAccessor : IActionContextAccessor
1518
private static readonly AsyncLocal<ActionContext> _storage = new AsyncLocal<ActionContext>();
1619

1720
/// <inheritdoc/>
18-
public ActionContext ActionContext
21+
[DisallowNull]
22+
public ActionContext? ActionContext
1923
{
2024
get { return _storage.Value; }
2125
set { _storage.Value = value; }
2226
}
2327

2428
private class NullActionContextAccessor : IActionContextAccessor
2529
{
26-
public ActionContext ActionContext
30+
public ActionContext? ActionContext
2731
{
2832
get => null;
2933
set { }

src/Mvc/Mvc.Core/src/Infrastructure/ActionDescriptorCollection.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System;
57
using System.Collections.Generic;
68
using Microsoft.AspNetCore.Mvc.Abstractions;
@@ -38,4 +40,4 @@ public ActionDescriptorCollection(IReadOnlyList<ActionDescriptor> items, int ver
3840
/// </summary>
3941
public int Version { get; }
4042
}
41-
}
43+
}

src/Mvc/Mvc.Core/src/Infrastructure/ActionDescriptorCollectionProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using Microsoft.AspNetCore.Mvc.Abstractions;
57
using Microsoft.Extensions.Primitives;
68

src/Mvc/Mvc.Core/src/Infrastructure/ActionInvokerFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System.Collections.Generic;
57
using System.Linq;
68
using Microsoft.AspNetCore.Mvc.Abstractions;
@@ -16,7 +18,7 @@ public ActionInvokerFactory(IEnumerable<IActionInvokerProvider> actionInvokerPro
1618
_actionInvokerProviders = actionInvokerProviders.OrderBy(item => item.Order).ToArray();
1719
}
1820

19-
public IActionInvoker CreateInvoker(ActionContext actionContext)
21+
public IActionInvoker? CreateInvoker(ActionContext actionContext)
2022
{
2123
var context = new ActionInvokerProviderContext(actionContext);
2224

src/Mvc/Mvc.Core/src/Infrastructure/ActionMethodExecutor.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System;
57
using System.Diagnostics;
68
using System.Threading.Tasks;
79
using Microsoft.AspNetCore.Mvc.Core;
8-
using Microsoft.AspNetCore.Mvc.Infrastructure;
910
using Microsoft.Extensions.Internal;
1011

1112
namespace Microsoft.AspNetCore.Mvc.Infrastructure
@@ -31,7 +32,7 @@ public abstract ValueTask<IActionResult> Execute(
3132
IActionResultTypeMapper mapper,
3233
ObjectMethodExecutor executor,
3334
object controller,
34-
object[] arguments);
35+
object?[]? arguments);
3536

3637
protected abstract bool CanExecute(ObjectMethodExecutor executor);
3738

@@ -56,7 +57,7 @@ public override ValueTask<IActionResult> Execute(
5657
IActionResultTypeMapper mapper,
5758
ObjectMethodExecutor executor,
5859
object controller,
59-
object[] arguments)
60+
object?[]? arguments)
6061
{
6162
executor.Execute(controller, arguments);
6263
return new ValueTask<IActionResult>(new EmptyResult());
@@ -74,9 +75,9 @@ public override ValueTask<IActionResult> Execute(
7475
IActionResultTypeMapper mapper,
7576
ObjectMethodExecutor executor,
7677
object controller,
77-
object[] arguments)
78+
object?[]? arguments)
7879
{
79-
var actionResult = (IActionResult)executor.Execute(controller, arguments);
80+
var actionResult = (IActionResult)executor.Execute(controller, arguments)!;
8081
EnsureActionResultNotNull(executor, actionResult);
8182

8283
return new ValueTask<IActionResult>(actionResult);
@@ -94,7 +95,7 @@ public override ValueTask<IActionResult> Execute(
9495
IActionResultTypeMapper mapper,
9596
ObjectMethodExecutor executor,
9697
object controller,
97-
object[] arguments)
98+
object?[]? arguments)
9899
{
99100
// Sync method returning arbitrary object
100101
var returnValue = executor.Execute(controller, arguments);
@@ -113,9 +114,9 @@ public override async ValueTask<IActionResult> Execute(
113114
IActionResultTypeMapper mapper,
114115
ObjectMethodExecutor executor,
115116
object controller,
116-
object[] arguments)
117+
object?[]? arguments)
117118
{
118-
await (Task)executor.Execute(controller, arguments);
119+
await (Task)executor.Execute(controller, arguments)!;
119120
return new EmptyResult();
120121
}
121122

@@ -130,7 +131,7 @@ public override async ValueTask<IActionResult> Execute(
130131
IActionResultTypeMapper mapper,
131132
ObjectMethodExecutor executor,
132133
object controller,
133-
object[] arguments)
134+
object?[]? arguments)
134135
{
135136
await executor.ExecuteAsync(controller, arguments);
136137
return new EmptyResult();
@@ -150,12 +151,12 @@ public override async ValueTask<IActionResult> Execute(
150151
IActionResultTypeMapper mapper,
151152
ObjectMethodExecutor executor,
152153
object controller,
153-
object[] arguments)
154+
object?[]? arguments)
154155
{
155156
// Async method returning Task<IActionResult>
156157
// Avoid extra allocations by calling Execute rather than ExecuteAsync and casting to Task<IActionResult>.
157158
var returnValue = executor.Execute(controller, arguments);
158-
var actionResult = await (Task<IActionResult>)returnValue;
159+
var actionResult = await (Task<IActionResult>)returnValue!;
159160
EnsureActionResultNotNull(executor, actionResult);
160161

161162
return actionResult;
@@ -173,7 +174,7 @@ public override async ValueTask<IActionResult> Execute(
173174
IActionResultTypeMapper mapper,
174175
ObjectMethodExecutor executor,
175176
object controller,
176-
object[] arguments)
177+
object?[]? arguments)
177178
{
178179
// Async method returning awaitable-of-IActionResult (e.g., Task<ViewResult>)
179180
// We have to use ExecuteAsync because we don't know the awaitable's type at compile time.
@@ -197,11 +198,11 @@ public override async ValueTask<IActionResult> Execute(
197198
IActionResultTypeMapper mapper,
198199
ObjectMethodExecutor executor,
199200
object controller,
200-
object[] arguments)
201+
object?[]? arguments)
201202
{
202203
// Async method returning awaitable-of-nonvoid
203204
var returnValue = await executor.ExecuteAsync(controller, arguments);
204-
var actionResult = ConvertToActionResult(mapper, returnValue, executor.AsyncResultType);
205+
var actionResult = ConvertToActionResult(mapper, returnValue, executor.AsyncResultType!);
205206
return actionResult;
206207
}
207208

@@ -217,7 +218,7 @@ private static void EnsureActionResultNotNull(ObjectMethodExecutor executor, IAc
217218
}
218219
}
219220

220-
private IActionResult ConvertToActionResult(IActionResultTypeMapper mapper, object returnValue, Type declaredType)
221+
private IActionResult ConvertToActionResult(IActionResultTypeMapper mapper, object? returnValue, Type declaredType)
221222
{
222223
var result = (returnValue as IActionResult) ?? mapper.Convert(returnValue, declaredType);
223224
if (result == null)

src/Mvc/Mvc.Core/src/Infrastructure/ActionResultObjectValueAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System;
57

68
namespace Microsoft.AspNetCore.Mvc.Infrastructure

0 commit comments

Comments
 (0)