smallrye / smallrye-open-api

SmallRye implementation of Eclipse MicroProfile OpenAPI
Apache License 2.0
118 stars 90 forks source link

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

Open xfh opened 3 months ago

xfh commented 3 months ago

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.

cristalp commented 2 months ago

The following worked for me:

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

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.

MikeEdgar commented 2 days ago

@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).

cristalp commented 1 day ago

Thanks, works as described.