From d568a714154b62972ec34584a8f4cb036de4e426 Mon Sep 17 00:00:00 2001 From: Bart Koelman <10324372+bkoelman@users.noreply.github.com> Date: Thu, 10 Oct 2024 11:59:06 +0200 Subject: [PATCH] Backport fixes for new analyzers in .NET 9 (#1618) --- .../QueryString/QueryStringParserBenchmarks.cs | 2 +- .../DapperExample/Repositories/DapperRepository.cs | 2 +- src/JsonApiDotNetCore/Middleware/TraceLogWriter.cs | 2 +- src/JsonApiDotNetCore/Queries/QueryLayerComposer.cs | 2 +- .../ReadWrite/Creating/CreateResourceTests.cs | 6 +++--- .../CreateResourceWithClientGeneratedIdTests.cs | 12 ++++++------ .../Updating/Resources/UpdateResourceTests.cs | 4 ++-- .../ResourceInheritance/ResourceTypeCaptureStore.cs | 6 +++--- .../CreateSortExpressionFromLambdaTests.cs | 6 +++--- .../ResourceGraph/ResourceGraphBuilderTests.cs | 2 +- test/UnitTests/Graph/Implementation.cs | 3 +++ test/UnitTests/Graph/TypeLocatorTests.cs | 8 ++++---- 12 files changed, 29 insertions(+), 26 deletions(-) diff --git a/benchmarks/QueryString/QueryStringParserBenchmarks.cs b/benchmarks/QueryString/QueryStringParserBenchmarks.cs index 548c08d532..5e5a65ed9f 100644 --- a/benchmarks/QueryString/QueryStringParserBenchmarks.cs +++ b/benchmarks/QueryString/QueryStringParserBenchmarks.cs @@ -31,7 +31,7 @@ public QueryStringParserBenchmarks() var request = new JsonApiRequest { - PrimaryResourceType = resourceGraph.GetResourceType(typeof(QueryableResource)), + PrimaryResourceType = resourceGraph.GetResourceType(), IsCollection = true }; diff --git a/src/Examples/DapperExample/Repositories/DapperRepository.cs b/src/Examples/DapperExample/Repositories/DapperRepository.cs index 23e9806139..1da38697d9 100644 --- a/src/Examples/DapperExample/Repositories/DapperRepository.cs +++ b/src/Examples/DapperExample/Repositories/DapperRepository.cs @@ -365,7 +365,7 @@ public async Task DeleteAsync(TResource? resourceFromDatabase, [DisallowNull] TI await _resourceDefinitionAccessor.OnWritingAsync(placeholderResource, WriteOperationKind.DeleteResource, cancellationToken); var deleteBuilder = new DeleteResourceStatementBuilder(_dataModelService); - DeleteNode deleteNode = deleteBuilder.Build(ResourceType, placeholderResource.Id!); + DeleteNode deleteNode = deleteBuilder.Build(ResourceType, placeholderResource.Id); CommandDefinition sqlCommand = _dapperFacade.GetSqlCommand(deleteNode, cancellationToken); await ExecuteInTransactionAsync(async transaction => diff --git a/src/JsonApiDotNetCore/Middleware/TraceLogWriter.cs b/src/JsonApiDotNetCore/Middleware/TraceLogWriter.cs index b78670286e..70ed7fdd01 100644 --- a/src/JsonApiDotNetCore/Middleware/TraceLogWriter.cs +++ b/src/JsonApiDotNetCore/Middleware/TraceLogWriter.cs @@ -120,7 +120,7 @@ public override void Write(Utf8JsonWriter writer, TWrapper value, JsonSerializer internal sealed partial class TraceLogWriter(ILoggerFactory loggerFactory) : TraceLogWriter { - private readonly ILogger _logger = loggerFactory.CreateLogger(typeof(T)); + private readonly ILogger _logger = loggerFactory.CreateLogger(); public void LogMethodStart(object? parameters = null, [CallerMemberName] string memberName = "") { diff --git a/src/JsonApiDotNetCore/Queries/QueryLayerComposer.cs b/src/JsonApiDotNetCore/Queries/QueryLayerComposer.cs index d030a95462..80125cde59 100644 --- a/src/JsonApiDotNetCore/Queries/QueryLayerComposer.cs +++ b/src/JsonApiDotNetCore/Queries/QueryLayerComposer.cs @@ -390,7 +390,7 @@ public QueryLayer ComposeForUpdate([DisallowNull] TId id, ResourceType prim { ArgumentGuard.NotNull(primaryResourceType); - IImmutableSet includeElements = _targetedFields.Relationships + ImmutableHashSet includeElements = _targetedFields.Relationships .Select(relationship => new IncludeElementExpression(relationship)).ToImmutableHashSet(); AttrAttribute primaryIdAttribute = GetIdAttribute(primaryResourceType); diff --git a/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceTests.cs b/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceTests.cs index 296dff280c..1e3ce6b66f 100644 --- a/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceTests.cs +++ b/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceTests.cs @@ -114,7 +114,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => PropertyInfo? property = typeof(WorkItem).GetProperty(nameof(Identifiable.Id)); property.ShouldNotBeNull(); - property.PropertyType.Should().Be(typeof(int)); + property.PropertyType.Should().Be(); } [Fact] @@ -162,7 +162,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => PropertyInfo? property = typeof(UserAccount).GetProperty(nameof(Identifiable.Id)); property.ShouldNotBeNull(); - property.PropertyType.Should().Be(typeof(long)); + property.PropertyType.Should().Be(); } [Fact] @@ -207,7 +207,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => PropertyInfo? property = typeof(WorkItemGroup).GetProperty(nameof(Identifiable.Id)); property.ShouldNotBeNull(); - property.PropertyType.Should().Be(typeof(Guid)); + property.PropertyType.Should().Be(); } [Fact] diff --git a/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceWithClientGeneratedIdTests.cs b/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceWithClientGeneratedIdTests.cs index f6c2381474..d4cd2afa1d 100644 --- a/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceWithClientGeneratedIdTests.cs +++ b/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceWithClientGeneratedIdTests.cs @@ -80,7 +80,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => PropertyInfo? property = typeof(WorkItemGroup).GetProperty(nameof(Identifiable.Id)); property.ShouldNotBeNull(); - property.PropertyType.Should().Be(typeof(Guid)); + property.PropertyType.Should().Be(); } [Theory] @@ -134,7 +134,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => PropertyInfo? property = typeof(WorkItemGroup).GetProperty(nameof(Identifiable.Id)); property.ShouldNotBeNull(); - property.PropertyType.Should().Be(typeof(Guid)); + property.PropertyType.Should().Be(); } [Theory] @@ -185,7 +185,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => PropertyInfo? property = typeof(RgbColor).GetProperty(nameof(Identifiable.Id)); property.ShouldNotBeNull(); - property.PropertyType.Should().Be(typeof(string)); + property.PropertyType.Should().Be(); } [Theory] @@ -236,7 +236,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => PropertyInfo? property = typeof(RgbColor).GetProperty(nameof(Identifiable.Id)); property.ShouldNotBeNull(); - property.PropertyType.Should().Be(typeof(string)); + property.PropertyType.Should().Be(); } [Theory] @@ -292,7 +292,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => PropertyInfo? property = typeof(RgbColor).GetProperty(nameof(Identifiable.Id)); property.ShouldNotBeNull(); - property.PropertyType.Should().Be(typeof(string)); + property.PropertyType.Should().Be(); } [Theory] @@ -475,7 +475,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => PropertyInfo? property = typeof(RgbColor).GetProperty(nameof(Identifiable.Id)); property.ShouldNotBeNull(); - property.PropertyType.Should().Be(typeof(string)); + property.PropertyType.Should().Be(); } [Theory] diff --git a/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/UpdateResourceTests.cs b/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/UpdateResourceTests.cs index 656f2ab56b..42df980d4e 100644 --- a/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/UpdateResourceTests.cs +++ b/test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/UpdateResourceTests.cs @@ -327,7 +327,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => PropertyInfo? property = typeof(WorkItemGroup).GetProperty(nameof(Identifiable.Id)); property.ShouldNotBeNull(); - property.PropertyType.Should().Be(typeof(Guid)); + property.PropertyType.Should().Be(); } [Fact] @@ -375,7 +375,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext => PropertyInfo? property = typeof(RgbColor).GetProperty(nameof(Identifiable.Id)); property.ShouldNotBeNull(); - property.PropertyType.Should().Be(typeof(string)); + property.PropertyType.Should().Be(); } [Fact] diff --git a/test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/ResourceTypeCaptureStore.cs b/test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/ResourceTypeCaptureStore.cs index 5d7fa01d52..d3eeaedb61 100644 --- a/test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/ResourceTypeCaptureStore.cs +++ b/test/JsonApiDotNetCoreTests/IntegrationTests/ResourceInheritance/ResourceTypeCaptureStore.cs @@ -23,13 +23,13 @@ internal void Reset() internal void AssertLeftType() { - LeftDeclaredType.Should().Be(typeof(TLeft)); + LeftDeclaredType.Should().Be(); LeftReflectedTypeName.Should().Be(typeof(TLeft).Name); Request.ShouldNotBeNull(); Request.PrimaryResourceType.ShouldNotBeNull(); - Request.PrimaryResourceType.ClrType.Should().Be(typeof(TLeft)); - Request.Relationship?.LeftType.ClrType.Should().Be(typeof(TLeft)); + Request.PrimaryResourceType.ClrType.Should().Be(); + Request.Relationship?.LeftType.ClrType.Should().Be(); } internal void AssertRightTypes(params Type[] types) diff --git a/test/JsonApiDotNetCoreTests/UnitTests/ResourceDefinitions/CreateSortExpressionFromLambdaTests.cs b/test/JsonApiDotNetCoreTests/UnitTests/ResourceDefinitions/CreateSortExpressionFromLambdaTests.cs index ec78afbd0d..607073d7e2 100644 --- a/test/JsonApiDotNetCoreTests/UnitTests/ResourceDefinitions/CreateSortExpressionFromLambdaTests.cs +++ b/test/JsonApiDotNetCoreTests/UnitTests/ResourceDefinitions/CreateSortExpressionFromLambdaTests.cs @@ -365,17 +365,17 @@ private abstract class FileSystemEntry : Identifiable public FileSystemEntry Parent { get; set; } = null!; [HasMany] - public IList Children { get; set; } = new List(); + public List Children { get; set; } = []; } [UsedImplicitly(ImplicitUseTargetFlags.Members)] private sealed class DirectoryEntry : FileSystemEntry { [HasMany] - public IList Subdirectories { get; set; } = new List(); + public List Subdirectories { get; set; } = []; [HasMany] - public IList Files { get; set; } = new List(); + public List Files { get; set; } = []; } [UsedImplicitly(ImplicitUseTargetFlags.Members)] diff --git a/test/JsonApiDotNetCoreTests/UnitTests/ResourceGraph/ResourceGraphBuilderTests.cs b/test/JsonApiDotNetCoreTests/UnitTests/ResourceGraph/ResourceGraphBuilderTests.cs index e37c06bb72..de7ac84d1b 100644 --- a/test/JsonApiDotNetCoreTests/UnitTests/ResourceGraph/ResourceGraphBuilderTests.cs +++ b/test/JsonApiDotNetCoreTests/UnitTests/ResourceGraph/ResourceGraphBuilderTests.cs @@ -343,7 +343,7 @@ public void Resolves_correct_type_for_lazy_loading_proxy() ResourceType resourceType = resourceGraph.GetResourceType(proxy.GetType()); // Assert - resourceType.ClrType.Should().Be(typeof(ResourceOfInt32)); + resourceType.ClrType.Should().Be(); } [Fact] diff --git a/test/UnitTests/Graph/Implementation.cs b/test/UnitTests/Graph/Implementation.cs index e098482049..c53e559379 100644 --- a/test/UnitTests/Graph/Implementation.cs +++ b/test/UnitTests/Graph/Implementation.cs @@ -1,3 +1,6 @@ +using JetBrains.Annotations; + namespace UnitTests.Graph; +[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)] internal sealed class Implementation : IGenericInterface; diff --git a/test/UnitTests/Graph/TypeLocatorTests.cs b/test/UnitTests/Graph/TypeLocatorTests.cs index 19699b69f4..b5023e87ba 100644 --- a/test/UnitTests/Graph/TypeLocatorTests.cs +++ b/test/UnitTests/Graph/TypeLocatorTests.cs @@ -23,8 +23,8 @@ public void GetContainerRegistrationFromAssembly_Gets_Implementation() // Assert result.ShouldNotBeNull(); - result.Value.implementationType.Should().Be(typeof(Implementation)); - result.Value.serviceInterface.Should().Be(typeof(IGenericInterface)); + result.Value.implementationType.Should().Be(); + result.Value.serviceInterface.Should().Be>(); } [Fact] @@ -39,7 +39,7 @@ public void GetIdType_Correctly_Identifies_JsonApiResource() Type? idType = typeLocator.LookupIdType(type); // Assert - idType.Should().Be(typeof(int)); + idType.Should().Be(); } [Fact] @@ -71,7 +71,7 @@ public void ResolveResourceDescriptor_Returns_Type_If_Type_Is_IIdentifiable() // Assert descriptor.ShouldNotBeNull(); descriptor.ResourceClrType.Should().Be(resourceClrType); - descriptor.IdClrType.Should().Be(typeof(int)); + descriptor.IdClrType.Should().Be(); } [Fact]