Description
openedon Jul 24, 2024
Is your feature request related to a problem? Please describe the problem.
Check List to #2649
Source CA1062
Cause
An externally visible method dereferences one of its reference arguments without verifying whether that argument is null (Nothing in Visual Basic).
You can configure this rule to exclude certain types and parameters from analysis. You can also indicate null-check validation methods.
Rule description
All reference arguments that are passed to externally visible methods should be checked against null. If appropriate, throw an ArgumentNullException when the argument is null.
If a method can be called from an unknown assembly because it is declared public or protected, you should validate all parameters of the method. If the method is designed to be called only by known assemblies, mark the method internal and apply the InternalsVisibleToAttribute attribute to the assembly that contains the method.
Describe the solution you'd like
I suggest covering this with additional tests.
Check every reference argument in the public API ArgumentNullException.ThrowIfNull(source)
.
Using the example of Aspire.Hosting.Redis
RedisBuilderExtensions.cs
public static IResourceBuilder<RedisResource> AddRedis(this IDistributedApplicationBuilder builder, string name, int? port = null)
{
ArgumentNullException.ThrowIfNull(builder);
var redis = new RedisResource(name);
return builder.AddResource(redis)
.WithEndpoint(port: port, targetPort: 6379, name: RedisResource.PrimaryEndpointName)
.WithImage(RedisContainerImageTags.Image, RedisContainerImageTags.Tag)
.WithImageRegistry(RedisContainerImageTags.Registry);
}
[Fact]
public void AddRedisContainerShouldThrowsWhenBuilderIsNull()
{
IDistributedApplicationBuilder builder = null!;
const string name = "Redis";
var action = () => builder.AddRedis(name);
Assert.Multiple(() =>
{
var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
});
}
Attached as an full example CA1062#Aspire.Hosting.Redis
Additional context
When checking, we get a NullReferenceException
Components.*
- - Adding public API test coverage for Aspire.Azure.AI.OpenAI (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Azure.Data.Tables (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Azure.Messaging.EventHubs (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Azure.Messaging.ServiceBus (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Azure.Messaging.WebPubSub (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Azure.Search.Documents (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Azure.Security.KeyVault (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Azure.Storage.Blobs (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Azure.Storage.Queues (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Confluent.Kafka (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Elastic.Clients.Elasticsearch (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.Keycloak.Authentication (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Microsoft.Azure.Cosmos (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Microsoft.Data.SqlClient (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Microsoft.EntityFrameworkCore.Cosmos (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Microsoft.EntityFrameworkCore.SqlServer (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Milvus.Client (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.MongoDB.Driver (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.MySqlConnector (assigned to @Zombach)
- - Adding public API test coverage for Aspire.NATS.Net (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.Npgsql (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Npgsql.EntityFrameworkCore.PostgreSQL (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Oracle.EntityFrameworkCore (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Pomelo.EntityFrameworkCore.MySql (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Qdrant.Client (assigned to @Zombach)
- - Adding public API test coverage for Aspire.RabbitMQ.Client (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Seq (assigned to @Zombach)
- - Adding public API test coverage for Aspire.StackExchange.Redis (assigned to @Zombach)
- - Adding public API test coverage for Aspire.StackExchange.Redis.DistributedCaching (assigned to @Zombach)
- - Adding public API test coverage for Aspire.StackExchange.Redis.OutputCaching (assigned to @Zombach)
Hosting.*
- - Adding public API test coverage for Aspire.Hosting.AWS (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Hosting.Dapr (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Hosting.Elasticsearch (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.Hosting.Garnet (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Hosting.Kafka (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.Hosting.Keycloak (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Hosting.Milvus (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.Hosting.MongoDB (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.Hosting.MySql (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.Hosting.Nats (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Hosting.NodeJs (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.Hosting.Oracle (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Hosting.PostgreSQL (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Hosting.Python (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Hosting.Qdrant (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Hosting.RabbitMQ (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.Hosting.Redis (assigned to @Zombach)
- - Adding public API test coverage for Aspire.Hosting.SqlServer (assigned to @Alirexaa)
- - Adding public API test coverage for Aspire.Hosting.Valkey (assigned to @bell-nat)
ServiceDiscovery.*
- - Adding public API test coverage for Microsoft.Extensions.ServiceDiscovery (assigned to @Zombach)
- - Adding public API test coverage for Microsoft.Extensions.ServiceDiscovery.Abstractions (assigned to @Zombach)
- - Adding public API test coverage for Microsoft.Extensions.ServiceDiscovery.Dns (assigned to @Zombach)
- - Adding public API test coverage for Microsoft.Extensions.ServiceDiscovery.Yarp (assigned to @Zombach)
Frozen*
- - Adding public API test coverage for Aspire.Hosting.AppHost
- - Adding public API test coverage for Aspire.Hosting.Orleans
- - Adding public API test coverage for Aspire.Hosting.Seq