javax validation on nested object with groups
Asked Answered
S

0

2

Here is my java code

class AbstractO{
     public interface Save{}; // for groups
     public interface Update{}; // for groups
}
class A extends AbstractO{
     public Integer id;
     @NotNull(groups={A.Save.class})
     @Valid
     public B b;
}
class B extends AbstractO{
     @NotNull(groups={B.Update.class})
     public Integer id;
     @NotNull(groups={A.Save.class})
     public Integer prop

}

// controller method

@ResponseBody
public ResponseEntity<A> save(@RequestBody @Validated(value = { A.Save.class }) A ,
            BindingResult bindingResult) {
// code goes here...
}

Issue

At time of saving A I want to have validation. Validation group is set as A.Save in controller method so B b in class A is is getting validated as notnull.

However I want to have validation of prop of class B while saving A, So I have added @Valid on B b and mentioned group A.Save on prop so that validate that only when A is getting saved.

My problem is even id of B has on B.Update as group, it still validates in controller (with group A.Save). May be issue is the @Valid annotation but without that annotation it does not validates nested things.

Sapowith answered 4/1, 2017 at 6:21 Comment(1)
That behavior sounds like a bug somewhere. Is there any chance you provide an isolated test case (ideally only containing Bean Validation and nothing else)?Zwieback

© 2022 - 2024 — McMap. All rights reserved.