Search code examples
javasonarqube

Optional.orElse(null) is marked with org.jetbrains.annotations.NotNull


How is Optional.orElse(null) marked with org.jetbrains.annotations.NotNull

The javadoc of orElse explicitly states that null is allowed

     * @param other the value to be returned, if no value is present.
     *        May be {@code null}.

But my code flags a "bug" in SonarQube.

        return users.findById(userID)
            .flatMap(user -> students.findByUser(user))
            .map(GetAllStudentsSvc::mapToStudent)
            .orElse(null);

Looking at the code it doesn't appear to be marked. Looking at the IDE it also does not appear to be marked

enter image description here

Even annotating the code as @Nullable does not help.

enter image description here


Solution

  • I think it is clear that null is perfectly fine here, so I's suggest to suppress it. This worked for me:

    @SuppressWarnings("squid:S2637")