-
Notifications
You must be signed in to change notification settings - Fork 164
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
CCDM: check for null in returned Java Bean from Connect service methods #6891
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 5 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @platosha)
flow-server/src/main/java/com/vaadin/flow/server/connect/ExplicitNullableTypeChecker.java, line 117 at r1 (raw file):
for (PropertyDescriptor propertyDescriptor : Introspector .getBeanInfo(clazz).getPropertyDescriptors()) {
The generator ignores static
, transient
, and @JsonIgnore
, so we should also ignore them here because we are not interested in checking them.
Also the @Nullable
property should be excluded from the check. I quickly added the following code before the loop:
for (Field field : clazz.getFields()) {
boolean shouldIgnore = Modifier
.isStatic(field.getModifiers())
|| Modifier.isTransient(field.getModifiers())
|| field.isAnnotationPresent(JsonIgnore.class)
|| field.isAnnotationPresent(Nullable.class);
if (!shouldIgnore) {
validProperties.add(field.getName());
}
}
flow-server/src/main/java/com/vaadin/flow/server/connect/ExplicitNullableTypeChecker.java, line 118 at r1 (raw file):
for (PropertyDescriptor propertyDescriptor : Introspector .getBeanInfo(clazz).getPropertyDescriptors()) { Method readMethod = propertyDescriptor.getReadMethod();
The readMethod
can be null if there is no getter. Should we check for the raw value in that case?
flow-server/src/main/java/com/vaadin/flow/server/connect/ExplicitNullableTypeChecker.java, line 130 at r1 (raw file):
} } } catch (IntrospectionException e) {
These exceptions can be combined in one catch clause
} catch (IntrospectionException | InvocationTargetException
| IllegalAccessException e) {
return e.toString();
}
but you also might need to log the exception to satisfy the Sonarqube
8f5afcc
to
3e3429a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 5 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @qtdzz and @vaadin-bot)
flow-server/src/main/java/com/vaadin/flow/server/connect/ExplicitNullableTypeChecker.java, line 117 at r1 (raw file):
Previously, qtdzz (Tien Nguyen) wrote…
for (PropertyDescriptor propertyDescriptor : Introspector .getBeanInfo(clazz).getPropertyDescriptors()) {
The generator ignores
static
,transient
, and@JsonIgnore
, so we should also ignore them here because we are not interested in checking them.
Also the@Nullable
property should be excluded from the check. I quickly added the following code before the loop:for (Field field : clazz.getFields()) { boolean shouldIgnore = Modifier .isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) || field.isAnnotationPresent(JsonIgnore.class) || field.isAnnotationPresent(Nullable.class); if (!shouldIgnore) { validProperties.add(field.getName()); } }
Done.
flow-server/src/main/java/com/vaadin/flow/server/connect/ExplicitNullableTypeChecker.java, line 118 at r1 (raw file):
The readMethod can be null if there is no getter. Should we check for the raw value in that case?
OK per spec it must contain a getter method to enable introspection.
flow-server/src/main/java/com/vaadin/flow/server/connect/ExplicitNullableTypeChecker.java, line 130 at r1 (raw file):
Done.
flow-server/src/main/java/com/vaadin/flow/server/connect/ExplicitNullableTypeChecker.java, line 130 at r1 (raw file):
Previously, qtdzz (Tien Nguyen) wrote…
These exceptions can be combined in one catch clause
} catch (IntrospectionException | InvocationTargetException | IllegalAccessException e) { return e.toString(); }
but you also might need to log the exception to satisfy the Sonarqube
Done.
flow-server/src/main/java/com/vaadin/flow/server/connect/ExplicitNullableTypeChecker.java, line 132 at r1 (raw file):
Previously, vaadin-bot (Vaadin Bot) wrote…
Either log or rethrow this exception.
Combine this catch with the one at line 130, which has the same body.
Done.
flow-server/src/main/java/com/vaadin/flow/server/connect/ExplicitNullableTypeChecker.java, line 134 at r1 (raw file):
Previously, vaadin-bot (Vaadin Bot) wrote…
Either log or rethrow this exception.
Combine this catch with the one at line 130, which has the same body.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 2 files at r2.
Reviewable status: 10 unresolved discussions, 0 of 1 LGTMs obtained (waiting on @platosha, @qtdzz, and @vaadin-bot)
flow-server/src/main/java/com/vaadin/flow/server/connect/ExplicitNullableTypeChecker.java, line 178 at r2 (raw file):
Quoted 16 lines of code…
Class<?> declaringClass = readMethod.getDeclaringClass(); final Set<String> declaredFieldNames = Arrays .stream(declaringClass.getDeclaredFields()) .map((Field field) -> field.getName()) .collect(Collectors.toSet()); if (!declaredFieldNames.contains(name)) { continue; } Field field = declaringClass.getDeclaredField(name); if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) || field.isAnnotationPresent(JsonIgnore.class) || field.isAnnotationPresent(Nullable.class)) { continue; }
We can extract this out as a method and combine it with the above continue
statement.
Something like this:
private boolean shouldSkip(Method readMethod, String fieldName)
throws NoSuchFieldException {
Class<?> declaringClass = readMethod.getDeclaringClass();
final Set<String> declaredFieldNames = Arrays
.stream(declaringClass.getDeclaredFields())
.map(Field::getName)
.collect(Collectors.toSet());
if (!declaredFieldNames.contains(fieldName)) {
return true;
}
Field field = declaringClass.getDeclaredField(fieldName);
return Modifier.isStatic(field.getModifiers()) ||
Modifier.isTransient(field.getModifiers()) ||
field.isAnnotationPresent(JsonIgnore.class) ||
field.isAnnotationPresent(Nullable.class);
}
flow-server/src/main/java/com/vaadin/flow/server/connect/ExplicitNullableTypeChecker.java, line 193 at r2 (raw file):
} catch (IntrospectionException | InvocationTargetException | IllegalAccessException | NoSuchFieldException e) { getLogger().error(e.getMessage());
I think we should return e.getMessage()
and log error(e)
3e3429a
to
3f29a28
Compare
@@ -128,4 +146,63 @@ private String checkIterable(Iterable value, Type expectedType) { | |||
|
|||
return null; | |||
} | |||
|
|||
private boolean isPropertySubjectForChecking(Class<?> clazz, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 2 files at r3.
Reviewable status: 8 unresolved discussions, 1 of 1 LGTMs obtained (waiting on @platosha, @qtdzz, and @vaadin-bot)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dismissed @vaadin-bot from 4 discussions.
Reviewable status:complete! all discussions resolved, 1 of 1 LGTMs obtained
Connected to #6717
This change is![Reviewable](https://camo.githubusercontent.com/23b05f5fb48215c989e92cc44cf6512512d083132bd3daf689867c8d9d386888/68747470733a2f2f72657669657761626c652e696f2f7265766965775f627574746f6e2e737667)