I’m using Bean Validation for Spring components annotated with @ConfigurationProperties in this way:
@Validated
@ConfigurationProperties("prefix")
public class Props {
@javax.validation.constraints.NotEmpty
public String string;
}
Then I create a Spring configuration where use this properties class:
@Configuration
@EnableConfigurationProperties(Props.class)
public class SomeConfiguration {
@Bean
public SomeService someService(Props props) { // <-- Sonar tells that the parameter should be annotated with @Valid
return new SomeService(props);
}
}
For SomeConfiguration.someService() method’s parameter props Sonar raises java:S5128 issue, telling that the parameter must be annotated with @Valid. However, it’s not required in Spring and it will be quite annoying to add @Valid in every method that uses Props type as a method parameter.
Unfortunately, I can’t exclude the rule for all method parameters, but keep it working for fields like private @Valid Props props or private List<@Valid Props> propsList.
The suggestion is to allow us to disable this rule for method parameters separately from fields. Basically, to allow to disable it for parameters but leave enabled for fields/getters.