spring-data-commons icon indicating copy to clipboard operation
spring-data-commons copied to clipboard

Consider individually annotated methods with non-null annotations for non-null validation [DATACMNS-1705]

Open spring-projects-issues opened this issue 5 years ago • 1 comments

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

spring-projects-issues avatar Apr 16 '20 22:04 spring-projects-issues

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.

spring-projects-issues avatar Sep 22 '20 13:09 spring-projects-issues