Skip to content

Fix S2325 FP: Method part of interface contract should be marked as static. #9825

Description

@coding-red-panda

Description

I have an interface like this:

public interface IOperationFilter
{
    void Apply(OpenApiOperation operation, OperationFilterContext context);
}

I've implemented a filter class with this operation, but because the implementation doesn't touch any instance fields, SonarCloud is telling me that this method needs to be static, which cannot be done since it's an interface implementation.

Reproducer

public class AddHeaderOperationFilter : IOperationFilter
{
    /// <inheritdoc />
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (context.ApiDescription.ActionDescriptor is ControllerActionDescriptor cad
            && (cad.MethodInfo.IsDefined(typeof(SkipHttpHeaderRequirementsAttribute), inherit: true)
                || cad.ControllerTypeInfo.IsDefined(typeof(SkipHttpHeaderRequirementsAttribute), inherit: true)))
        {
            return;
        }

        operation.Parameters ??= new List<IOpenApiParameter>();

        operation.Parameters.Add(new OpenApiParameter
        {
            Name = "OrganisationId",
            In = ParameterLocation.Header,
            Required = true,
            Schema = new OpenApiSchema
            {
                Type = JsonSchemaType.String
            },
            Description = "Organisation identifier"
        });

        operation.Parameters.Add(new OpenApiParameter
        {
            Name = "CallingSystem",
            In = ParameterLocation.Header,
            Required = true,
            Schema = new OpenApiSchema
            {
                Type = JsonSchemaType.String
            },
            Description = "Identifier of system which calls the endpoint"
        });
    }
}

Apply gets flagged to be made static.

Product and Version

SonarCube Cloud

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions