Skip to content

Commit 7cfa5ca

Browse files
committed
Merge branch 'maintenance/code-analysis' into develop
With the tests updated to .NET 6, additional nullability hints were exposed, which allowed us to address potential nulls via our code in alignment with these hints. In addition, did some minor refactoring to take advantage of pattern matching in the filter attributes.
2 parents 6b827de + 1fed75b commit 7cfa5ca

4 files changed

Lines changed: 13 additions & 19 deletions

File tree

OnTopic.AspNetCore.Mvc.Tests/TopicViewComponentTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public async Task Menu_Invoke_ReturnsNavigationViewModel() {
121121

122122
var result = await viewComponent.InvokeAsync().ConfigureAwait(false);
123123
var concreteResult = result as ViewViewComponentResult;
124-
var model = concreteResult?.ViewData.Model as NavigationViewModel<NavigationTopicViewModel>;
124+
var model = concreteResult?.ViewData?.Model as NavigationViewModel<NavigationTopicViewModel>;
125125

126126
Assert.NotNull(model);
127127
Assert.Equal(_topic.GetWebPath(), model?.CurrentWebPath);
@@ -147,7 +147,7 @@ public async Task Menu_Invoke_ReturnsConfiguredNavigationRoot() {
147147

148148
var result = await viewComponent.InvokeAsync().ConfigureAwait(false);
149149
var concreteResult = result as ViewViewComponentResult;
150-
var model = concreteResult?.ViewData.Model as NavigationViewModel<NavigationTopicViewModel>;
150+
var model = concreteResult?.ViewData?.Model as NavigationViewModel<NavigationTopicViewModel>;
151151

152152
Assert.NotNull(model);
153153
Assert.Equal(webPath, model?.CurrentWebPath);
@@ -194,7 +194,7 @@ public async Task PageLevelNavigation_Invoke_ReturnsNavigationViewModel() {
194194

195195
var result = await viewComponent.InvokeAsync().ConfigureAwait(false);
196196
var concreteResult = result as ViewViewComponentResult;
197-
var model = concreteResult?.ViewData.Model as NavigationViewModel<NavigationTopicViewModel>;
197+
var model = concreteResult?.ViewData?.Model as NavigationViewModel<NavigationTopicViewModel>;
198198

199199
Assert.NotNull(model);
200200
Assert.Equal(_topic.GetWebPath(), model?.CurrentWebPath);
@@ -222,7 +222,7 @@ public async Task PageLevelNavigation_Invoke_ReturnsNull() {
222222

223223
var result = await viewComponent.InvokeAsync().ConfigureAwait(false);
224224
var concreteResult = result as ViewViewComponentResult;
225-
var model = concreteResult?.ViewData.Model as NavigationViewModel<NavigationTopicViewModel>;
225+
var model = concreteResult?.ViewData?.Model as NavigationViewModel<NavigationTopicViewModel>;
226226

227227
Assert.NotNull(model);
228228
Assert.Equal(webPath, model?.CurrentWebPath);
@@ -250,7 +250,7 @@ public async Task PageLevelNavigation_InvokeWithNullTopic_ReturnsNull()
250250

251251
var result = await viewComponent.InvokeAsync().ConfigureAwait(false);
252252
var concreteResult = result as ViewViewComponentResult;
253-
var model = concreteResult?.ViewData.Model as NavigationViewModel<NavigationTopicViewModel>;
253+
var model = concreteResult?.ViewData?.Model as NavigationViewModel<NavigationTopicViewModel>;
254254

255255
Assert.NotNull(model);
256256
Assert.Equal(String.Empty, model?.CurrentWebPath);

OnTopic.AspNetCore.Mvc.Tests/ValidateTopicAttributeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static ActionExecutingContext GetActionExecutingContext(Controller contro
4444
var actionExecutingContext = new ActionExecutingContext(
4545
actionContext,
4646
new List<IFilterMetadata>(),
47-
new Dictionary<string, object>(),
47+
new Dictionary<string, object?>(),
4848
controller
4949
);
5050

OnTopic.AspNetCore.Mvc/_filters/TopicResponseCacheAttribute.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public override void OnActionExecuting(ActionExecutingContext context) {
6262
/*------------------------------------------------------------------------------------------------------------------------
6363
| Validate controller
6464
\-----------------------------------------------------------------------------------------------------------------------*/
65-
var controller = context.Controller as TopicController;
66-
67-
if (controller is null) {
65+
if (context.Controller is not TopicController controller) {
6866
throw new InvalidOperationException(
6967
$"The {nameof(TopicResponseCacheAttribute)} can only be applied to a controller deriving from {nameof(TopicController)}."
7068
);

OnTopic.AspNetCore.Mvc/_filters/ValidateTopicAttribute.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,19 @@ public override void OnActionExecuting(ActionExecutingContext context) {
6060
Contract.Requires(context, nameof(context));
6161

6262
/*------------------------------------------------------------------------------------------------------------------------
63-
| Establish variables
63+
| Validate controller
6464
\-----------------------------------------------------------------------------------------------------------------------*/
65-
var controller = context.Controller as TopicController;
66-
var currentTopic = controller?.CurrentTopic;
67-
68-
/*------------------------------------------------------------------------------------------------------------------------
69-
| Validate context
70-
\-----------------------------------------------------------------------------------------------------------------------*/
71-
if (controller is null) {
65+
if (context.Controller is not TopicController controller) {
7266
throw new InvalidOperationException(
73-
$"The {nameof(ValidateTopicAttribute)} can only be applied to a controller deriving from {nameof(TopicController)}."
67+
$"The {nameof(TopicResponseCacheAttribute)} can only be applied to a controller deriving from {nameof(TopicController)}."
7468
);
7569
}
7670

7771
/*------------------------------------------------------------------------------------------------------------------------
78-
| Handle exceptions
72+
| Validate current topic
7973
\-----------------------------------------------------------------------------------------------------------------------*/
74+
var currentTopic = controller.CurrentTopic;
75+
8076
if (currentTopic is null) {
8177
if (!AllowNull) {
8278
context.Result = controller.NotFound("There is no topic associated with this path.");

0 commit comments

Comments
 (0)