Skip to content

Commit 11dfd04

Browse files
committed
Bug Report: Hibernate Validator Not Working in Script/Lambda Mode (BeanValidator.validate(...))
- fix #3751
1 parent 6d07b4a commit 11dfd04

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

jooby/src/main/java/io/jooby/validation/ValidationContext.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public <T> T to(@NonNull Type type) {
9797
return BeanValidator.apply(ctx, ((Body) delegate).toNullable(type));
9898
}
9999

100+
@NonNull @Override
101+
public <T> T to(@NonNull Class<T> type) {
102+
// Call nullable version to let bean validator to run
103+
return BeanValidator.apply(ctx, ((Body) delegate).toNullable(type));
104+
}
105+
100106
@Nullable @Override
101107
public <T> T toNullable(@NonNull Type type) {
102108
return BeanValidator.apply(ctx, ((Body) delegate).toNullable(type));
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3751;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
import java.util.List;
11+
12+
import io.jooby.hibernate.validator.HibernateValidatorModule;
13+
import io.jooby.jackson.JacksonModule;
14+
import io.jooby.junit.ServerTest;
15+
import io.jooby.junit.ServerTestRunner;
16+
import io.jooby.validation.BeanValidator;
17+
import jakarta.validation.Validator;
18+
import okhttp3.MediaType;
19+
import okhttp3.RequestBody;
20+
21+
public class Issue3751 {
22+
@ServerTest
23+
public void shouldValidateDataBody(ServerTestRunner runner) {
24+
runner
25+
.define(
26+
app -> {
27+
app.install(new HibernateValidatorModule());
28+
app.install(new JacksonModule());
29+
app.post(
30+
"/3751-direct",
31+
(ctx -> {
32+
Validator validator = ctx.require(Validator.class);
33+
return validator.validate(ctx.body(User3721.class));
34+
}));
35+
app.post(
36+
"/3751-bean-validator",
37+
BeanValidator.validate(
38+
ctx -> {
39+
ctx.body(User3721.class);
40+
// all good
41+
return List.of();
42+
}));
43+
})
44+
.ready(
45+
http -> {
46+
var user =
47+
RequestBody.create(
48+
"{\"password\": \"1234\"}", MediaType.parse("application/json"));
49+
http.post(
50+
"/3751-direct",
51+
user,
52+
rsp -> {
53+
http.post(
54+
"/3751-direct",
55+
user,
56+
rsp2 -> {
57+
assertEquals(rsp.body().string(), rsp2.body().string());
58+
});
59+
});
60+
});
61+
}
62+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3751;
7+
8+
import jakarta.validation.constraints.NotBlank;
9+
10+
public class User3721 {
11+
@NotBlank(message = "password can not empty") private String password;
12+
13+
public String getPassword() {
14+
return password;
15+
}
16+
17+
public void setPassword(String password) {
18+
this.password = password;
19+
}
20+
}

0 commit comments

Comments
 (0)