Skip to content

NullnessMethodInvocationValidator should consider jakarta.annotation.Nullable annotations #3316

Closed as not planned
@fjakop

Description

@fjakop

We switched to spring-data 3.5.1 recently and encountered errors on formerly (spring-data 3.4.4) working code

java.lang.NullPointerException: Return value is null but must not be null
	at org.springframework.data.util.NullnessMethodInvocationValidator.returnValueIsNull(NullnessMethodInvocationValidator.java:124)
...

The method in question is annotated with jakarta.annotation.Nullable, so this should not happen imho.
It's easily reproducible with this test code

package org.springframework.data.util;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.core.ParameterNameDiscoverer;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class MethodNullnessTest {

    private final ParameterNameDiscoverer discoverer = new ParameterNameDiscoverer() {
        @Override
        public String[] getParameterNames(Method method) {
            return null;
        }

        @Override
        public String[] getParameterNames(Constructor<?> ctor) {
            return null;
        }
    };

    @Test
    void isNullableReturn() throws NoSuchMethodException {

        assertFalse(NullnessMethodInvocationValidator.MethodNullness
                .of(Dummy.class.getDeclaredMethod("getNonnullData"), discoverer)
                .isNullableReturn());

        assertTrue(NullnessMethodInvocationValidator.MethodNullness
                .of(Dummy.class.getDeclaredMethod("getNullableData"), discoverer)
                .isNullableReturn());

    }

    static class Dummy {

        @Nonnull
        String getNonnullData() {
            return "";
        }

        @Nullable
        String getNullableData() {
            return "";
        }
    }
}

where the assertion for nullable = true always fails.

Metadata

Metadata

Assignees

Labels

status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions