Skip to content

Commit 3eea70b

Browse files
authored
Add io.avaje.http.api @Valid Annotation to support custom validators (#195)
* valid * ignore BeanField * Revert "ignore BeanField" This reverts commit bd70d8a. * Update MethodReader.java * Update README.md * Update README.md
1 parent 97f4d3c commit 3eea70b

File tree

7 files changed

+42
-15
lines changed

7 files changed

+42
-15
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ The annotation processor will generate controller adapters that can register rou
8080
There isn't a hard requirement to use Avaje for dependency injection. In the absence of avaje-inject the generated class will use `@jakarta.inject.Singleton` or `@javax.inject.Singleton` depending on what's on the classpath. Any DI library that can find and wire the generated @Singleton beans can be used. You can even use Dagger2 or Guice to wire the controllers if you so desire.
8181

8282
To force the AP to generate with `@javax.inject.Singleton`(in the case where you have both jakarta and javax on the classpath), use the compiler arg `-AuseJavax=true`
83-
```
84-
<plugin>
85-
<groupId>org.apache.maven.plugins</groupId>
86-
<artifactId>maven-compiler-plugin</artifactId>
87-
<configuration>
88-
<compilerArgs>
89-
<arg>-AuseJavax=true</arg>
90-
</compilerArgs>
91-
</configuration>
92-
</plugin>
83+
```xml
84+
<plugin>
85+
<groupId>org.apache.maven.plugins</groupId>
86+
<artifactId>maven-compiler-plugin</artifactId>
87+
<configuration>
88+
<compilerArgs>
89+
<arg>-AuseJavax=true</arg>
90+
</compilerArgs>
91+
</configuration>
92+
</plugin>
9393
```
9494

9595
### Usage with Javalin
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.avaje.http.api;
2+
3+
import static java.lang.annotation.ElementType.METHOD;
4+
import static java.lang.annotation.ElementType.PARAMETER;
5+
import static java.lang.annotation.ElementType.TYPE;
6+
import static java.lang.annotation.RetentionPolicy.SOURCE;
7+
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.Target;
10+
11+
/**
12+
*
13+
* Add @Valid annotation on a controller/method/BeanParam that we want bean validation to be included for.
14+
* When we do this controller methods that take a request payload will then have the request bean
15+
* (populated by JSON payload or form parameters) validated before it is passed to the controller
16+
* method.
17+
*
18+
* When trying to validate a @BeanParam bean, this will need to be placed on the BeanParam type
19+
20+
*/
21+
@Retention(SOURCE)
22+
@Target({METHOD, TYPE, PARAMETER})
23+
public @interface Valid {}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ private boolean initDocHidden() {
162162
private boolean initHasValid() {
163163

164164
return findAnnotation(JavaxValidPrism::getOptionalOn).isPresent()
165+
|| findAnnotation(JakartaValidPrism::getOptionalOn).isPresent()
165166
|| findAnnotation(ValidPrism::getOptionalOn).isPresent();
166167
}
167168

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ private boolean useValidation() {
129129
}
130130
final var elementType = typeElement(rawType);
131131
return elementType != null
132-
&& (ValidPrism.isPresent(elementType) || JavaxValidPrism.isPresent(elementType));
132+
&& (ValidPrism.isPresent(elementType)
133+
|| JavaxValidPrism.isPresent(elementType)
134+
|| JakartaValidPrism.isPresent(elementType));
133135
}
134136

135137
private void readAnnotations(Element element, ParamType defaultType) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private Javadoc buildJavadoc(ExecutableElement element) {
9292
private boolean initValid() {
9393
return findAnnotation(ValidPrism::getOptionalOn).isPresent()
9494
|| findAnnotation(JavaxValidPrism::getOptionalOn).isPresent()
95+
|| findAnnotation(JakartaValidPrism::getOptionalOn).isPresent()
9596
|| superMethodHasValid();
9697
}
9798

http-generator-core/src/main/java/io/avaje/http/generator/core/package-info.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
@GeneratePrism(value = io.avaje.http.api.OpenAPIResponse.class, publicAccess = true)
2727
@GeneratePrism(value = io.avaje.http.api.OpenAPIResponses.class, publicAccess = true)
2828
@GeneratePrism(value = io.swagger.v3.oas.annotations.Hidden.class, publicAccess = true)
29-
@GeneratePrism(value = javax.validation.Valid.class, name = "JavaxValidPrism", publicAccess = true)
30-
@GeneratePrism(value = jakarta.validation.Valid.class, publicAccess = true)
3129
@GeneratePrism(value = org.jetbrains.annotations.NotNull.class, publicAccess = true)
3230
@GeneratePrism(value = io.avaje.http.api.Client.class, publicAccess = true)
3331
@GeneratePrism(value = io.avaje.http.api.Client.Import.class, publicAccess = true)
3432
@GeneratePrism(value = io.avaje.http.api.RequestTimeout.class, publicAccess = true)
33+
@GeneratePrism(value = javax.validation.Valid.class, name = "JavaxValidPrism")
34+
@GeneratePrism(value = jakarta.validation.Valid.class, name = "JakartaValidPrism")
35+
@GeneratePrism(value = io.avaje.http.api.Valid.class)
3536
package io.avaje.http.generator.core;
3637

3738
import io.avaje.prism.GeneratePrism;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import java.util.concurrent.CompletableFuture;
99
import java.util.concurrent.Executors;
1010

11-
import javax.validation.Valid;
12-
1311
import org.example.myapp.service.MyService;
1412

1513
import io.avaje.http.api.BeanParam;
@@ -23,6 +21,7 @@
2321
import io.avaje.http.api.Post;
2422
import io.avaje.http.api.Produces;
2523
import io.avaje.http.api.QueryParam;
24+
import io.avaje.http.api.Valid;
2625
import io.javalin.http.Context;
2726
import io.swagger.v3.oas.annotations.Hidden;
2827
import jakarta.inject.Inject;

0 commit comments

Comments
 (0)