Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(controller): do not propagate NonNullApi to other packages #3235

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,13 @@ public String checkValueForAnnotatedElement(Object value,
}
if (annotatedElement instanceof Method) {
return checkValueForType(value,
((Method) annotatedElement).getGenericReturnType(),
requiredByContext);
((Method) annotatedElement).getGenericReturnType());
}
return null;
}

String checkValueForType(Object value, Type expectedType,
boolean requiredByContext) {
return new ExplicitNullableTypeCheckerHelper(requiredByContext)
.checkValueForType(value, expectedType);
String checkValueForType(Object value, Type expectedType) {
return new ExplicitNullableTypeCheckerHelper().checkValueForType(value,
expectedType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.vaadin.hilla.parser.utils.Generics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNullApi;

import static com.vaadin.hilla.ExplicitNullableTypeChecker.isRequired;

Expand All @@ -49,22 +50,10 @@ class ExplicitNullableTypeCheckerHelper {
// A map for tracking already visited Beans.
private Map<Type, Set<Object>> visitedBeans;

private boolean requiredByContext;

private static Logger getLogger() {
return LoggerFactory.getLogger(EndpointController.class);
}

/**
* Creates a new helper.
*
* @param requiredByContext
* {@code true} if the context defines that the node is required
*/
public ExplicitNullableTypeCheckerHelper(boolean requiredByContext) {
this.requiredByContext = requiredByContext;
}

/**
* Check if the Bean value and type have been visited.
*/
Expand Down Expand Up @@ -208,10 +197,14 @@ private String checkBeanFields(Object value, Type expectedType) {
}
markAsVisited(value, expectedType);
Class<?> clazz = (Class<?>) expectedType;
// the context is the package where the object is defined
boolean requiredByContext = clazz.getPackage() != null
&& clazz.getPackage().isAnnotationPresent(NonNullApi.class);
try {
for (PropertyDescriptor propertyDescriptor : Introspector
.getBeanInfo(clazz).getPropertyDescriptors()) {
if (!isPropertySubjectForChecking(propertyDescriptor)) {
if (!isPropertySubjectForChecking(propertyDescriptor,
requiredByContext)) {
continue;
}

Expand Down Expand Up @@ -239,7 +232,7 @@ private String checkBeanFields(Object value, Type expectedType) {
}

private boolean isPropertySubjectForChecking(
PropertyDescriptor propertyDescriptor) {
PropertyDescriptor propertyDescriptor, boolean requiredByContext) {
try {
String name = propertyDescriptor.getName();
Method readMethod = propertyDescriptor.getReadMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.cglib.proxy.Enhancer;
Expand Down Expand Up @@ -1128,8 +1127,8 @@ public void should_Invoke_ExplicitNullableTypeChecker()
ExplicitNullableTypeChecker.class);

when(explicitNullableTypeChecker.checkValueForType(
eq(NullCheckerTestClass.OK_RESPONSE), eq(String.class),
eq(false))).thenReturn(null);
eq(NullCheckerTestClass.OK_RESPONSE), eq(String.class)))
.thenReturn(null);

String testOkMethod = "testOkMethod";
ResponseEntity<String> response = createVaadinController(
Expand Down Expand Up @@ -1445,8 +1444,8 @@ private <T> EndpointController createVaadinController(T endpoint,
if (explicitNullableTypeChecker == null) {
explicitNullableTypeChecker = mock(
ExplicitNullableTypeChecker.class);
when(explicitNullableTypeChecker.checkValueForType(any(), any(),
ArgumentMatchers.anyBoolean())).thenReturn(null);
when(explicitNullableTypeChecker.checkValueForType(any(), any()))
.thenReturn(null);
}

ApplicationContext mockApplicationContext = mockApplicationContext(
Expand Down
Loading