smallrye-open-api icon indicating copy to clipboard operation
smallrye-open-api copied to clipboard

BeanValidation @NotNull on a request body parameter should mark the body parameter as a required parameter

Open xfh opened this issue 1 year ago • 2 comments

BeanValidation's @NotNull annotation integrates seamlessly into openapi schema generation - I much appreciated feature.

Unfortunately, there is an inconsistency regarding @NotNull and method parameters.

The following endpoint doesn't mark the input parameter Payload as required:

@POST
public Response create(@Nonnull @NotNull @Valid Payload payload) {
    return Response.ok().build();
}

generated schema:

    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Payload"

Manually adding @RequestBody(required = true) the the create method yields in the desired output:

    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Payload"
        required: true

It would be really nice, if a @NotNull annotation on a body parameter automatically added the required: true flag.

xfh avatar Jul 17 '24 17:07 xfh

The following worked for me:

@POST
public Response create(@Nonnull @NotNull @Valid @RequestBody Payload payload) {
    return Response.ok().build();
}

cristalp avatar Aug 27 '24 12:08 cristalp

I'm sorry, I was wrong (probably issues with dependencies). It doesn't work the way I wrote, the request body is not generated as required.

cristalp avatar Aug 27 '24 15:08 cristalp

@xfh , @cristalp - this should no longer be an issue as of version 4.0. Request bodies are marked as required: true by default (not due to the not-null constraint).

MikeEdgar avatar Nov 03 '24 12:11 MikeEdgar

Thanks, works as described.

cristalp avatar Nov 04 '24 07:11 cristalp

Since version 4.0 makes request bodies required: true by default, it no longer would make sense to set it as a result of @NotNull. I will go ahead and close this issue, but please comment or re-open if you think I'm overlooking something.

MikeEdgar avatar Nov 11 '24 11:11 MikeEdgar