netceteragroup / valdr-bean-validation

Java Bean Validation plugin
MIT License
35 stars 21 forks source link

No validation in a class cause a NullPointerException #30

Closed sebastienvermeille closed 8 years ago

sebastienvermeille commented 8 years ago

In some case we extends our model with another class. If that other class does not have any validation annotation then a NullPointExceptionOccurs breaking all the servlet ... :

Avertissement: StandardWrapperValve[valdr Bean Validation Servlet]: Servlet.service() for servlet valdr Bean Validation Servlet threw exception
java.lang.NullPointerException
    at java.lang.reflect.Field.getAnnotation(Field.java:1112)
    at java.lang.reflect.AccessibleObject.isAnnotationPresent(AccessibleObject.java:189)
    at org.reflections.ReflectionUtils$4.apply(ReflectionUtils.java:173)
    at org.reflections.ReflectionUtils$4.apply(ReflectionUtils.java:171)
    at com.google.common.base.Predicates$OrPredicate.apply(Predicates.java:377)
    at com.google.common.base.Predicates$AndPredicate.apply(Predicates.java:343)
    at com.google.common.collect.Iterators$8.computeNext(Iterators.java:736)
    at com.google.common.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:143)
    at com.google.common.collect.AbstractIterator.hasNext(AbstractIterator.java:138)
    at com.google.common.collect.Sets.newHashSet(Sets.java:250)
    at com.google.common.collect.Sets.newHashSet(Sets.java:230)
    at org.reflections.ReflectionUtils.filter(ReflectionUtils.java:449)
    at org.reflections.ReflectionUtils.getFields(ReflectionUtils.java:110)
    at org.reflections.ReflectionUtils.getAllFields(ReflectionUtils.java:104)
    at com.github.valdr.AnnotatedClass.extractValidationRules(AnnotatedClass.java:44)
    at com.github.valdr.ConstraintParser.parse(ConstraintParser.java:57)
    at com.github.valdr.ValidationRulesServlet.doGet(ValidationRulesServlet.java:60)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)```
marcelstoer commented 8 years ago

Please provide a minimal test project that demonstrates this behavior or show at least your two model classes (super class & sub class).

What you describe should be covered by unit test https://github.com/netceteragroup/valdr-bean-validation/blob/master/valdr-bean-validation/src/test/java/com/github/valdr/ConstraintParserTest.java#L177 which uses the two classes in https://github.com/netceteragroup/valdr-bean-validation/tree/master/valdr-bean-validation/src/test/java/com/github/valdr/model/d

sebastienvermeille commented 8 years ago

I investigate a bit more to determine what is really not working and it seems that just a class without any validation constraints annotation produce the error.

Exemple :

package com.mypackage.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.xml.bind.annotation.XmlRootElement;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Getter;
import lombok.NonNull;

@MappedSuperclass
@XmlRootElement
public class AbstractAuditFields {

    @Getter
    @NonNull
    @Column(name = "CREATED_AT")
    @Temporal(TemporalType.TIMESTAMP)
    public Date createdAt;

    @Getter
    @NonNull
    @Column(name = "UPDATED_AT")
    @Temporal(TemporalType.TIMESTAMP)
    public Date updatedAt;  

    @Getter
    @JsonIgnore
    @Column(name = "VERSION", columnDefinition = "integer DEFAULT 0", nullable = false)
    @Version
    public int version;

    @PrePersist
    void createdAt() {
        this.createdAt = this.updatedAt = new Date();
    }

    @PreUpdate
    void updatedAt() {
        this.updatedAt = new Date();
    }

}
marcelstoer commented 8 years ago

I added a unit test with exactly this class https://github.com/netceteragroup/valdr-bean-validation/commit/45ea17d4bf269e00f500afa34490b96262dbd296 and it passes. I don't see the problem.