Java: S5128 reports missing @Valid despite validation on interface

SonarCloud reports missing @Valid annotations on both a controller implementation’s request parameter and a converter’s parameter, although validation is already configured at the HTTP boundary.

The following is a simplified example of our setup, using Spring Boot 4 and Jakarta Validation:

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.service.annotation.HttpExchange;

interface ManualChecksApi {
    @HttpExchange(method = "POST", url = "/manual-checks")
    void create(@RequestBody @Valid CreateRequest body);
}

record CreateRequest(@NotNull @Valid Definition definition) {}

record Definition(@NotBlank String title) {}

@RestController
class ManualChecksController implements ManualChecksApi {
    private final DefinitionConverter converter;

    ManualChecksController(DefinitionConverter converter) {
        this.converter = converter;
    }

    @Override
    public void create(CreateRequest body) {
        // S5128: Add missing "@Valid" on "body".
        converter.convert(body.definition());
    }
}

@Component
class DefinitionConverter {
    public String convert(Definition definition) {
        // S5128: Add missing "@Valid" on "definition".
        return definition.title();
    }
}

Expected behavior

The controller parameter should be recognized as validated: Spring MVC takes the @Valid annotation declared on the implemented interface into account. Validation then cascades into definition through CreateRequest.definition.

The converter receives an already validated object and is not configured as a separate method-validation boundary. Requiring @Valid on every subsequent method parameter appears unnecessarily broad.

Observed behavior

S5128 reports missing @Valid on both parameters. In our application, an existing MockMvc test confirms that an invalid nested definition returns HTTP 400 before the application service is invoked.

Could S5128 account for interface parameter annotations and avoid requiring repeated validation annotations on downstream conversion methods?

The sample above is simplified from our application; it has not been tested as a standalone reproducer.

Hello @sebastianhaeni,

Thank you for your feedback and reproducers. Let me clarify: here you’re talking about two different types of issues.

  • The first one related to inheritance:
    @Override
    public void create(CreateRequest body) {
        // S5128: Add missing "@Valid" on "body".
        converter.convert(body.definition());
    }

definitely should be fixed, and fixing it is not a big deal.

  • The second one related to the usage context of the method:
@Component
class DefinitionConverter {
    public String convert(Definition definition) {
        // S5128: Add missing "@Valid" on "definition".
        return definition.title();
    }
}

This one requires symbolic execution mechanisms (to analyze all the places where the converter is used) which current rule can’t provide. And we can’t guarantee it at all, to be honest.

I’ve created JIRA ticket to fix the first case.