Skip to content

Check List: Validate arguments of public methods #5047

Open

Description

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.*

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    area-app-modelIssues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplicationuntriagedNew issue has not been triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions