Skip to content

Commit

Permalink
Prepare for nullability changes in ASP.NET 6
Browse files Browse the repository at this point in the history
ASP.NET 6.0 contains some nullability changes [1] that, in
the absence of these null-forgiving operators, would generate CS8602 and
CS8604 warnings after upgrading.

[1] e.g. aspnet/Announcements#444 and
dotnet/aspnetcore#31338
  • Loading branch information
smfeest committed Dec 31, 2021
1 parent d9943e3 commit 4b115d3
Show file tree
Hide file tree
Showing 14 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public async Task ChangePasswordPostAddsErrorWhenCurrentPasswordIsIncorrect()

var errors = fixture
.AccountController
.ModelState[nameof(ChangePasswordViewModel.CurrentPassword)]
.ModelState[nameof(ChangePasswordViewModel.CurrentPassword)]!
.Errors;

var error = Assert.Single(errors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public async Task SignInPostAddsErrorWhenAuthenticationFails()
await fixture.SignInPost();

var error = Assert.Single(
fixture.AuthenticationController.ModelState[string.Empty].Errors);
fixture.AuthenticationController.ModelState[string.Empty]!.Errors);

Assert.Equal("translated-wrong-email-or-password-error", error.ErrorMessage);
}
Expand Down Expand Up @@ -313,7 +313,7 @@ public void SignOutSetsCacheControlHeader()

var result = fixture.AuthenticationController.SignOut();

var cacheControlHeader = fixture.HttpContext.Response.GetTypedHeaders().CacheControl;
var cacheControlHeader = fixture.HttpContext.Response.GetTypedHeaders().CacheControl!;

Assert.True(cacheControlHeader.NoCache);
Assert.True(cacheControlHeader.NoStore);
Expand Down
4 changes: 2 additions & 2 deletions src/Buttercup.Web.Tests/Controllers/RecipesControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public async Task NewPostAddsRecipeAndRedirectsToShowPage()

var redirectResult = Assert.IsType<RedirectToActionResult>(result);
Assert.Equal(nameof(RecipesController.Show), redirectResult.ActionName);
Assert.Equal(5L, redirectResult.RouteValues["id"]);
Assert.Equal(5L, redirectResult.RouteValues!["id"]);
}

[Fact]
Expand Down Expand Up @@ -162,7 +162,7 @@ public async Task EditPostUpdatesRecipeAndRedirectsToShowPage()

var redirectResult = Assert.IsType<RedirectToActionResult>(result);
Assert.Equal(nameof(RecipesController.Show), redirectResult.ActionName);
Assert.Equal(3L, redirectResult.RouteValues["id"]);
Assert.Equal(3L, redirectResult.RouteValues!["id"]);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void OnActionExecutingRedirectsToSignOutIfAuthenticated()

Assert.Equal("Authentication", redirectResult.ControllerName);
Assert.Equal(nameof(AuthenticationController.SignOut), redirectResult.ActionName);
Assert.Equal(new PathString(RequestPath), redirectResult.RouteValues["returnUrl"]);
Assert.Equal(new PathString(RequestPath), redirectResult.RouteValues!["returnUrl"]);
}

[Fact]
Expand All @@ -43,7 +43,7 @@ public void OnActionExecutingSetsCacheControlHeader()
var actionExecutingContext = CallOnActionExecuting(null);

var cacheControlHeader =
actionExecutingContext.HttpContext.Response.GetTypedHeaders().CacheControl;
actionExecutingContext.HttpContext.Response.GetTypedHeaders().CacheControl!;

Assert.True(cacheControlHeader.NoCache);
Assert.True(cacheControlHeader.NoStore);
Expand All @@ -64,7 +64,7 @@ private static ActionExecutingContext CallOnActionExecuting(string? authenticati
var actionExecutingContext = new ActionExecutingContext(
new(httpContext, new(), new()),
Array.Empty<IFilterMetadata>(),
new Dictionary<string, object>(),
new Dictionary<string, object?>(),
new());

new EnsureSignedOutAttribute().OnActionExecuting(actionExecutingContext);
Expand Down
4 changes: 2 additions & 2 deletions src/Buttercup.Web.Tests/TestUtils/ListLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter) =>
Exception? exception,
Func<TState, Exception?, string> formatter) =>
this.entries.Add(new(logLevel, eventId, formatter(state, exception), state, exception));

private sealed class NullDisposable : IDisposable
Expand Down
2 changes: 1 addition & 1 deletion src/Buttercup.Web.Tests/TestUtils/LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
namespace Buttercup.Web.TestUtils
{
public record LogEntry(
LogLevel LogLevel, EventId EventId, string Message, object? State, Exception Exception);
LogLevel LogLevel, EventId EventId, string Message, object? State, Exception? Exception);
}
2 changes: 1 addition & 1 deletion src/Buttercup.Web/Views/Account/Show.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="account-overview__item">
<div class="account-overview__item-content">
<div class="account-overview__item-label">@Localizer["Label_Name"]</div>
<div class="account-overview__item-data">@Model.Name</div>
<div class="account-overview__item-data">@Model!.Name</div>
</div>
</div>
<div class="account-overview__item">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
ViewData["Title"] = Localizer["Title_ResetPassword"];
}
<h1>@Localizer["Title_ResetPassword"]</h1>
<p>@Localizer["Message_PasswordResetLinkSent", @Model.Email]</p>
<p>@Localizer["Message_PasswordResetLinkSent", @Model!.Email!]</p>
2 changes: 1 addition & 1 deletion src/Buttercup.Web/Views/Error/Error.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<h1>@Localizer["Title_Error"]</h1>
<p>@Localizer["Message_GeneralError"]</p>

@if (Model.ShowRequestId)
@if (Model!.ShowRequestId)
{
<p>
<strong>@Localizer["Label_RequestId"]:</strong> <code>@Model.RequestId</code>
Expand Down
2 changes: 1 addition & 1 deletion src/Buttercup.Web/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<section class="home-page__recipe-list">
<h1>@Localizer["Heading_RecentlyAdded"]</h1>
<ul>
@foreach (var recipe in Model.RecentlyAddedRecipes!)
@foreach (var recipe in Model!.RecentlyAddedRecipes!)
{
<li><a asp-controller="Recipes" asp-action="Show" asp-route-id="@recipe.Id">@recipe.Title</a></li>
}
Expand Down
4 changes: 2 additions & 2 deletions src/Buttercup.Web/Views/Recipes/Delete.cshtml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
@inject IViewLocalizer Localizer
@model Recipe
@{
ViewData["Title"] = Localizer["Title_DeleteRecipe", Model.Title];
ViewData["Title"] = Localizer["Title_DeleteRecipe", Model!.Title!];
}
<h1>@Localizer["Heading_DeleteRecipe"]</h1>
<form method="post">
<input type="hidden" asp-for="Revision">
<p>@Localizer["Message_DeleteConfirmation", Model.Title]</p>
<p>@Localizer["Message_DeleteConfirmation", Model.Title!]</p>
<div class="button-bar">
<button class="critical-push-button">@Localizer["Label_DeleteForever"]</button>
<a class="push-button" asp-action="Show" asp-route-id="@Model.Id">@Localizer["Label_Cancel"]</a>
Expand Down
2 changes: 1 addition & 1 deletion src/Buttercup.Web/Views/Recipes/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@inject IViewLocalizer Localizer
@model RecipeEditModel
@{
ViewData["Title"] = Localizer["Title_EditRecipe", Model.Title];
ViewData["Title"] = Localizer["Title_EditRecipe", Model!.Title!];
}
<h1>@Localizer["Heading_EditRecipe"]</h1>
<form method="post">
Expand Down
2 changes: 1 addition & 1 deletion src/Buttercup.Web/Views/Recipes/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</tr>
</thead>
<tbody>
@foreach (var recipe in Model)
@foreach (var recipe in Model!)
{
<tr>
<td><a asp-action="Show" asp-route-id="@recipe.Id">@recipe.Title</a></td>
Expand Down
2 changes: 1 addition & 1 deletion src/Buttercup.Web/Views/Recipes/Show.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@inject IViewLocalizer Localizer
@model Recipe
@{
ViewData["Title"] = Model.Title;
ViewData["Title"] = Model!.Title;
}
<h1>@Model.Title</h1>

Expand Down

0 comments on commit 4b115d3

Please sign in to comment.