glassfish
  1. glassfish
  2. GLASSFISH-21048

Field-level validators do not work on beans that have been @Inject'ed

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 4.0
    • Fix Version/s: None
    • Component/s: bean-validator, cdi
    • Labels:
      None
    • Environment:

      Windows 7, GlassFish Server Open Source Edition 4.0 (build 89), JDK 1.8.0_05, NetBeans 8.0

      Description

      Validation does not work on beans that have been injected using @Inject.

      When validating two identical beans with identical state, this is the expected output:

      Injected:
      []
      Not Injected:
      []
      

      But this is the actual output:

      Injected:
      [ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=name, rootBeanClass=class eetestlite.Simple$Proxy$_$$_WeldClientProxy, messageTemplate='{javax.validation.constraints.NotNull.message}'}]
      Not Injected:
      []
      

      Code:

      eetestlite/Welcome.java
      package eetestlite;
      
      import java.io.IOException;
      import java.io.PrintWriter;
      import javax.inject.Inject;
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import javax.validation.Validator;
      
      @WebServlet(name = "Welcome", urlPatterns = {"/Welcome"})
      public class Welcome extends HttpServlet {
      
          @Inject
          Simple injected;
          
          @Inject
          Validator validator;
          
          protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
              response.setContentType("text/plain");
              Simple notInjected = new Simple();
              try (PrintWriter out = response.getWriter()) {
                  injected.setName("Hello");
                  notInjected.setName("Hello");
                  out.println("Injected:");
                  out.println(validator.validate(injected));
                  out.println("Not Injected:");
                  out.println(validator.validate(notInjected));
              }
          }
      
          @Override
          protected void doGet(HttpServletRequest request, HttpServletResponse response)
                  throws ServletException, IOException {
              processRequest(request, response);
          }
      }
      
      eetestlite/Simple.java
      package eetestlite;
      
      import java.io.Serializable;
      import javax.enterprise.context.SessionScoped;
      import javax.validation.constraints.NotNull;
      
      @SessionScoped
      public class Simple implements Serializable {
          
          @NotNull
          private String name;
          
          public String getName() {
              return name;
          }
          
          public void setName(String name) {
              this.name = name;
          }
          
      }
      
      beans.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
             bean-discovery-mode="annotated">
      </beans>
      

      The code does work if I move the validation property from the private field to getName().

        Activity

        Hide
        jjsnyder83 added a comment -

        This appears to be an issue with the Weld proxy that is generated for the injected field. I have an email out to JBoss to discuss the problem.

        Show
        jjsnyder83 added a comment - This appears to be an issue with the Weld proxy that is generated for the injected field. I have an email out to JBoss to discuss the problem.
        Hide
        Martin Kouba added a comment -

        FYI I've submitted a topic on the Bean Validation forum: https://forum.hibernate.org/viewtopic.php?f=26&t=1039523

        Show
        Martin Kouba added a comment - FYI I've submitted a topic on the Bean Validation forum: https://forum.hibernate.org/viewtopic.php?f=26&t=1039523

          People

          • Assignee:
            Joe Di Pol
            Reporter:
            benjamin.johnston
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: