Skip to content
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

Validation is not working on nested REST endpoints #40550

Closed
BureschT opened this issue May 10, 2024 · 1 comment
Closed

Validation is not working on nested REST endpoints #40550

BureschT opened this issue May 10, 2024 · 1 comment
Labels
area/hibernate-validator Hibernate Validator kind/bug Something isn't working triage/invalid This doesn't seem right

Comments

@BureschT
Copy link

BureschT commented May 10, 2024

Describe the bug

I have the following REST service with a nested endpoint. Validation does not work on the nested end point.

@Path("/api/test/validator")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Singleton
public class ValidatorTestRESTService {

    @GET
    @Path("/annotatedbean")
    public Response annotatedbean(@BeanParam @Valid InputParams params) {
        return Response.ok().entity(params).build();
    }


    @Path("/nested/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public NestedRESTResource nested(@PathParam("id") String id) {
        return new NestedRESTResource(id);
    }

    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @AllArgsConstructor
    public static class NestedRESTResource {

        private String id;

        @GET
        @Path("/annotatedbean")
        public Response annotatedbean(@BeanParam @Valid InputParams params) {
            return Response.ok().entity(params).build();
        }
    }
}

@Getter
@Setter
public class InputParams {

    @QueryParam("mandatoryString")
    @NotNull(message = "mandatoryString is mandatory")
    String mandatoryString;

}

Expected behavior

When I call "/api/test/validator/nested/42/annotatedbean" without providing "mandatoryString" parameter, I expect a HTTP 400 (bad request) response. A jakarta.validation.ConstraintViolationException thrown in the background , so I could map that exception too.

Actual behavior

Instead, I get HTTP 200 (OK)

How to Reproduce?

nested-rest-validation-issue.zip

Output of uname -a or ver

No response

Output of java -version

openjdk version "17.0.9" 2023-10-17

Quarkus version or git rev

3.10.0

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.6

Additional information

No response

@BureschT BureschT added the kind/bug Something isn't working label May 10, 2024
@geoand geoand added area/hibernate-validator Hibernate Validator and removed triage/needs-triage labels May 10, 2024
@geoand
Copy link
Contributor

geoand commented May 17, 2024

What you are trying to do will not work properly because you are constructing NestedRESTResource manually.

One way of making it work is to use something like:

    @Path("/nested/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public NestedRESTResource nested(@Context ResourceContext context) {
        return context.getResource(NestedRESTResource.class);
    }

    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Singleton
    public static class NestedRESTResource {

        @GET
        @Path("/annotatedbean")
        public Response annotatedbean(@BeanParam @Valid InputParams params, @PathParam("id") String id) {
            return Response.ok().entity(params).build();
        }
    }

@geoand geoand closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2024
@geoand geoand added the triage/invalid This doesn't seem right label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hibernate-validator Hibernate Validator kind/bug Something isn't working triage/invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants