From 8a19bbc3a01e530af4946d3aedc914b04eafb27a Mon Sep 17 00:00:00 2001 From: Pramoth Suwanpech Date: Thu, 31 Oct 2024 03:22:10 +0700 Subject: [PATCH 1/2] MyValidationInterceptor should add validation.error only if it extends renarde Controller else throw ResteasyReactiveViolationException. So rest reactive endpoint validation will work as normal --- .../renarde/util/MyValidationInterceptor.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/runtime/src/main/java/io/quarkiverse/renarde/util/MyValidationInterceptor.java b/runtime/src/main/java/io/quarkiverse/renarde/util/MyValidationInterceptor.java index d34bb810..28adc527 100644 --- a/runtime/src/main/java/io/quarkiverse/renarde/util/MyValidationInterceptor.java +++ b/runtime/src/main/java/io/quarkiverse/renarde/util/MyValidationInterceptor.java @@ -2,6 +2,7 @@ import java.util.Set; +import io.quarkiverse.renarde.Controller; import jakarta.annotation.Priority; import jakarta.inject.Inject; import jakarta.interceptor.AroundConstruct; @@ -35,8 +36,12 @@ public Object validateMethodInvocation(InvocationContext ctx) throws Exception { ctx.getMethod(), ctx.getParameters()); if (!violations.isEmpty()) { - // just collect them and go on - validation.addErrors(violations); + if(ctx.getTarget() instanceof Controller){ + // just collect them and go on + validation.addErrors(violations); + }else{ + throw new ResteasyReactiveViolationException(violations); + } } Object result = ctx.proceed(); From 4ecc0e31cd38de8d17b7802bd593bc9c7f57763a Mon Sep 17 00:00:00 2001 From: Pramoth Suwanpech Date: Fri, 1 Nov 2024 20:28:14 +0700 Subject: [PATCH 2/2] add testcase for validation on rest endpoint /renard endpoint --- .../main/java/rest/ValidationController.java | 30 +++++++++++++++++++ .../renarde/it/ValidationTest.java | 26 ++++++++++++++++ .../renarde/util/MyValidationInterceptor.java | 6 ++-- 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 integration-tests/src/main/java/rest/ValidationController.java create mode 100644 integration-tests/src/test/java/io/quarkiverse/renarde/it/ValidationTest.java diff --git a/integration-tests/src/main/java/rest/ValidationController.java b/integration-tests/src/main/java/rest/ValidationController.java new file mode 100644 index 00000000..98607b66 --- /dev/null +++ b/integration-tests/src/main/java/rest/ValidationController.java @@ -0,0 +1,30 @@ +package rest; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +import org.jboss.resteasy.reactive.RestQuery; + +import io.quarkiverse.renarde.Controller; + +public class ValidationController { + @Path("/ValidationController") + public static class RenardController extends Controller { + @GET + @Path("/RenardController") + public void renardValidate(@Valid @NotNull @RestQuery String input) { + + } + } + + @Path("/ValidationController") + public static class RestController { + @GET + @Path("/RestController") + public void restValidate(@Valid @NotNull @RestQuery String input) { + + } + } +} diff --git a/integration-tests/src/test/java/io/quarkiverse/renarde/it/ValidationTest.java b/integration-tests/src/test/java/io/quarkiverse/renarde/it/ValidationTest.java new file mode 100644 index 00000000..260205a3 --- /dev/null +++ b/integration-tests/src/test/java/io/quarkiverse/renarde/it/ValidationTest.java @@ -0,0 +1,26 @@ +package io.quarkiverse.renarde.it; + +import static io.restassured.RestAssured.given; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +public class ValidationTest { + @Test + public void validateOnRenardeEndpointShouldResponse20x() { + given() + .when().get("/ValidationController/RenardController") + .then() + .statusCode(204); + } + + @Test + public void validateOnRestEndpointShouldResponse400() { + given() + .when().get("/ValidationController/RestController") + .then() + .statusCode(400); + } +} diff --git a/runtime/src/main/java/io/quarkiverse/renarde/util/MyValidationInterceptor.java b/runtime/src/main/java/io/quarkiverse/renarde/util/MyValidationInterceptor.java index 28adc527..0afac200 100644 --- a/runtime/src/main/java/io/quarkiverse/renarde/util/MyValidationInterceptor.java +++ b/runtime/src/main/java/io/quarkiverse/renarde/util/MyValidationInterceptor.java @@ -2,7 +2,6 @@ import java.util.Set; -import io.quarkiverse.renarde.Controller; import jakarta.annotation.Priority; import jakarta.inject.Inject; import jakarta.interceptor.AroundConstruct; @@ -13,6 +12,7 @@ import jakarta.validation.Validator; import jakarta.validation.executable.ExecutableValidator; +import io.quarkiverse.renarde.Controller; import io.quarkus.hibernate.validator.runtime.interceptor.AbstractMethodValidationInterceptor; import io.quarkus.hibernate.validator.runtime.jaxrs.JaxrsEndPointValidated; import io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyReactiveViolationException; @@ -36,10 +36,10 @@ public Object validateMethodInvocation(InvocationContext ctx) throws Exception { ctx.getMethod(), ctx.getParameters()); if (!violations.isEmpty()) { - if(ctx.getTarget() instanceof Controller){ + if (ctx.getTarget() instanceof Controller) { // just collect them and go on validation.addErrors(violations); - }else{ + } else { throw new ResteasyReactiveViolationException(violations); } }