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

HV-1831: Optimize cascading validation for large lists #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.hibernate.validator.bugs;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

public class FirstChildBean {
@NotBlank
private String name;

// 3 to 4 further fields with simple validators

@Valid
private List<SecondChildBean> secondChildren;

public FirstChildBean(String name, List<SecondChildBean> secondChildren) {
this.name = name;
this.secondChildren = secondChildren;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FirstChildBean that = (FirstChildBean) o;
return Objects.equals(name, that.name) &&
Objects.equals(secondChildren, that.secondChildren);
}

@Override
public int hashCode() {
return Objects.hash(name, secondChildren);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.hibernate.validator.bugs;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.List;

public class ParentAnnotatedBean {

@NotBlank
@Size
private String name;

@Valid
private List<FirstChildBean> firstChildren;

protected ParentAnnotatedBean() {}

public ParentAnnotatedBean(String name, List<FirstChildBean> firstChildren) {
this.name = name;
this.firstChildren = firstChildren;
}

public List<FirstChildBean> getFirstChildren() { return firstChildren; }

public void setFirstChildren(List<FirstChildBean> firstChildren) { this.firstChildren = firstChildren; }

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.hibernate.validator.bugs;

import javax.validation.constraints.NotBlank;
import java.util.Objects;

public class SecondChildBean {

@NotBlank
private String foo;

@NotBlank
private String bar;

public SecondChildBean(String foo, String bar) {
this.foo = foo;
this.bar = bar;
}

public String getFoo() {
return foo;
}

public void setFoo(String foo) {
this.foo = foo;
}

public String getBar() {
return bar;
}

public void setBar(String bar) {
this.bar = bar;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SecondChildBean that = (SecondChildBean) o;
return Objects.equals(foo, that.foo) &&
Objects.equals(bar, that.bar);
}

@Override
public int hashCode() {
return Objects.hash(foo, bar);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
Expand All @@ -24,15 +26,34 @@ public static void setUp() {
}

@Test
@TestForIssue(jiraKey = "HV-NNNNN") // Please fill in the JIRA key of your issue
@TestForIssue(jiraKey = "HV-1831") // Please fill in the JIRA key of your issue
public void testYourBug() {
YourAnnotatedBean yourEntity1 = new YourAnnotatedBean( null, "example" );
List<FirstChildBean> firstChildren = createFirstChildren();

Set<ConstraintViolation<YourAnnotatedBean>> constraintViolations = validator.validate( yourEntity1 );
assertEquals( 1, constraintViolations.size() );
assertEquals(
"must not be null",
constraintViolations.iterator().next().getMessage() );
ParentAnnotatedBean yourEntity1 = new ParentAnnotatedBean( "parent-bean", firstChildren );

Set<ConstraintViolation<ParentAnnotatedBean>> constraintViolations = validator.validate( yourEntity1 );
assertEquals( 0, constraintViolations.size() );
}

private List<FirstChildBean> createFirstChildren() {
List<FirstChildBean> firstChildren = new ArrayList<>();

for(int i = 0; i < 50; i++) {
firstChildren.add(new FirstChildBean("foo" + i, createSecondChildren()));
}

return firstChildren;
}

private List<SecondChildBean> createSecondChildren() {
List<SecondChildBean> secondChildren = new ArrayList<>();

for(int i = 0; i < 10; i++) {
secondChildren.add(new SecondChildBean("foo" + i, "bar" + i));
}

return secondChildren;
}

}