We recently upgraded one of our microservices from Spring Boot 2.2 to 2.3 and ran into a bug with hibernate validator that seems to affect all 6.1.x versions. Here is an example that reproduces the issue.
``` {code:java} @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Parent.class) public class Parent { @NotNull private Long id;
@Valid private Set<Child> children = new HashSet<>(); // getters and setters }
@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Child.class) public class Child { @NotNull private Long id;
@NotNull @Valid private Parent parent; // getters and setters }
@RestController @Validated public class ValidatorController { @GetMapping("/") public Parent get() { final Parent parent = new Parent(); parent.setId(1L);
final Child child = new Child(); child.setId(1L); child.setParent(parent);
parent.getChildren().add(child);
return parent; }
@PostMapping("/") public void post(@Valid @NotNull @RequestBody Parent parent) { System.out.println(parent.getId()); } }
@SpringBootTest @RunWith(SpringRunner.class) @AutoConfigureMockMvc public class ValidatorTest { @Autowired private MockMvc mvc;
@Test public void test() throws Exception { final MvcResult result = mvc.perform(get("/").accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()).andReturn();
final String json = result.getResponse().getContentAsString(); System.out.println(json);
mvc.perform(post("/").contentType(MediaType.APPLICATION_JSON).content(json)) .andExpect(status().is2xxSuccessful()); } } {code} ```
This will trigger a stack overflow...
``` {noformat} at org.hibernate.validator.internal.engine.ValidatorImpl.access$300(ValidatorImpl.java:84) at org.hibernate.validator.internal.engine.ValidatorImpl$CascadingValueReceiver.doValidate(ValidatorImpl.java:715) at org.hibernate.validator.internal.engine.ValidatorImpl$CascadingValueReceiver.iterableValue(ValidatorImpl.java:675) at org.hibernate.validator.internal.engine.valueextraction.IterableValueExtractor.extractValues(IterableValueExtractor.java:24) at org.hibernate.validator.internal.engine.valueextraction.IterableValueExtractor.extractValues(IterableValueExtractor.java:14) at org.hibernate.validator.internal.engine.valueextraction.ValueExtractorHelper.extractValues(ValueExtractorHelper.java:42) at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedContainerElementsForCurrentGroup(ValidatorImpl.java:651) at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedConstraints(ValidatorImpl.java:598) at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:409) at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedAnnotatedObjectForCurrentGroup(ValidatorImpl.java:629) at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedConstraints(ValidatorImpl.java:590) at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:409) at org.hibernate.validator.internal.engine.ValidatorImpl.access$300(ValidatorImpl.java:84) ``` {noformat} |
|