Skip to content

Commit 58ac1ec

Browse files
author
Bart Koelman
committed
Fix nullability warnings produced by .NET 6 with EF Core 6
1 parent 4b61bf7 commit 58ac1ec

File tree

11 files changed

+20
-20
lines changed

11 files changed

+20
-20
lines changed

src/JsonApiDotNetCore/Configuration/InverseNavigationResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private void Resolve(DbContext dbContext)
3838
{
3939
foreach (ResourceType resourceType in _resourceGraph.GetResourceTypes().Where(resourceType => resourceType.Relationships.Any()))
4040
{
41-
IEntityType entityType = dbContext.Model.FindEntityType(resourceType.ClrType);
41+
IEntityType? entityType = dbContext.Model.FindEntityType(resourceType.ClrType);
4242

4343
if (entityType != null)
4444
{

src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private async Task<bool> ValidateIfMatchHeaderAsync(HttpContext httpContext, Jso
134134
private static async Task<bool> ValidateContentTypeHeaderAsync(string allowedContentType, HttpContext httpContext,
135135
JsonSerializerOptions serializerOptions)
136136
{
137-
string contentType = httpContext.Request.ContentType;
137+
string? contentType = httpContext.Request.ContentType;
138138

139139
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
140140
// Justification: Workaround for https://github.com/dotnet/aspnetcore/issues/32097 (fixed in .NET 6)

src/JsonApiDotNetCore/Queries/Internal/QueryableBuilding/SelectClauseBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private ICollection<PropertySelector> ToPropertySelectors(IDictionary<ResourceFi
109109

110110
foreach (IProperty entityProperty in entityProperties)
111111
{
112-
var propertySelector = new PropertySelector(entityProperty.PropertyInfo);
112+
var propertySelector = new PropertySelector(entityProperty.PropertyInfo!);
113113

114114
if (propertySelector.Property.SetMethod != null)
115115
{

src/JsonApiDotNetCore/Repositories/EntityFrameworkCoreRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ private bool RequiresLoadOfRelationshipForDeletion(RelationshipAttribute relatio
379379

380380
private INavigation? GetNavigation(RelationshipAttribute relationship)
381381
{
382-
IEntityType entityType = _dbContext.Model.FindEntityType(typeof(TResource));
382+
IEntityType? entityType = _dbContext.Model.FindEntityType(typeof(TResource));
383383
return entityType?.FindNavigation(relationship.Property.Name);
384384
}
385385

src/JsonApiDotNetCore/Serialization/Response/LinkBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private bool ShouldIncludeResourceLink(LinkTypes linkType, ResourceType resource
257257
return _options.ResourceLinks.HasFlag(linkType);
258258
}
259259

260-
private string GetLinkForResourceSelf(ResourceType resourceType, string resourceId)
260+
private string? GetLinkForResourceSelf(ResourceType resourceType, string resourceId)
261261
{
262262
string? controllerName = _controllerResourceMapping.GetControllerNameForResourceType(resourceType);
263263
IDictionary<string, object?> routeValues = GetRouteValues(resourceId, null);
@@ -286,15 +286,15 @@ private string GetLinkForResourceSelf(ResourceType resourceType, string resource
286286
return links.HasValue() ? links : null;
287287
}
288288

289-
private string GetLinkForRelationshipSelf(string leftId, RelationshipAttribute relationship)
289+
private string? GetLinkForRelationshipSelf(string leftId, RelationshipAttribute relationship)
290290
{
291291
string? controllerName = _controllerResourceMapping.GetControllerNameForResourceType(relationship.LeftType);
292292
IDictionary<string, object?> routeValues = GetRouteValues(leftId, relationship.PublicName);
293293

294294
return RenderLinkForAction(controllerName, GetRelationshipControllerActionName, routeValues);
295295
}
296296

297-
private string GetLinkForRelationshipRelated(string leftId, RelationshipAttribute relationship)
297+
private string? GetLinkForRelationshipRelated(string leftId, RelationshipAttribute relationship)
298298
{
299299
string? controllerName = _controllerResourceMapping.GetControllerNameForResourceType(relationship.LeftType);
300300
IDictionary<string, object?> routeValues = GetRouteValues(leftId, relationship.PublicName);
@@ -315,11 +315,11 @@ private string GetLinkForRelationshipRelated(string leftId, RelationshipAttribut
315315
return routeValues;
316316
}
317317

318-
protected virtual string RenderLinkForAction(string? controllerName, string actionName, IDictionary<string, object?> routeValues)
318+
protected virtual string? RenderLinkForAction(string? controllerName, string actionName, IDictionary<string, object?> routeValues)
319319
{
320320
return _options.UseRelativeLinks
321-
? _linkGenerator.GetPathByAction(_httpContextAccessor.HttpContext, actionName, controllerName, routeValues)
322-
: _linkGenerator.GetUriByAction(_httpContextAccessor.HttpContext, actionName, controllerName, routeValues);
321+
? _linkGenerator.GetPathByAction(HttpContext, actionName, controllerName, routeValues)
322+
: _linkGenerator.GetUriByAction(HttpContext, actionName, controllerName, routeValues);
323323
}
324324

325325
/// <summary>

test/JsonApiDotNetCoreTests/IntegrationTests/Archiving/TelevisionBroadcastDefinition.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using JsonApiDotNetCore.Resources;
1414
using JsonApiDotNetCore.Resources.Annotations;
1515
using JsonApiDotNetCore.Serialization.Objects;
16-
using Microsoft.EntityFrameworkCore;
16+
using TestBuildingBlocks;
1717

1818
namespace JsonApiDotNetCoreTests.IntegrationTests.Archiving
1919
{
@@ -134,8 +134,7 @@ public override async Task OnWritingAsync(TelevisionBroadcast broadcast, WriteOp
134134
}
135135
else if (writeOperation == WriteOperationKind.DeleteResource)
136136
{
137-
TelevisionBroadcast broadcastToDelete =
138-
await _dbContext.Broadcasts.FirstOrDefaultAsync(resource => resource.Id == broadcast.Id, cancellationToken);
137+
TelevisionBroadcast? broadcastToDelete = await _dbContext.Broadcasts.FirstWithIdAsync(broadcast.Id, cancellationToken);
139138

140139
if (broadcastToDelete != null)
141140
{

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Transactions/LyricRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public sealed class LyricRepository : EntityFrameworkCoreRepository<Lyric, long>
1313
{
1414
private readonly ExtraDbContext _extraDbContext;
1515

16-
public override string TransactionId => _extraDbContext.Database.CurrentTransaction.TransactionId.ToString();
16+
public override string? TransactionId => _extraDbContext.Database.CurrentTransaction?.TransactionId.ToString();
1717

1818
public LyricRepository(ExtraDbContext extraDbContext, ITargetedFields targetedFields, IDbContextResolver dbContextResolver,
1919
IResourceGraph resourceGraph, IResourceFactory resourceFactory, IEnumerable<IQueryConstraintProvider> constraintProviders,

test/JsonApiDotNetCoreTests/IntegrationTests/CompositeKeys/CompositeKeyTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
186186

187187
await _testContext.RunOnDatabaseAsync(async dbContext =>
188188
{
189-
Car carInDatabase = await dbContext.Cars.FirstOrDefaultAsync(car => car.RegionId == newCar.RegionId && car.LicensePlate == newCar.LicensePlate);
189+
Car? carInDatabase =
190+
await dbContext.Cars.FirstOrDefaultAsync(car => car.RegionId == newCar.RegionId && car.LicensePlate == newCar.LicensePlate);
190191

191192
carInDatabase.ShouldNotBeNull();
192193
carInDatabase.Id.Should().Be($"{newCar.RegionId}:{newCar.LicensePlate}");
@@ -508,7 +509,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
508509

509510
await _testContext.RunOnDatabaseAsync(async dbContext =>
510511
{
511-
Car carInDatabase =
512+
Car? carInDatabase =
512513
await dbContext.Cars.FirstOrDefaultAsync(car => car.RegionId == existingCar.RegionId && car.LicensePlate == existingCar.LicensePlate);
513514

514515
carInDatabase.Should().BeNull();

test/JsonApiDotNetCoreTests/UnitTests/Links/LinkInclusionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ public override string GetUriByAddress<TAddress>(HttpContext httpContext, TAddre
412412
return "https://domain.com/some/path";
413413
}
414414

415-
public override string GetUriByAddress<TAddress>(TAddress address, RouteValueDictionary values, string scheme, HostString host,
415+
public override string GetUriByAddress<TAddress>(TAddress address, RouteValueDictionary values, string? scheme, HostString host,
416416
PathString pathBase = new(), FragmentString fragment = new(), LinkOptions? options = null)
417417
{
418418
throw new NotImplementedException();

test/TestBuildingBlocks/DbContextExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ private static async Task ClearTablesAsync(this DbContext dbContext, params Type
3838
{
3939
foreach (Type model in models)
4040
{
41-
IEntityType entityType = dbContext.Model.FindEntityType(model);
41+
IEntityType? entityType = dbContext.Model.FindEntityType(model);
4242

4343
if (entityType == null)
4444
{
4545
throw new InvalidOperationException($"Table for '{model.Name}' not found.");
4646
}
4747

48-
string tableName = entityType.GetTableName();
48+
string tableName = entityType.GetTableName()!;
4949

5050
// PERF: We first try to clear the table, which is fast and usually succeeds, unless foreign key constraints are violated.
5151
// In that case, we recursively delete all related data, which is slow.

0 commit comments

Comments
 (0)