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

CCDM: check for null in returned Java Bean from Connect service methods #6891

Merged
merged 1 commit into from
Nov 12, 2019

Conversation

platosha
Copy link
Contributor

@platosha platosha commented Nov 8, 2019

Connected to #6717


This change is Reviewable

@platosha platosha added the hilla Issues related to Hilla label Nov 8, 2019
@project-bot project-bot bot added this to External Reviews in OLD Vaadin Flow ongoing work (Vaadin 10+) Nov 8, 2019
@ujoni ujoni added the +0.0.1 label Nov 8, 2019
Copy link
Contributor

@qtdzz qtdzz left a 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

OLD Vaadin Flow ongoing work (Vaadin 10+) automation moved this from External Reviews to Changes Requested Nov 11, 2019
@platosha platosha force-pushed the ap/ccdm/explicit-nullables-beans branch from 8f5afcc to 3e3429a Compare November 11, 2019 16:49
Copy link
Contributor Author

@platosha platosha left a 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):

Previously, vaadin-bot (Vaadin Bot) wrote…

CRITICAL Either log or rethrow this exception. rule

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…

CRITICAL Either log or rethrow this exception. rule
MINOR Combine this catch with the one at line 130, which has the same body. rule

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…

CRITICAL Either log or rethrow this exception. rule
MINOR Combine this catch with the one at line 130, which has the same body. rule

Done.

Copy link
Contributor

@qtdzz qtdzz left a 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)

@platosha platosha force-pushed the ap/ccdm/explicit-nullables-beans branch from 3e3429a to 3f29a28 Compare November 12, 2019 09:27
@@ -128,4 +146,63 @@ private String checkIterable(Iterable value, Type expectedType) {

return null;
}

private boolean isPropertySubjectForChecking(Class<?> clazz,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Remove this unused method parameter "clazz". rule

@vaadin-bot
Copy link
Collaborator

SonarQube analysis reported 1 issue

  • MAJOR 1 major

Watch the comments in this conversation to review them.

Copy link
Contributor

@qtdzz qtdzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

ps: the major sonarqube issue will be fixed in the incoming follow up PR

Reviewed 2 of 2 files at r3.
Reviewable status: 8 unresolved discussions, 1 of 1 LGTMs obtained (waiting on @platosha, @qtdzz, and @vaadin-bot)

Copy link
Contributor

@qtdzz qtdzz left a 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: :shipit: complete! all discussions resolved, 1 of 1 LGTMs obtained

OLD Vaadin Flow ongoing work (Vaadin 10+) automation moved this from Changes Requested to Reviewer approved Nov 12, 2019
@qtdzz qtdzz merged commit c03e79a into ccdm Nov 12, 2019
OLD Vaadin Flow ongoing work (Vaadin 10+) automation moved this from Reviewer approved to Done - pending release Nov 12, 2019
@qtdzz qtdzz deleted the ap/ccdm/explicit-nullables-beans branch November 12, 2019 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hilla Issues related to Hilla +0.0.1
Projects
OLD Vaadin Flow ongoing work (Vaadin ...
  
Done - pending release
Development

Successfully merging this pull request may close these issues.

None yet

4 participants