Consider individually annotated methods with non-null annotations for non-null validation [DATACMNS-1705]
Dmitry Solomakha opened DATACMNS-1705 and commented
This behavior manifests itself in spring-data-jpa:
when there is a repository with a method that is annotated with org.springframework.lang.NonNull and the method returns null value, the EmptyResultDataAccessException exception is not thrown unless the class is annotated with javax.annotation.Nonnull.
Is that intentional?
This behavior is caused by MethodInvocationValidator class.
Reproduction with spring-data-jpa:
public interface CustomerRepository extends CrudRepository<Customer, Long> {
@NonNull
Customer findByLastName(String lastName);
}
@DataJpaTest
public class CustomerRepositoryTests {
@Autowired
private CustomerRepository customers;
@Test
public void testFindByLastName()
{ assertThatThrownBy(() -> customers.findByLastName("name")) .isInstanceOf(EmptyResultDataAccessException.class)
.hasMessageMatching("Result must not be null!");
}
}
No further details from DATACMNS-1705
Mark Paluch commented
Right now, the mechanism is designed as opt-in on package level. Once non-null is enabled on the package level, you can declare individual methods/method arguments as nullable where required.
From the code above, it seems that you want to enable non-null validation for individual methods. Introducing non-nullability in small steps makes sense to avoid touching the entire codebase.