javagradleopenapivert.xvertx-httpclient

Vert.x OpenAPI v4.5.10 ValidatorException: "The related request does not contain the required body" for POST request


I'm working with Vert.x and OpenAPI to create an HTTP server, and I'm encountering a ValidatorException when handling a request with a required body. Specifically, the error message is: The related request does not contain the required body.

public void start() {
    String pathContract = "./api.yaml";
    contract = OpenAPIContract.from(vertx , pathContract);

    contract.onSuccess(contractRes -> {
        routerBuilder = RouterBuilder.create(vertx, contractRes , RequestExtractor.withBodyHandler());

        requestValidator = RequestValidator.create(vertx , contractRes);
        responseValidator = ResponseValidator.create(vertx , contractRes);

        routerBuilder.getRoute("addUser").addHandler(this::addUser);
        router = routerBuilder.createRouter();

        vertx.createHttpServer().requestHandler(router).listen(8000 , res -> {
            if (res.succeeded()) {
                System.out.println("server is running");
            } else {
                System.out.println("error " + res.cause());
            }
        });
        System.out.println("Validator created successfully.");
    }).onFailure(error -> {
        System.out.println("Failed to load OpenAPI contract: " + error.getMessage());
    });
}

public void addUser(RoutingContext ctx) {
    requestValidator.validate(ctx.request(), "addUser").onSuccess(validatedRequest -> {
        JsonObject bodyData = validatedRequest.getBody().getJsonObject();
        String name = bodyData.getString("name");

        if (!name.isEmpty()) {
            ctx.response().end("name is : " + name);
        } else {
            ctx.response().end("error " + name);
        }
    }).onFailure(err -> {
        ctx.response().setStatusCode(400).end("error " + err.getMessage());
    });
}

yaml :

  /user :
    post:
      summary : Add a new user
      operationId: addUser
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
      responses:
        '201':
          description : user created

When I disable validation with route.setDoValidation(false), the request works without errors.

route.setDoValidation(false);

I tried to set up request validation for a POST route (/addUser) in Vert.x OpenAPI v4.5.10, following the OpenAPI definition in api.yaml. I expected the validator to confirm the presence and structure of the required JSON body. Instead, I received a ValidatorException stating "The related request does not contain the required body." However, when I disable validation with route.setDoValidation(false), the route works as expected, but this bypasses validation, which I need active to check the body structure.


Solution

  • The issue occurs because Vert.x needs a body handler to extract the request body before validation.

    routerBuilder.rootHandler(BodyHandler.create());