Quarkus not validate DTO with hibernate-validator

320 views Asked by At

I have an endpoint based on Quarkus:

import com.company.report.dto.ReportDto
import com.company.report.service.ReportService
import javax.enterprise.context.ApplicationScoped
import javax.validation.Valid

import javax.ws.rs.Consumes
import javax.ws.rs.POST
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response

@ApplicationScoped
@Path("/app")
class ReportController(
    private val reportService: ReportService
) {

    @POST
    @Path("/report")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/pdf")
    fun generateReport(@Valid reportDto: ReportDto): Response {
        return Response
            .ok(reportService.generatePdfReport(reportDto), "application/pdf")
            .header("Content-Disposition", "inline; filename=\"Report.pdf\"")
            .build()
    }
}


import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import javax.validation.constraints.NotNull

@JsonIgnoreProperties(ignoreUnknown = true)
class ReportDto(
    @JsonProperty("rows") @NotNull val rows: List<ReportRowDto>
)


import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotNull

@JsonIgnoreProperties(ignoreUnknown = true)
class ReportRowDto (
    @JsonProperty("name") @NotBlank(message = "name cant be blank") @NotNull(message = "name cant be null") val name: String
)

@Provider
class ValidationExceptionMapper : ExceptionMapper<ConstraintViolationException> {
    override fun toResponse(exception: ConstraintViolationException): Response {
        return Response.status(Response.Status.BAD_REQUEST)
            .entity("Validation failed: ${exception.message}")
            .type(MediaType.TEXT_PLAIN)
            .build()
    }
}

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-validator</artifactId>
</dependency>

And javax.annotation is defined in parent pom and I can't change anything in there.

<dependency>
  <groupId>javax.annotation</groupId>
  <artifactId>javax.annotation-api</artifactId>
  <version>1.3.2</version>
</dependency>

If the name is null or empty I expect 400 and the message about the error. "name cant be blank" or "name cant be blank"accordingly annotation message parameter.

But instead of an error message in Postmen I've got 400 and Failed to load PDF file. and The request cannot be fulfilled due to bad syntax.

Why did it happen, and how to fix it?

ps. Yes, I know it should work with storage, and return the download link but in my case, I need to return the document in the body.

1

There are 1 answers

1
Guillaume Smet On

First, which version of Quarkus are you using? Because if you are using Quarkus 3+, you need to use jakarta.validation, not javax.validation. Quarkus 3 is based on Jakarta EE 10 with the jakarta namespace. javax.validation constraints will be completely ignored in Quarkus 3.

Then you miss a @Valid there:

@JsonProperty("rows") @NotNull val rows: List<ReportRowDto>