aspire icon indicating copy to clipboard operation
aspire copied to clipboard

Check List: Validate arguments of public methods

Open Zombach opened this issue 1 year ago • 27 comments

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 image

Components.*

Hosting.*

ServiceDiscovery.*

Zombach avatar Jul 24 '24 08:07 Zombach

@davidfowl @eerhardt Helloy. If you don't mind, could you familiarize yourself with this task?

Zombach avatar Jul 24 '24 09:07 Zombach

It seems to duplicate of #2649

Alirexaa avatar Jul 24 '24 12:07 Alirexaa

It seems to duplicate of #2649

Really. Then you need to understand whether this solution option is suitable and make adjustments if necessary.

Zombach avatar Jul 24 '24 12:07 Zombach

@DamianEdwards Good afternoon. Because you are the original creator of this issue #2649. Could you please rate this topic? I would like to resolve this issue and track progress status using a checklist.

But this requires a go-ahead, And also successful Merge CA1062#Aspire.Hosting.Redis To have a working example taking into account the necessary edits

Zombach avatar Jul 26 '24 08:07 Zombach

I will work on Aspire.Hosting.Elasticsearch.

Alirexaa avatar Jul 29 '24 21:07 Alirexaa

I will work on Aspire.Hosting.Nats

Zombach avatar Jul 31 '24 19:07 Zombach

I will work on Aspire.Hosting.Keycloak

Zombach avatar Aug 01 '24 21:08 Zombach

I will work on Aspire.Hosting.Milvus

Alirexaa avatar Aug 01 '24 21:08 Alirexaa

I will work on Aspire.Hosting.PosgreSQL

Zombach avatar Aug 02 '24 06:08 Zombach

I will work on Aspire.Hosting.RabbitMQ

Alirexaa avatar Aug 02 '24 12:08 Alirexaa

I will work on Aspire.Hosting.MySql

Alirexaa avatar Aug 02 '24 12:08 Alirexaa

I will work on Adding public API test coverage for Aspire.Hosting.Kafka

Alirexaa avatar Aug 02 '24 16:08 Alirexaa

I will work on Adding public API test coverage for Aspire.Hosting.Garnet

Zombach avatar Aug 02 '24 16:08 Zombach

I will work in Aspire.Hosting.NodeJs

Alirexaa avatar Aug 02 '24 16:08 Alirexaa

I will work in Microsoft.Extensions.ServiceDiscovery.Yarp

Zombach avatar Aug 04 '24 11:08 Zombach

Add groups Components.* and ServiceDiscovery.*

Zombach avatar Aug 04 '24 11:08 Zombach

Add groups Components.* and ServiceDiscovery.*

hmm, a lot of work :) Thanks.

Alirexaa avatar Aug 04 '24 12:08 Alirexaa

I will work on Aspire.Elastic.Clients.Elasticsearch

Alirexaa avatar Aug 04 '24 17:08 Alirexaa

I will work on Aspire.Milvus.Client

Alirexaa avatar Aug 04 '24 17:08 Alirexaa

I will work on Aspire.MongoDB.Driver

Alirexaa avatar Aug 04 '24 17:08 Alirexaa

I will work on Aspire.Hosting.MongoDB

Alirexaa avatar Aug 06 '24 19:08 Alirexaa

I will work on Microsoft.Extensions.ServiceDiscovery.Dns

Zombach avatar Aug 08 '24 06:08 Zombach

I will work in Aspire.NATS.Net

Alirexaa avatar Aug 09 '24 13:08 Alirexaa

I will work on Aspire.Hosting.SqlServer

Alirexaa avatar Aug 09 '24 13:08 Alirexaa

@Alirexaa @Zombach Could you make these in batches, it might help reduce the noise and we are on agreement with how these should be done so the back-and-forth should be minimal. Thanks. Mayube one for clients and one for hosting, and if one of you owns it in the fork then they could invite the other so they can work on the same branch if necessary (I can already as a contributor of this repos). Then when I merge I will keep both names as contributors for internet points.

sebastienros avatar Aug 09 '24 22:08 sebastienros

@Alirexaa @Zombach Could you make these in batches, it might help reduce the noise and we are on agreement with how these should be done so the back-and-forth should be minimal. Thanks. Mayube one for clients and one for hosting, and if one of you owns it in the fork then they could invite the other so they can work on the same branch if necessary (I can already as a contributor of this repos). Then when I merge I will keep both names as contributors for internet points.

Yes, okay. We will do this in blocks.

Zombach avatar Aug 09 '24 22:08 Zombach

I'm working with @Alirexaa on #5250 Microsoft.Extensions.ServiceDiscovery.Abstractions Microsoft.Extensions.ServiceDiscovery Aspire.Keycloak.Authentication Aspire.Microsoft.Azure.Cosmos Aspire.Azure.AI.OpenAI Aspire.Azure.Data.Tables Aspire.Hosting.AWS Aspire.Confluent.Kafka Aspire.Azure.Search.Documents Aspire.Azure.Security.KeyVault Aspire.Azure.Storage.Queues Aspire.Hosting.Dapr

Zombach avatar Aug 09 '24 23:08 Zombach

I'm working on https://github.com/dotnet/aspire/pull/5402 Aspire.Azure.Messaging.EventHubs Aspire.Azure.Messaging.ServiceBus Aspire.Azure.Messaging.WebPubSub Aspire.Azure.Storage.Blobs Aspire.Microsoft.Data.SqlClient Aspire.Microsoft.EntityFrameworkCore.Cosmos Aspire.Microsoft.EntityFrameworkCore.SqlServer Aspire.MySqlConnector Aspire.Npgsql Aspire.Npgsql.EntityFrameworkCore.PostgreSQL Aspire.Oracle.EntityFrameworkCore

Zombach avatar Aug 23 '24 17:08 Zombach

I'm working on https://github.com/dotnet/aspire/pull/5407 Aspire.Pomelo.EntityFrameworkCore.MySql Aspire.Qdrant.Client Aspire.RabbitMQ.Client Aspire.Seq Aspire.StackExchange.Redis Aspire.StackExchange.Redis.DistributedCaching Aspire.StackExchange.Redis.OutputCaching

Zombach avatar Aug 23 '24 21:08 Zombach

These projects are frozen for now, since they have not yet moved the test projects into a separate project.

Aspire.Hosting.AppHost Aspire.Hosting.Orleans Aspire.Hosting.Seq

Zombach avatar Aug 25 '24 07:08 Zombach