openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[REQ] Support not null validation on elements of an array in Java/Spring

Open arkigabor opened this issue 1 year ago • 0 comments

Is your feature request related to a problem? Please describe.

When using the OpenAPI generator to generate Spring server code, it efficiently handles automatic validation for numerous constraints outlined in the API, even for list elements. However, it currently lacks the capability to specify that elements within a list should not be null.

This limitation results in input such as [null, null, null] being valid and accepted, even though the list type doesn't allow nullable, the list itself is marked as required (it cannot be null and has @NotNull annotation), and the elements have detailed constraints, such as string patterns or in a complex type of a list all fields are marked required.

Describe the solution you'd like

I would like the OpenAPI generator for Java/Spring to support placing the @NotNull annotation onto elements of an array. It seems that nullable can be declared on items, and even defaults to false. However, this seems to have no effect on the generated code which accepts both the declared type and null as elements.

My naive recommendation would be to adhere to the nullable attribute specified for items in an array. If nullable is set to false, the array should only accept the declared type but not null, and the @NotNull annotation should be automatically added to the element. If nullable is set to true, then the annotation should be omitted, thereby allowing null elements in the array. I think this approach would align the generated code's behavior closely with the explicit intentions stated in the OpenAPI specification.

Describe alternatives you've considered

After a lot of googling and finding recommendations to use nullable: false or try to achieve this with required, I'm fairly certain this is currently not supported by OpenAPI generator. Also, I found no option for this in Jackson either. The only viable alternative seems to be implementing a custom validation in every controller directly to ensure no explicit null values have been provided in arrays, or do the same with a ConstraintValidator implementation and a custom annotation forced onto the class or field.

Additional context

Essentially, declaring the following specification:

type: object
properties:
  exampleArray:
    type: array
    items:
      type: string

Would result in the code which doesn't allow for null elements during validation:

  @Schema(name = "exampleArray", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("exampleArray")
  public List<@NotNull String> getExampleArray() {
    return exampleArray;
  }

However, explicitly allowing the items to be nullable:

type: object
properties:
  exampleArray:
    type: array
    items:
      type: string
      nullable: true

Would result in a code that would also accept null items during validation:

  @Schema(name = "exampleArray", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("exampleArray")
  public List<String> getExampleArray() {
    return exampleArray;
  }

arkigabor avatar Oct 17 '24 11:10 arkigabor