Skip to content

Add io.avaje.http.api @Valid Annotation to support custom validators #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions http-api/src/main/java/io/avaje/http/api/IgnoreBeanField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.avaje.http.api;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.SOURCE;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/** Mark a field on a BeanParam/FormParam class as not a request parameter of any kind */
@Target(FIELD)
@Retention(SOURCE)
public @interface IgnoreBeanField {}
23 changes: 23 additions & 0 deletions http-api/src/main/java/io/avaje/http/api/Valid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.avaje.http.api;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.SOURCE;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
*
* Add @Valid annotation on a controller/method/BeanParam that we want bean validation to be included for.
* When we do this controller methods that take a request payload will then have the request bean
* (populated by JSON payload or form parameters) validated before it is passed to the controller
* method.
*
* When trying to validate a @BeanParam bean, this will need to be placed on the BeanParam type

*/
@Retention(SOURCE)
@Target({METHOD, TYPE, PARAMETER})
public @interface Valid {}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ private void read() {
}

private void readField(Element enclosedElement) {

if (IgnoreBeanFieldPrism.isPresent(enclosedElement)) {
return;
}
FieldReader field = new FieldReader(enclosedElement, defaultParamType);
fieldMap.put(field.varName(), field);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ private boolean initDocHidden() {
private boolean initHasValid() {

return findAnnotation(JavaxValidPrism::getOptionalOn).isPresent()
|| findAnnotation(JakartaValidPrism::getOptionalOn).isPresent()
|| findAnnotation(ValidPrism::getOptionalOn).isPresent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ private boolean useValidation() {
}
final var elementType = typeElement(rawType);
return elementType != null
&& (ValidPrism.isPresent(elementType) || JavaxValidPrism.isPresent(elementType));
&& (ValidPrism.isPresent(elementType)
|| JavaxValidPrism.isPresent(elementType)
|| JakartaValidPrism.isPresent(elementType));
}

private void readAnnotations(Element element, ParamType defaultType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** Generate the prisms to access annotation info */
@GeneratePrism(value = io.avaje.http.api.Controller.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.BeanParam.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.IgnoreBeanField.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.QueryParam.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Client.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Cookie.class, publicAccess = true)
Expand All @@ -26,12 +27,13 @@
@GeneratePrism(value = io.avaje.http.api.OpenAPIResponse.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.OpenAPIResponses.class, publicAccess = true)
@GeneratePrism(value = io.swagger.v3.oas.annotations.Hidden.class, publicAccess = true)
@GeneratePrism(value = javax.validation.Valid.class, name = "JavaxValidPrism", publicAccess = true)
@GeneratePrism(value = jakarta.validation.Valid.class, publicAccess = true)
@GeneratePrism(value = org.jetbrains.annotations.NotNull.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Client.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Client.Import.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.RequestTimeout.class, publicAccess = true)
@GeneratePrism(value = javax.validation.Valid.class, name = "JavaxValidPrism")
@GeneratePrism(value = jakarta.validation.Valid.class, name = "JakartaValidPrism")
@GeneratePrism(value = io.avaje.http.api.Valid.class)
package io.avaje.http.generator.core;

import io.avaje.prism.GeneratePrism;
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import javax.validation.constraints.Size;

import io.avaje.http.api.Header;
import io.avaje.http.api.IgnoreBeanField;
import io.avaje.http.api.QueryParam;
import io.avaje.jsonb.Json;
import io.avaje.jsonb.Json.Ignore;

@Json
@Valid
Expand All @@ -30,6 +32,12 @@ public class GetBeanForm {

@QueryParam private Set<ServerType> type;

@Ignore @IgnoreBeanField private String ignored;

public String getIgnored() {
return ignored;
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;

import javax.validation.Valid;

import org.example.myapp.service.MyService;

import io.avaje.http.api.BeanParam;
Expand All @@ -23,6 +21,7 @@
import io.avaje.http.api.Post;
import io.avaje.http.api.Produces;
import io.avaje.http.api.QueryParam;
import io.avaje.http.api.Valid;
import io.javalin.http.Context;
import io.swagger.v3.oas.annotations.Hidden;
import jakarta.inject.Inject;
Expand Down