java:S3553 should not trigger for @Autowired methods from Spring

For the Java rule that raises issues when java.util.Optional is used with method parameters, there is a false-positive (tested with SonarQube 9 LTS) if it is used on a method that is used with Autowiring from Spring.

Something similar to this seems to have been brought up already in java:S3553 does not consider spring mvc optional parameters but this does at least not seem to consider the following case:

  @Autowired
  public void initStuff( Optional<List<SomeInterface>> injectedIfAvailable, SomeService service )
  {
    //
  }

In this case a proper implementation of SomeInterface may not have been configured by the time this method is called. Using java.util.Optional to avoid failing in this case is a valid use case, as discussed by Spring developers in Allow autowiring of empty collection in @Bean method parameters using java.util.Optional [SPR-9132] · Issue #13771 · spring-projects/spring-framework · GitHub, so Sonar should not raise an issue when Optional is used as parameter in Autowired methods. Maybe this could be added in the “Spring filter” that was mentioned in the ticket from the other thread.

Hi there, I think this makes sense. I created a ticket to fix it.

Thanks, and have a nice day!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.