Skip to content

Commit 768f48a

Browse files
committed
#60 - Validation for bean params for controller class @get method
1 parent a89b69f commit 768f48a

File tree

5 files changed

+75
-15
lines changed

5 files changed

+75
-15
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/ElementReader.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import javax.lang.model.element.Element;
1414
import javax.lang.model.element.TypeElement;
15+
import javax.validation.Valid;
1516

1617
public class ElementReader {
1718

@@ -179,8 +180,16 @@ void buildApiDocumentation(MethodDocBuilder methodDoc) {
179180

180181
void writeValidate(Append writer) {
181182
if (!isPlatformContext() && typeHandler == null) {
182-
writer.append("validator.validate(%s);", varName).eol();
183-
writer.append(" ");
183+
TypeElement formBeanType = ctx.getTypeElement(rawType);
184+
if (formBeanType != null) {
185+
final Valid valid = formBeanType.getAnnotation(Valid.class);
186+
if (valid != null) {
187+
writer.append("validator.validate(%s);", varName).eol();
188+
} else {
189+
writer.append("// no validation required on %s", varName).eol();
190+
}
191+
writer.append(" ");
192+
}
184193
}
185194
}
186195

http-generator-core/src/main/java/io/avaje/http/generator/core/MethodReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public String getFullPath() {
233233
}
234234

235235
public boolean includeValidate() {
236-
return bean.isIncludeValidator() && webMethod != WebMethod.GET;
236+
return bean.isIncludeValidator();
237237
}
238238

239239
public String simpleName() {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.example.myapp.web;
2+
3+
import javax.validation.Valid;
4+
import javax.validation.constraints.Email;
5+
import javax.validation.constraints.NotNull;
6+
import javax.validation.constraints.Size;
7+
8+
@Valid
9+
public class GetBeanForm {
10+
11+
@NotNull @Size(min = 2, max = 150)
12+
String name;
13+
14+
@Email @Size(max = 100)
15+
String email;
16+
17+
public GetBeanForm(String name, String email) {
18+
this.name = name;
19+
this.email = email;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "HelloForm{" +
25+
"name='" + name + '\'' +
26+
", email='" + email + '\'' +
27+
'}';
28+
}
29+
}

tests/test-javalin/src/main/java/org/example/myapp/web/HelloController.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
package org.example.myapp.web;
22

3-
import io.avaje.http.api.Controller;
4-
import io.avaje.http.api.Default;
5-
import io.avaje.http.api.Delete;
6-
import io.avaje.http.api.Form;
7-
import io.avaje.http.api.Get;
8-
import io.avaje.http.api.MediaType;
9-
import io.avaje.http.api.Path;
10-
import io.avaje.http.api.Post;
11-
import io.avaje.http.api.Produces;
12-
import io.avaje.http.api.QueryParam;
3+
import io.avaje.http.api.*;
134
import io.javalin.http.Context;
145
import io.swagger.v3.oas.annotations.Hidden;
156
import org.example.myapp.service.MyService;
167

178
import jakarta.inject.Inject;
9+
1810
import javax.validation.Valid;
1911
import java.time.LocalDate;
2012
import java.util.ArrayList;
@@ -126,6 +118,12 @@ HelloDto saveForm3(HelloForm helloForm) {
126118
return new HelloDto(52, helloForm.name, helloForm.email);
127119
}
128120

121+
@Produces("text/plain")
122+
@Get("withValidBean")
123+
String getGetBeanForm(@BeanParam GetBeanForm bean) {
124+
return "ok name:" + bean.name;
125+
}
126+
129127
@Hidden
130128
@Get
131129
List<HelloDto> getAll() {

tests/test-javalin/src/test/java/org/example/myapp/HelloControllerTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class HelloControllerTest extends BaseWebTest {
4141
.with(httpClient)
4242
.build();
4343
}
44+
4445
@Test
4546
void hello() {
4647
final Response response = get(baseUrl + "/hello/message");
@@ -214,9 +215,9 @@ void postForm_validation_expect_badRequest() {
214215
.asVoid();
215216

216217
} catch (HttpException e) {
217-
assertEquals(422, e.getStatusCode());
218+
assertEquals(422, e.statusCode());
218219

219-
final HttpResponse<?> httpResponse = e.getHttpResponse();
220+
final HttpResponse<?> httpResponse = e.httpResponse();
220221
assertNotNull(httpResponse);
221222
assertEquals(422, httpResponse.statusCode());
222223

@@ -229,6 +230,29 @@ void postForm_validation_expect_badRequest() {
229230
}
230231
}
231232

233+
@Test
234+
void get_validate_bean_expect422() {
235+
final HttpResponse<String> hres = clientContext.request()
236+
.path("hello/withValidBean")
237+
.queryParam("email", "user@foo.com")
238+
.GET()
239+
.asString();
240+
241+
assertThat(hres.statusCode()).isEqualTo(422);
242+
}
243+
244+
@Test
245+
void get_validate_bean_expect200() {
246+
final HttpResponse<String> hres = clientContext.request()
247+
.path("hello/withValidBean")
248+
.queryParam("name", "hello")
249+
.queryParam("email", "user@foo.com")
250+
.GET()
251+
.asString();
252+
253+
assertThat(hres.statusCode()).isEqualTo(200);
254+
}
255+
232256
@Test
233257
void delete() {
234258
given().delete(baseUrl + "/hello/52")

0 commit comments

Comments
 (0)