[#JERSEY-1911] bean validation doesn`t work when constraints

[JERSEY-1911] bean validation doesn't work when constraints placed on
getters Created: 05/Jun/13 Updated: 10/Sep/15 Resolved: 14/Jun/13
Status:
Project:
Component/s:
Affects
Version/s:
Fix Version/s:
Closed
jersey
extensions
2.0, 2.1
Type:
Reporter:
Resolution:
Labels:
Remaining
Estimate:
Time Spent:
Original
Estimate:
Bug
Łukasz Wiktor
Fixed
None
0 minutes
Tags:
jersey-bean-validation
2.0.1, 2.1
Priority:
Assignee:
Votes:
Major
Michal Gajdos
0
4 hours
3 hours
Description
Normally, there are 2 ways to declare validation constraints of a bean property: directly on a
field or on a getter.
The second way doesn't work with jersey-bean-validation when you pass a bean as an argument
to a method.
How to reproduce:
1. Take the bean-validation-webapp example project
2. Run ContactCardTest - all tests passes
3. Go to ContactCard and move all validation annotations from fields to appropriate getters, for
example:
4. Run ContactCardTest again - testAddInvalidContact fails
Comments
Comment by Łukasz Wiktor [ 05/Jun/13 ]
Here you have the modified version of the ContactCard class (with constraint annotations
moved to getters):
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.jersey.examples.beanvalidation.webapp.domain;
import
import
import
import
javax.validation.constraints.DecimalMin;
javax.validation.constraints.NotNull;
javax.validation.constraints.Pattern;
javax.xml.bind.annotation.XmlRootElement;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
/**
* @author Michal Gajdos (michal.gajdos at oracle.com)
*/
@XmlRootElement
public class ContactCard {
private Long id;
private String fullName;
private String email;
private String phone;
@DecimalMin(value = "1")
public Long getId() {
return id;
}
public void setId(final Long id) {
this.id = id;
}
@NotNull(message = "{contact.wrong.name}")
@Length(min = 2, max = 20)
public String getFullName() {
return fullName;
}
public void setFullName(final String fullName) {
this.fullName = fullName;
}
@Email(message = "{contact.wrong.email}", regexp = "[a-zA-Z0-9._%-]+@[a-zAZ0-9.-]+\\.[a-zA-Z]{2,4}")
public String getEmail() {
return email;
}
public void setEmail(final String email) {
this.email = email;
}
@Pattern(message = "{contact.wrong.phone}", regexp = "[0-9]{3,9}")
public String getPhone() {
return phone;
}
public void setPhone(final String phone) {
this.phone = phone;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ContactCard)) {
return false;
}
final ContactCard that = (ContactCard) o;
if (email != null ? !email.equals(that.email) : that.email != null) {
return false;
}
if (fullName != null ? !fullName.equals(that.fullName) : that.fullName !=
null) {
return false;
}
if (phone != null ? !phone.equals(that.phone) : that.phone != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = fullName != null ? fullName.hashCode() : 0;
result = 31 * result + (email != null ? email.hashCode() : 0);
result = 31 * result + (phone != null ? phone.hashCode() : 0);
return result;
}
}
To confirm that validation should work when constraints are defined on getters you can run
following test:
@Test
public void testInvalidContactDirectValidation() throws Exception {
final ContactCard entity = new ContactCard();
entity.setPhone("Crrrn");
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<ContactCard>> constraintViolations =
validator.validate(entity);
Set<String> messageTemplates = new HashSet<String>();
for (ConstraintViolation<ContactCard> cv : constraintViolations) {
messageTemplates.add(cv.getMessageTemplate());
}
assertEquals(2, constraintViolations.size());
assertTrue(messageTemplates.contains("{contact.wrong.name}"));
assertTrue(messageTemplates.contains("{contact.wrong.phone}"));
}
It has data and asserts the same as in the testAddInvalidContact. The only difference is that in
this case you run the validation directly.
Comment by Łukasz Wiktor [ 17/Jun/13 ]
Good job Michal! Nice to see it resolved so quickly. Thanks a lot.
Generated at Tue Feb 09 17:32:43 UTC 2016 using JIRA 6.2.3#6260sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.